Mentioning Users and Self with Discord.PY

若如初见. 提交于 2020-01-06 05:07:57

问题


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.

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)

回答1:


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)


来源:https://stackoverflow.com/questions/51905563/mentioning-users-and-self-with-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!