我如何在discord.py中提及使用用户ID的用户

时间:2017-05-14 13:16:17

标签: python discord.py

我正在尝试使用discord.py编写一个简单的机器人,所以我开始使用有趣的命令,只是为了获得api的悬念

import discord
import asyncio
client = discord.Client()


@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!hug'):
        await client.send_message(message.channel, "hugs {0.author.mention}".format(message))

    # Greetings
    if message.content.startswith('hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await client.send_message(message.channel, msg)        

    # say (id) is the best
    # This is where I am lost. how to mention someone's name or id ?
    if message.content.startswith('!best'):
        mid = User.id('ZERO#6885').format(message)
        await client.send_message(message.channel, '{mid} mentioned')

6 个答案:

答案 0 :(得分:13)

所以我终于想出了如何在经过几天的试验和错误之后做到这一点,希望其他人能从中受益并且比我实际上有更少的痛苦。 解决方案最终很容易..

  if message.content.startswith('!best'):
        myid = '<@201909896357216256>'
        await client.send_message(message.channel, ' : %s is the best ' % myid)

答案 1 :(得分:3)

User对象中,使用属性User.mention获取表示用户提及的字符串。要从其ID获取用户对象,您需要Client.get_user_info(id)。要从用户名(&#39; ZERO&#39;)和鉴别符(&#39;#6885&#39;)中获取用户,请使用效用函数discord.utils.get(iterable, **attrs)。在上下文中:

if message.content.startswith('!best'):
    user = discord.utils.get(message.server.members, name = 'ZERO', discriminator = 6885)
    # user = client.get_user_info(id) is used to get User from ID, but OP doesn't need that
    await client.send_message(message.channel, user.mention + ' mentioned')

答案 2 :(得分:2)

如果您正在处理命令,最好使用discord.py的内置命令功能,您的拥抱命令将变为:

import discord
from discord.ext import commands

@commands.command(pass_context=True)
async def hug(self, ctx):
    await self.bot.say("hugs {}".format(ctx.message.author.mention()))

这假设你在代码的开头做了类似的事情:

def __init__(self):
    self.bot = discord.Client(#blah)

答案 3 :(得分:2)

如果您只想响应on_message回调,则可以像这样从作者那里获取提及字符串:

@bot.event
async def on_message(message):
    # No infinite bot loops
    if message.author == bot.user or message.author.bot:
        return

    mention = message.author.mention
    response = f"hey {mention}, you're great!"
    await message.channel.send(response)

答案 4 :(得分:1)

要提及用户并显示其用户名(而不是他们的ID),您需要在接受的自答案中添加!

await client.send_message(message, '<@!20190989635716256>, hi!')

当你只有id而不是Member或User对象时,我建议不要使用Peter G的答案,他们使用get()get_user_info(id)来实际获取User / Member对象。这些操作非常耗时且不需要,因为.mention只返回这个字符串。

答案 5 :(得分:0)

虽然OP的问题已得到长期解决(并且很可能已被遗忘)-如果您正在使用Python构建Discord机器人,但是找到此信息仍然有点困难-希望这会对某人有所帮助。 如果您尝试使用@ bot.command方法-以下将适用(在python3中):

@bot.command(name='ping', help='Ping the bot to text name')
async def ping(ctx):
    await ctx.send('Pong ' + format(ctx.author))
    print("debug: " + dir(ctx.author))

如果要显示“作者”(称为命令)的昵称,则可以改用此名称”:

@bot.command(name='ping', help='Ping the bot to text name')
async def ping(ctx):
    # await ctx.send('Pong {0}'.format(ctx.author))
    await ctx.send('Pong ' + format(ctx.author.display_name))
    print("debug: " + dir(ctx.author))

另一个有用的提示: 您可以使用dir(ctx.author)查看ctx.author 对象属性