繁体   English   中英

如何检查 discord.py 中的文件扩展名

[英]how to check for file extension in discord.py

我正在创建一个 python discord 机器人,并且我有一个 function ,如果在某个通道中发送消息以做出反应,它会随机发送一个限制机器人发送的消息,当我包含发送的消息时,一切正常但我想限制响应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("Фигня творчество")

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!

您可以使用以下条件来限制它:

if message.attachments[0].url.endswith('PNG') or message.attachments[0].url.endswith('JPG') or message.attachments[0].url.endswith('JPEG'):
    pass
else:
    pass

根据您的内容更改通行证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM