这是我目前拥有的代码
import discord
from discord import app_commands
with open("token.txt") as file:
token = file.read().strip()
class aclient(discord.Client):
def __init__(self):
super().__init__(intents = discord.Intents.all())
self.synced = False # Only Sync once
async def on_ready(self):
await self.wait_until_ready()
if not self.synced:
guild_id1 = 1129737425653071992
await tree1.sync(guild = discord.Object(id=guild_id1))
self.synced = True
print(f"Logged in as {self.user}.")
client = aclient()
tree1 = app_commands.CommandTree(client) # Define Tree 1
@tree1.context_menu(name="name")
async def react(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message('Very cool message!', ephemeral=True)
client.run(token)
当我运行代码时,没有给出错误,但也无法选择上下文菜单。
之后不起作用,我用“commands.Bot”客户端尝试了
import discord
from discord import app_commands
from discord.ext import commands
with open("token.txt") as file:
token = file.read().strip()
client = commands.Bot(command_prefix="!", intents = discord.Intents.all())
@client.event
async def on_ready():
await client.tree.sync()
print("Synced")
@client.tree.context_menu(name="Repeat!")
async def repeat(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message("A message", ephemeral=True)
client.run(token)
但是这里也有同样的问题。
我不希望“commands.Bot”代码起作用,而是想让最上面的代码起作用
您将上下文菜单创建为全局(您没有为其指定行会)。然而,当同步你的树时,你指定了一个行会。也就是说,仅同步为该公会创建的命令。 面对这种一致性错误,你有两个选择:
guild_id1
;采用第一种方案:
import discord
from discord import app_commands
my_guild = discord.Object(id=1129737425653071992)
with open("token.txt") as file:
token = file.read().strip()
class aclient(discord.Client):
def __init__(self):
super().__init__(intents = discord.Intents.all())
self.synced = False # Only Sync once
async def on_ready(self):
if not self.synced:
await tree1.sync(guild=my_guild)
self.synced = True
print(f"Logged in as {self.user}.")
client = aclient()
tree1 = app_commands.CommandTree(client) # Define Tree 1
@tree1.context_menu(name="name")
@app_commands.guilds(my_guild)
async def react(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message('Very cool message!', ephemeral=True)
client.run(token)