Discord.py:如何获取“Bot”对象的User_ID?

huangapple go评论76阅读模式
英文:

Discord.py: How do I get the User_ID of a "Bot" object?

问题

我正在尝试使用 discord.py 2.0.0 制作一个 Discord 机器人。我想创建一个斜杠命令,以返回Bot对象的用户ID。下面是我的代码的简化版本:

import discord
from discord.ext.commands import Bot
intents = discord.Intents.all()
client = Bot(command_prefix='?', intents=intents)
tree = client.tree
@tree.command(name="help", description="Help command")
async def help(interaction: discord.Interaction):
    await interaction.response.send_message(f"Hello, I am {str(client.id)}")

token = "My Token"
client.run(token)

我尝试使用 Bot.id 属性来访问机器人的用户ID。然而,我收到了以下错误,指出该属性不存在:

Traceback (most recent call last):
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 851, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
  File "C:\discordbot\bot.py", line 8, in help
    await interaction.response.send_message(f"Hello, I am {str(client.id)}")
AttributeError: 'Bot' object has no attribute 'id'

上述异常是以下异常的直接原因

Traceback (most recent call last):
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\tree.py", line 1240, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 876, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 869, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'help' raised an exception: AttributeError: 'Bot' object has no attribute 'id'

有人可以帮我吗?谢谢!

英文:

I am trying to make a discord bot using discord.py 2.0.0. I want to make a slash command that returns the user id of a Bot object, below is a simplified version of my code:

import discord
from discord.ext.commands import Bot
intents = discord.Intents.all()
client = Bot(command_prefix='?', intents=intents)
tree = client.tree
@tree.command(name="help", description="Help command")
async def help(interaction: discord.Interaction):
    await interaction.response.send_message(f"Hello, I am {str(client.id)}")

token = "My Token"
client.run(token)

I tried to access the bot's user id by using the Bot.id attribute. However, I got the following error that the attribute does not exist:

Traceback (most recent call last):
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 851, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
  File "C:\discordbot\bot.py", line 8, in help
    await interaction.response.send_message(f"Hello, I am {str(client.id)}")
AttributeError: 'Bot' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\tree.py", line 1240, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 876, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "C:\Users\trnt\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 869, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'help' raised an exception: AttributeError: 'Bot' object has no attribute 'id'

Can someone please help me? Thank you!

答案1

得分: 2

根据文档和代码,Bot 的基类 BotBaseClient 都没有 id 属性。

BotBase 分别提供了 owner_idowner_ids 来获取拥有用户的 ID。

id 属性可以在 user 属性上找到(代码)以及用户名:

import discord
import asyncio

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run('token')
英文:

According to the documentation and code neither of Bot's base classes, BotBase and Client have an id attribute.

BotBase provides owner_id and owner_ids respectively to get the id of the owning user.

The id attribute can be found on the user attribute (code) along with the name:

import discord
import asyncio

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run('token')

答案2

得分: 0

根据版本2.0中的discord.ext.commands.Bot文档,您需要使用client.application_id,这是机器人的属性。

英文:

Following the Documentation for discord.ext.commands.Bot in version 2.0. you need to use client.application_id which is a property of the bot.

huangapple
  • 本文由 发表于 2023年6月18日 22:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定