I want Discord bot to be able to randomly send images in the list in response to specific words.

Asked 2 years ago, Updated 2 years ago, 102 views

I'd like to send random images to specific words on the discord bot.
I asked you a question because I don't know.
At this stage, it is possible to take random items on the list, but I don't know how to take them as strings instead of files...

Language Used
python 3.9.1

source code

import discord

import random

from discord import channel

TOKEN=----------------------------------------------------

CHANNEL_ID=--------------------------------------------------------

client=discord.Client()

@client.event
async def on_ready():

print('Logged in')
print(client.user.name)
print(client.user.id)
print('_______')


@client.event
async def on_message(message):

if message.author.bot:
return

if message.content=='Good morning':
wait message.channel.send('Oh my gosh!')


if message.content=='Free':
wait message.channel.send('What's wrong?')

if message.content==' 〇 '':
file = ["pathname", "pathname" ]
file2 = random.choice(file)
wait message.channel.send(file2)

# """Event handler to be executed when new members join"""
@client.event
async def on_member_join(member):
wait member.send('Nice to meet you~!')

# events at bot startup
async def greet():
channel=client.get_channel(CHANNEL_ID)
wait channel.send('Everyone~'!Oh my gosh!)

@client.event
async def on_ready():
wait great()

client.run (TOKEN)

python python3 api discord

2022-09-29 22:19

1 Answers

 file=["pathname", "pathname"]
file2 = random.choice(file)

At this stage, file2 (the selected file) is still just a path.
Therefore,

wait message.channel.send(file2)

The only sends the path.

Use discord.File to send the file to discord.py.
Generally speaking,

 file=discord.File("file path.png", filename="file name.png")
wait message.channel.send(file=file)

is the case.
If this is the random specification I mentioned earlier,

if message.content==' 〇 '':
    file = ["pathname", "pathname 2" ]
    file2 = random.choice(file)
    file3=discord.File(file2, filename=file2)
    wait message.channel.send(file=file3)

will be


2022-09-29 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.