我一直在寻找一种方法来编码我的Discord机器人,提到一个被某个命令标记的用户,以及发送一个图像/ gif来处理该消息。到目前为止,我能够从一个命令生成随机图像,并提及具有不同命令的用户。我只需要弄清楚如何为一个命令实现这两个命令。
这是我用来从一个命令生成随机图像的代码:
client = Bot(command_prefix=BOT_PREFIX)
@client.event
async def on_message(message):
if message.content.upper().startswith("?DOG"):
jessie1 = "https://cdn.discordapp.com/attachments/432563417887277060/484484259386621993/22B25E7A-3157-4C23-B889-47ECFE8A15A9.jpg"
snowy = "https://cdn.discordapp.com/attachments/487045791697862666/487390822485065749/824B6151-E818-49A4-A564-C2C752ED6384.jpg"
await client.send_message(message.channel, random.choice([snowy, jessie1]))
以下是我用来提及其他用户的代码:
elif message.content.upper().startswith('?GIVE BANANA'):
user = message.mentions[0]
responses = ["{} gave a banana to {} :banana:"]
choice = random.choice(responses)
choice = choice.format(message.author.mention, user.mention)
await client.send_message(message.channel, choice)
在第二个代码中,我无法弄清楚如何使用它添加图像。最好,我想用它生成随机图像,这就是我提供两个代码示例的原因。
您可以使用提及格式化消息,然后指向消息的链接。
elif message.content.upper().startswith('?SEND GIF'):
user = message.mentions[0]
jessie1 = "https://cdn.discordapp.com/attachments/432563417887277060/484484259386621993/22B25E7A-3157-4C23-B889-47ECFE8A15A9.jpg"
snowy = "https://cdn.discordapp.com/attachments/487045791697862666/487390822485065749/824B6151-E818-49A4-A564-C2C752ED6384.
gif_message = "{} here is the gif {}".format(user.mention, random.choice([snowy, jessie1]))
await client.send_message(message.channel, gif_message)