英文:
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
的基类 BotBase
和 Client
都没有 id
属性。
BotBase
分别提供了 owner_id
和 owner_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论