如何检查 discord.py 中的文件扩展名
how to check for file extension in discord.py
我正在创建一个 python discord 机器人,我有一个功能,如果在某个频道发送消息,
它随机发出一个响应,一切正常,但我想限制机器人仅在发送的消息包含 png、jpg 或 jpeg 文件时做出反应
这里是代码(寻找底部的 if str(message.channel) 语句):
@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("Фигня творчество")
您可以使用以下条件进行限制:
if message.attachments[0].url.endswith('PNG') or message.attachments[0].url.endswith('JPG') or message.attachments[0].url.endswith('JPEG'):
pass
else:
pass
根据您的内容更改通行证。
discord.py 中的附件在其 content_type 属性中包含有关其文件类型的信息。这也有望使代码更清晰!
除了访问,@Sofi的逻辑仍然适用:
for attachment in message.attachments:
if attachment.content_type in ('image/jpeg', 'image/jpg', 'image/png'):
# Your Logic Here
如果你只需要检查某物是否是图片,你的代码可以更简单:
for attachment in message.attachments:
if 'image' in attachment.content_type:
# Your Logic Here!
我正在创建一个 python discord 机器人,我有一个功能,如果在某个频道发送消息, 它随机发出一个响应,一切正常,但我想限制机器人仅在发送的消息包含 png、jpg 或 jpeg 文件时做出反应
这里是代码(寻找底部的 if str(message.channel) 语句):
@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("Фигня творчество")
您可以使用以下条件进行限制:
if message.attachments[0].url.endswith('PNG') or message.attachments[0].url.endswith('JPG') or message.attachments[0].url.endswith('JPEG'):
pass
else:
pass
根据您的内容更改通行证。
discord.py 中的附件在其 content_type 属性中包含有关其文件类型的信息。这也有望使代码更清晰!
除了访问,@Sofi的逻辑仍然适用:
for attachment in message.attachments:
if attachment.content_type in ('image/jpeg', 'image/jpg', 'image/png'):
# Your Logic Here
如果你只需要检查某物是否是图片,你的代码可以更简单:
for attachment in message.attachments:
if 'image' in attachment.content_type:
# Your Logic Here!