使用discord.py的Discord机器人执行斜杠命令时出现Unknown Integration错误
Discord斜杠命令执行时显示"Unknown Integration"错误,无终端报错且数据库未写入数据
环境:
- Python 3.11
- discord.py 框架
- PostgreSQL 数据库
需求:通过斜杠命令获取频道信息并存储到数据库中。已设置斜杠为命令前缀,Discord中命令可自动补全,但执行命令时,Discord聊天框显示Unknown Integration错误,VSCode终端无任何报错信息,同时数据库中也没有存入对应数据。
以下是获取频道信息并写入数据库的代码:
async def set_command_channel(interaction, command: str): connection = DB_Connection() conn = connection.db_connect() if conn is None: message = await interaction.response.send_message("❌ Failed to connect to the database.") return False, message try: cursor = conn.cursor() # Fetch the internal server ID fetch_server_sql = "SELECT id FROM server WHERE guild_id = %s;" cursor.execute(fetch_server_sql, (interaction.guild.id,)) server_result = cursor.fetchone() if server_result is None: message = await interaction.response.send_message("❌ This server is not registered in the database.") return False, message # Explanation of `server_id = server_result[0]` # `cursor.fetchone()` returns a tuple, like (12,) — the first item is the internal 'id' server_id = server_result[0] insert_sql = ''' INSERT INTO channel (channel_id, name, command, activator, join_date, server_id) VALUES (%s, %s, %s, %s, NOW(), %s) ON CONFLICT (channel_id) DO UPDATE SET name = EXCLUDED.name, command = EXCLUDED.command, activator = EXCLUDED.activator, join_date = EXCLUDED.join_date, remove_date = NULL, server_id = EXCLUDED.server_id; ''' cursor.execute( insert_sql, ( interaction.channel.id, interaction.channel.name, command, interaction.user.id, server_id ) ) conn.commit() message = await interaction.response.send_message(f"✅ Channel registered for `{command}` command!") return True, message except Exception as e: await interaction.response.send_message(f"❌ Database error: {e}") finally: cursor.close() conn.close() # AI news channel setup. @clients.tree.command(name="news", description="Get the latest AI news!") async def set_news_channel(interaction): if interaction.response.is_done(): # Already responded somehow (possible duplicate trigger) return try: await interaction.response.defer(thinking=True) success, message = await set_command_channel(interaction, 'news') # Safe follow-up only if success: await interaction.followup.send("📡 Got it! You'll receive AI updates in this channel.") else: await interaction.followup.send(f"❌ {message}") except Exception as e: # Just in case there's an unexpected failure if not interaction.response.is_done(): await interaction.response.send_message(f"⚠️ Error: {e}", ephemeral=True) else: await interaction.followup.send(f"⚠️ Error: {e}", ephemeral=True)
set_command_channel()协程用于其他需要配置频道并存储信息的方法,目前无法定位问题根源,希望得到帮助。
内容的提问来源于stack exchange,提问作者lony235
相关产品推荐
相关产品推荐

