通过Discord.PY提及用户和自我 [英] Mentioning Users and Self with Discord.PY

查看:82
本文介绍了通过Discord.PY提及用户和自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码仍然很陌生,使此命令生效的唯一方法是按照下面的步骤进行操作。我敢肯定,有一种更好的编写方法,但是无论如何,这也不完全是我想要的。

I'm still really new to coding and the only way I can get this command to work is by doing what I'm doing below. I'm pretty sure there's a better way to write it, but it's not exactly how I want it anyway.

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

I'm trying to mention the user who uses the command at the beginning and then mention the user that's mentioned within the message. message.author.name is only returning the name of instead of the actual tag of the user that uses the command. Also - I'm not sure if there's an easier way to put the mention first, the only way I could think of doing it is to put a blank space before the mention.

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)


推荐答案

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

You can use the User.mention attribute to get a string that will mention the given user. This applies to the message.author as well. If you have multiple messages you're choosing from, it will simplify your code to select the correct template and then fill in the mentions

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)

这篇关于通过Discord.PY提及用户和自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆