i am creating a python discord bot, and i have a function that if a message is sent in a certain channel, it randomly sends out a response,everything works fine but i want to restrict the bot to react only when the sent message contains a png, jpg or jpeg file
heres the code(look for the bottom if str(message.channel) statement):
@client.event
async def on_message(message):
await client.process_commands(message)
if str(message.channel) not in restr:
if message.content == ".sabaton":
random.seed(time.time())
randnum = random.randint(0, len(songs) - 1)
await message.channel.send(file=discord.File(songs[randnum]))
elif message.content == ".time":
await message.channel.send(f"Current time is {time.localtime()}")
elif message.content == ".pmsh":
await message.channel.send(help)
if client.user.mention in message.content.split():
await message.channel.send('Дарова гандон(иха),мой префикс "."')
if str(message.channel) == "🥳творчество🥳":
random.seed(time.time())
rn3 = random.randint(1,2)
if rn3 == 1 and message.author.bot == False:
await message.channel.send("Заебись творчество")
elif rn3 == 2 and message.author.bot == False:
await message.channel.send("Фигня творчество")
Attachments in discord.py have information about their file type in their content_type attribute. This should hopefully make for cleaner code too!
Apart from the access, @Sofi's logic still applies:
for attachment in message.attachments:
if attachment.content_type in ('image/jpeg', 'image/jpg', 'image/png'):
# Your Logic Here
If you just need to check if something is an image, your code can be simpler:
for attachment in message.attachments:
if 'image' in attachment.content_type:
# Your Logic Here!
You can use the following conditions to restrict it:
if message.attachments[0].url.endswith('PNG') or message.attachments[0].url.endswith('JPG') or message.attachments[0].url.endswith('JPEG'):
pass
else:
pass
Change the pass according to your content.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.