[英]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.