Discord Bot树命令未更新问题求助(已配置Guild ID)
Discord Bot Tree命令无法同步更新的问题解决
问题描述
开发Discord Bot时,Tree斜杠命令无法随代码修改更新。已添加Guild ID并尝试每次启动实例时同步,但新增命令、命令参数均未在服务器的Bot集成中显示。当前Discord服务器中仅能看到部分旧命令,新增的add_group_event、remove_group_event等命令完全不显示,部分已存在的命令参数也未同步。
运行日志
2023-10-20 14:21:24 INFO discord.client logging in using static token 2023-10-20 14:21:25 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: eb846f09f3692f1f936ecb88b8d3e3ca). MeetupMentor#1586 has connected to Discord!
核心代码(主文件)
import discord from discord import app_commands from discord.ext import commands import config import os import events import groups import members TOKEN = config.bot_token client = commands.Bot(command_prefix="!", intents=discord.Intents.all()) all_users = [] all_groups = [] client.synced = False client.added = False @client.event async def on_ready(): await client.wait_until_ready() if not client.synced: await client.tree.sync(guild=discord.Object(id=1143632695503638528)) client.synced = True print(f'{client.user} has connected to Discord!') @client.tree.command(name="create_member", description="Creates Member") async def create_member(interaction: discord.Interaction): user = interaction.user.name newM = members.Member(user) all_users.append(newM) await interaction.response.send_message(f'"{user}" has been initialized') @client.tree.command(name="create_group", description="Create a group with the user as the only member ") async def create_group(interaction: discord.Interaction, GroupName: str): user = interaction.user.name mem = None for people in all_users: if people.name == user: mem = people break if mem is None: await interaction.response.send_message(f'"{user}" not initialized') return newG = groups.Group(GroupName) mem.addGroup(newG) all_groups.append(newG) await interaction.response.send_message(f'New Group Created, "{newG.name}"!') @client.tree.command(name="add_member_group", description="Adding usernames of people to a group") async def add_member_group(interaction: discord.Interaction, GroupName: str, newUser: str): group = None for existing_group in all_groups: if existing_group.name == GroupName: group = existing_group break if group is None: await interaction.response.send_message(f'Group "{GroupName}" not found.') return mem = None for people in all_users: if people.name == newUser: mem = people break if mem is None: await interaction.response.send_message(f'"{newUser}" not initialized') return mem.addGroup(group) await interaction.response.send_message(f'"{newUser}" has been added to "{GroupName}"') @client.tree.command(name="my_groups", description="Return the names of groups you are a part of") async def my_groups(interaction: discord.Interaction): user = interaction.user.name mem = None for people in all_users: if people.name == user: mem = people break if mem is None: await interaction.response.send_message(f'"{user}" not initialized') return groups = [group.name for group in mem.groups] group_info = '\n'.join([f'Group Name: {group}' for group in groups]) await interaction.response.send_message(f'You are a part of the following groups:\n{group_info}') @client.tree.command(name="my_schedule", description="Return your schedule or events in your groups ") async def my_schedule(interaction: discord.Interaction): user = interaction.user.name mem = None for people in all_users: if people.name == user: mem = people break if mem is None: await interaction.response.send_message(f'"{user}" not initialized') return schedule = [] for group in mem.groups: for event in group.events: schedule.append(event.get_event_info()) if not schedule: await interaction.response.send_message('No events in your schedule or groups.') else: schedule_info = '\n'.join(schedule) await interaction.response.send_message(f'Your schedule:\n{schedule_info}') @client.tree.command(name="add_group_event", description="Add event to a group you are part of") async def add_group_event(interaction: discord.Interaction, GroupName: str, EventName: str, year: int, month: int, day: int, start_hour: int, start_minute: int, duration_minutes: int): group = None for existing_group in all_groups: if existing_group.name == GroupName: group = existing_group break if group is None: await interaction.response.send_message(f'Group "{GroupName}" not found.') return new_event = events.Event(EventName, group) new_event.createEvent(year, month, day, start_hour, start_minute, duration_minutes) group.addEvent(new_event) # 补充交互响应,Discord要求必须响应交互 await interaction.response.send_message(f'Event "{EventName}" has been added to group "{GroupName}".') @client.tree.command(name="remove_group_event", description="Remove event from a group you are part of") async def remove_group_event(interaction: discord.Interaction, GroupName: str, EventName: str): group = None for existing_group in all_groups: if existing_group.name == GroupName: group = existing_group break if group is None: await interaction.response.send_message(f'Group "{GroupName}" not found.') return event_to_remove = None for event in group.events: if event.name == EventName: event_to_remove = event break if event_to_remove: group.events.remove(event_to_remove) await interaction.response.send_message(f'Event "{EventName}" removed from group "{GroupName}".') else: await interaction.response.send_message(f'Event "{EventName}" not found in group "{GroupName}".') client.run(TOKEN)
解决步骤
1. 修复命令的交互响应问题
代码中add_group_event命令缺少await interaction.response.send_message()调用,Discord要求所有斜杠命令必须在3秒内对交互做出响应,否则会被判定为无效命令,无法完成同步。添加响应语句即可解决该问题(已在上述代码示例中修正)。
2. 强制清除旧命令并重新同步
修改on_ready函数,先清除目标服务器的所有命令,再重新同步,确保旧命令被完全覆盖:
@client.event async def on_ready(): await client.wait_until_ready() # 先清除旧命令 await client.tree.clear_commands(guild=discord.Object(id=1143632695503638528)) # 再同步新命令 await client.tree.sync(guild=discord.Object(id=1143632695503638528)) client.synced = True print(f'{client.user} has connected to Discord!')
测试完成后,可恢复if not client.synced:的判断,避免每次启动重复同步。
3. 确认Bot权限
- 确保Bot在目标服务器中拥有
applications.commands权限。可重新生成邀请链接,链接必须包含scope=bot+applications.commands参数,示例:https://discord.com/oauth2/authorize?client_id=YOUR_BOT_ID&permissions=8&scope=bot+applications.commands - 检查服务器的Bot角色权限,确保未禁用任何相关权限。
4. 验证Guild ID正确性
开启Discord开发者模式(用户设置→高级→开发者模式),右键服务器图标复制ID,确认代码中的1143632695503638528与实际服务器ID一致。
5. 等待Discord缓存更新
同步命令后,Discord服务器缓存可能需要1-5分钟才能更新完成,耐心等待后再检查命令列表。
6. 检查开发者门户设置
- 登录Discord开发者门户,进入Bot页面,确保所有必要的Intents已开启(即使代码中使用
Intents.all(),也需确认门户中对应开关已打开)。 - 确认Bot的
Public Bot设置符合需求,若设为私有,需确保目标服务器已在授权列表中。
内容的提问来源于stack exchange,提问作者Nabil Collison-Cofie
相关产品推荐
相关产品推荐

