Discord.py斜杠命令踢人报错:如何将通知发送到指定频道
问题:Discord.py斜杠命令踢人并发送通知到指定频道报错
我在用Discord.py开发Discord机器人,需要实现通过斜杠命令踢出成员,并将User {member.mention} was kicked because {reason}的通知发送到指定频道,而非命令执行的频道。当前使用的代码如下:
@bot.slash_command(description = "Kick someone", guild_ids=[1041057700823449682]) @commands.has_permissions(kick_members=True) @option("member",description = "Select member") @option("reason",description = "Reason for kick (you can leave this empty)") async def kick( ctx, member: discord.Member, channel: bot.get_channel(1042042492020863037), *, reason=None): if reason==None: reason="(no reason)" await ctx.guild.kick(member) await ctx.respond("Done :)") await ctx.channel.send(f'User {member.mention} was kicked because {reason}')
运行代码时出现如下报错:
Traceback (most recent call last): File "c:\Users\fonti\Documents\Projetos Python\Bot do Discord\Iniciar Bot.py", line 152, in <module> async def kick( File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 905, in decorator self.add_application_command(result) File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 127, in add_application_command command._set_cog(None) File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 603, in _set_cog self.cog = cog File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 827, in cog self._validate_parameters() File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 705, in _validate_parameters self.options: list[Option] = self._parse_options(params) File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 745, in _parse_options option = Option(option) File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\options.py", line 210, in __init__ self.input_type = SlashCommandOptionType.from_datatype(input_type) File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\enums.py", line 707, in from_datatype if datatype.__name__ in ["Member", "User"]: AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?
移除channel参数后机器人可正常运行,但通知会发送到命令执行的频道,而非目标指定频道。
错误原因
- 机器人启动阶段尚未完成与Discord服务器的连接,此时调用
bot.get_channel()会返回None,Discord.py在解析命令参数类型时无法识别None,因此抛出AttributeError。 - 将
channel定义为函数参数会被Discord.py当作斜杠命令的可选参数,但你实际需要的是固定发送到指定频道,不需要用户输入。
修正后的代码
@bot.slash_command(description = "Kick someone", guild_ids=[1041057700823449682]) @commands.has_permissions(kick_members=True) @option("member",description = "Select member") @option("reason",description = "Reason for kick (you can leave this empty)") async def kick( ctx, member: discord.Member, *, reason=None): if reason is None: reason = "(no reason)" # 执行踢人操作 await ctx.guild.kick(member) # 仅向命令执行者发送操作完成提示 await ctx.respond("Done :)", ephemeral=True) # 获取指定通知频道 target_channel = bot.get_channel(1042042492020863037) if target_channel: # 发送通知到指定频道 await target_channel.send(f'User {member.mention} was kicked because {reason}') else: # 频道不存在时向执行者提示 await ctx.followup.send("无法找到指定的通知频道,请检查频道ID是否正确", ephemeral=True)
关键说明
- 将获取指定频道的逻辑移到函数内部执行,此时机器人已完成连接,能正确获取频道对象。
- 移除函数参数中的
channel定义,避免被Discord.py当作命令选项解析。 - 使用
ephemeral=True让操作提示仅对命令执行者可见,避免频道刷屏。 - 增加频道不存在的容错处理,防止因频道ID错误导致的异常。
内容的提问来源于stack exchange,提问作者Jack142
相关产品推荐
相关产品推荐

