获取已标记用户的用户ID

2024-04-24 03:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用discordpy构建一个discord机器人。我希望它具有的一个特性是,能够给出用户的随机百分比

例如:

用户1:!w@user2

bot:@user2是x%y

我不确定如何从user1发送的消息中提取user2的userid。有人有什么建议吗


Tags: 用户消息bot机器人特性建议百分比userid
3条回答

您在标题中提出了一个问题,但描述与之不匹配;-

下面是一个基本代码,根据您在说明中的要求,可以根据需要进行自定义:)

确保将“客户机”替换为您使用过的任何东西

@client.command()
async def cool(ctx, user: discord.Member = None):
    pctg = (random.randint(0, 100))
    if user == None:
        await ctx.send('Mention a user')
    else:
        await ctx.send(f"{user.name} is {pctg}% cool")

如果您希望上述用户的ID作为bot的回复,请使用以下命令-

@client.command()
async def uid(ctx, user: discord.Member):
    uid = user.id
    await ctx.send(uid)

enter image description here

enter image description here

您可以使用^{}^{} attribute

示例:

@bot.command()
def w(ctx, user: discord.Member):
    user_id = user.id
    print(user_id)

@client.event
async def on_message(message):
    if message.content.startswith('w!cool '):
        if message.mentions == []:
            await message.channel.send("You should mention someone!\n```\nw!cool <mention>\n```")
            return
        coolness_tosend=""
        for i in message.mentions:
            coolness_tosend=coolness_tosend+"<@" + str(i.id) + "> is " + str(random.choice(range(100))) + "\% cool.\n"
        await message.channel.send(coolness_tosend)

链接:

discord.Message.mentions in API reference

discord.Message in API reference

不过,请注意,没有参数的w!cool单独使用是行不通的。它可以防止与w!coolpic等命令发生冲突。别忘了做一个import random

相关问题 更多 >