当我尝试使用以下代码运行命令 $getUserId @user
并尝试打印它时,我将@mention 用户名作为字符串而不是用户 ID 获得:
from discord.ext import commands
client = commands.Bot(command_prefix = "$")
@client.command()
async def getUserId(ctx, *arg):
if not arg:
userId = ctx.author.id
else:
userId = arg.id
await ctx.send(userId)
在不使用 on_message(message)
的情况下,我找不到关于从给定提及中查找 id 的好的文档。我很幸运使用 async def getUserId(ctx, user: Discord.user)
并导入了 discord 本身,但是当我运行 $getUserId
我将如何处理这个问题:将提到的用户作为参数并在给出 $getUserId
命令时处理所有异常
?
答案 0 :(得分:1)
你可以做这样的事情:
@bot.command()
async def get_userId(ctx, user: discord.User):
await ctx.send(user.id)
首先你需要提到你的用户 或者你可以这样
@bot.command()
async def get_userId(ctx, user:discord.User):
user_id = user.id
get_user_id = bot.get_user(user_id)
await ctx.send(get_user_id)