discord.py机器人执行斜杠命令创建语音频道后无法回复消息的问题求助
discord.py机器人执行斜杠命令创建语音频道后无法回复消息的问题求助
我用discord.py写了一个Python的Discord机器人,想要实现执行命令时创建一个自定义名称的语音频道。现在频道能成功创建,但机器人不会回复消息,还显示“The application did not respond”。
以下是机器人的代码:
import discord from discord.ext import commands bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) @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 create_channel(ctx, channel_name: str): guild = ctx.guild existing_channel = discord.utils.get(guild.channels, name=f"🎤 | {channel_name}") if existing_channel: await ctx.send(f"Il canale '{channel_name}' esiste già.") else: category_name = "Live" # Sostituisci con il nome della categoria desiderata # Trova la categoria per nome category = discord.utils.get(guild.categories, name=category_name) if category: member_role = discord.utils.get(guild.roles, name="👤︱Member") # Sostituisci con il nome del ruolo "👤︱Member" personalizzato if member_role: overwrites = { guild.default_role: discord.PermissionOverwrite(connect=False), # Impedisce ai membri di connettersi member_role: discord.PermissionOverwrite(connect=False), # Impedisce ai membri di connettersi guild.get_role(id_1): discord.PermissionOverwrite(connect=True), # Ruolo 1: 🛡️︱Moderator guild.get_role(id_2): discord.PermissionOverwrite(connect=True), # Ruolo 2: 🛡️︱Trial Moderator guild.get_role(id_3): discord.PermissionOverwrite(connect=True), # Ruolo 3: 🔧︱Helper guild.get_role(id_4): discord.PermissionOverwrite(connect=True), # Ruolo 4: 🎥︱Content Creator } channel = await guild.create_voice_channel(f"🎤 | {channel_name}", category=category, overwrites=overwrites) await ctx.send(f"Il canale vocale '{channel.mention}' è stato creato nella categoria '{category_name}'.") else: await ctx.send(f"Il ruolo '👤︱Member' non esiste.") else: await ctx.send(f"La categoria '{category_name}' non esiste.") bot.run('TOKEN')
终端里的错误代码如下:
2023-06-10 21:56:03 ERROR discord.app_commands.tree Ignoring exception in command 'hello' Traceback (most recent call last): File "C:\Users\ACER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 842, in _do_call return await self._callback(interaction, **params) # type: ignore File "C:\Users\ACER\Desktop\bot ds\main.py", line 42, in create_channel await ctx.send(f"Il canale vocale '{channel.mention}' è stato creato nella categoria '{category_name}'.") AttributeError: 'Interaction' object has no attribute 'send' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\ACER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\tree.py", line 1248, in _call await command._invoke_with_namespace(interaction, namespace) File "C:\Users\ACER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 867, in _invoke_with_namespace return await self._do_call(interaction, transformed_values) File "C:\Users\ACER\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app_commands\commands.py", line 860, in _do_call raise CommandInvokeError(self, e) from e discord.app_commands.errors.CommandInvokeError: Command 'hello' raised an exception: AttributeError: 'Interaction' object has no attribute 'send'
我试过用不同的方法,比如把ctx.send替换成interaction.response.send_message相关的写法,但还是没解决问题。
备注:内容来源于stack exchange,提问作者Andrearoma2
相关产品推荐
相关产品推荐

