Search code examples
discorddiscord.py

discord.py - Problem making a context menu (apps)


I am currently having a problem while creating context menus using discord.py I mean this

This is the code I currently have

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)

When I ran the the code no error was given, but also no context menu was able to be selected.

After that did not work, I tried it with a "commands.Bot" client

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)

But here there is the same problem.

I do not want the "commands.Bot" code to work, but rather the top one


Solution

  • You created your context menu as global (you didn't specify a guild for it). However, when synchronizing your tree, you are specifying a guild. That is, only commands created for that guild are being synchronized. Faced with this coherence error, you have two options:

    • Set your menu context as belonging to the guild guild_id1;
    • Sync your global commands.

    Adopting the first solution:

    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)