【问题标题】:Discord Python: How to mention a user along with sending a GIFDiscord Python:如何在发送 GIF 时提及用户
【发布时间】:2018-09-11 18:37:35
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您使用提及和链接来格式化消息。

     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)
    

    【讨论】:

    • 谢谢。我改变了一些东西,终于得到了我想要的。在格式后的括号内,我在“user.mention”之前添加了“message.author.mention”,这样我就可以提及我自己以及我在命令中提到的用户:)
    猜你喜欢
    • 2021-07-20
    • 2021-10-24
    • 2020-07-27
    • 2021-04-13
    • 2020-11-26
    • 2018-10-30
    • 2018-08-17
    • 2021-04-05
    • 2021-02-07
    相关资源
    最近更新 更多