Discord Python:如何在发送GIF时提及用户

时间:2018-09-11 01:11:35

标签: python-3.x discord discord.py

我一直在寻找一种方法来编码我的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)

在第二个代码中,我不知道如何添加图像。最好是,我想用它生成随机图像,这就是为什么我提供了两个代码示例。

1 个答案:

答案 0 :(得分:0)

您可以使用提及信息格式化消息,然后再链接到消息。

 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)