Mentioning Users and Self with Discord.PY

2019-08-23 14: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条回答
我只想做你的唯一
2楼-- · 2019-08-23 15:16

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)
查看更多
登录 后发表回答