【问题标题】:Mentioning Users and Self with Discord.PY用 Discord.PY 提及用户和自我
【发布时间】:2019-01-25 02:19:39
【问题描述】:

我对编码还是很陌生,我可以让这个命令工作的唯一方法是做我在下面做的事情。我很确定有更好的方法来编写它,但它并不是我想要的那样。

我试图在开头提及使用该命令的用户,然后提及消息中提到的用户。 message.author.name 仅返回名称,而不是使用该命令的用户的实际标签。另外 - 我不确定是否有更简单的方法将提及放在首位,我能想到的唯一方法是在提及之前放置一个空格。

elif message.content.startswith('!sayhello'):
    user = message.mentions[0]
    responses = ['' + message.author.name + ' says hello to' + user.mention + '']
    choice = random.choice(responses)
    await client.send_message(message.channel, choice)

【问题讨论】:

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


    【解决方案1】:

    您可以使用User.mention 属性来获取将提及给定用户的字符串。这也适用于message.author。如果您要选择多条消息,它将简化您的代码以选择正确的模板,然后填写提及

    elif message.content.startswith('!sayhello'):
        user = message.mentions[0]
        responses = ["{} says hello to {}", "{} says hi to {}", "{} waves to {}"]
        choice = random.choice(responses)
        choice = choice.format(message.author.mention, user.mention)
        await client.send_message(message.channel, choice)
    

    【讨论】:

    • 非常感谢帕特里克!这效果要好得多。我有时确实会出错,但并非总是如此。 user = message.mentions[0] IndexError: list index out of range
    • 如果message 不包含任何提及,就会发生这种情况。
    猜你喜欢
    • 2021-02-19
    • 2017-10-13
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2019-04-01
    • 2021-11-25
    相关资源
    最近更新 更多