如何用Python Discord机器人通过斜杠指令添加角色?报错排查
Discord机器人添加角色报错解决方案
我是Python完全新手,正在为Discord服务器编写机器人,尝试给消息作者添加角色(后续会改为指定用户)。机器人启动正常,但执行/hello指令时触发错误。
原代码
import discord from discord import app_commands, message, guild, context_managers, interactions, member, role from discord.ext import commands from discord.utils import get intents = discord.Intents.all() bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print("Bot is up and ready!") try: synced = await bot.tree.sync() print(f"Synced {len(synced)} command(s)") except Exception as e: print(e) @bot.tree.command(name="hello") async def hello(interaction: guild.Member.roles): __member__ = message.Member __Guild__ = discord.Guild role = discord.utils.get(message.Guild.roles, id="1235347026967789618") await interaction.member.add_roles(role) bot.run("token")
报错信息(翻译后)
[2024-05-01 20:18:35] [ERROR ] discord.app_commands.tree: 忽略命令'hello'中的异常 回溯(最近的调用最后): 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\app_commands\commands.py", 第828行, 在 _do_call中 return await self._callback(interaction, **params) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\myName\PycharmProjects\discordBot\main.py", 第23行, 在hello中 role = discord.utils.get(message.Guild.roles, id="1235347026967789618") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\utils.py", 第595行, 在get中 else _get(iterable, **attrs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\utils.py", 第492行, 在_get中 return next((elem for elem in iterable if pred(elem) == v), None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'property'对象不可迭代 上述异常直接导致以下异常发生: 回溯(最近的调用最后): 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\app_commands\tree.py", 第1248行, 在_call中 await command._invoke_with_namespace(interaction, namespace) 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\app_commands\commands.py", 第853行, 在_invoke_with_namespace中 return await self._do_call(interaction, transformed_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件 "C:\Users\myName\PycharmProjects\discordBot\.venv\Lib\site-packages\discord\app_commands\commands.py", 第842行, 在_do_call中 raise CommandInvokeError(self, e) from e discord.app_commands.errors.CommandInvokeError: 命令'hello'触发异常: TypeError: 'property'对象不可迭代
错误原因分析
- 函数参数类型错误:
hello函数的参数应该是discord.Interaction类型,而不是guild.Member.roles,后者是类的属性而非交互对象类型。 - 混淆类与实例:
message.Member、discord.Guild是Discord库中的类,不是当前交互对应的成员或服务器实例。应该从interaction对象中获取当前成员(interaction.member)和服务器(interaction.guild)。 - 角色ID类型错误:角色ID必须是整数,不能用字符串传递给
discord.utils.get。
修正后的代码
import discord from discord import app_commands from discord.ext import commands from discord.utils import get intents = discord.Intents.all() bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print("Bot is up and ready!") try: synced = await bot.tree.sync() print(f"Synced {len(synced)} command(s)") except Exception as e: print(e) @bot.tree.command(name="hello") async def hello(interaction: discord.Interaction): # 从交互对象中获取当前服务器和成员 guild = interaction.guild member = interaction.member # 角色ID必须是整数 role = get(guild.roles, id=1235347026967789618) if role and member: await member.add_roles(role) await interaction.response.send_message("角色已添加!", ephemeral=True) else: await interaction.response.send_message("无法找到角色或成员!", ephemeral=True) bot.run("你的机器人令牌")
修改说明
- 修正了
hello函数的参数类型为discord.Interaction,这是Slash命令的标准参数类型。 - 通过
interaction.guild和interaction.member获取当前的服务器和成员实例,而非直接使用类。 - 将角色ID改为整数类型,符合Discord库的要求。
- 添加了基础的错误判断,避免因角色不存在或成员无效导致的崩溃,并通过
ephemeral=True发送仅用户可见的回复。
内容的提问来源于stack exchange,提问作者Jinsoku Okami
相关产品推荐
相关产品推荐

