I cannot for the life of me understand how to get an Interaction (context: for ephemeral response) #9854
-
I am trying to make a discord bot, and i would like to send an ephemeral response to a bot command. I have been trying to figure out how to send an ephemeral message for the last like two days. It seems to require some kind of Decorating code with @bot.command provides a context with None as the interaction. The examples folder was not helpful - the command example uses Client which doesn't handle commands at all. My code is this: import discord
from discord.ext import commands
import requests
TOKEN = 'token'
intents = discord.Intents.default()#(534723950656)
intents.message_content = True
intents.members = True
intents.presences = False
# Create an instance of the bot
bot = commands.Bot(command_prefix='!', intents=intents)
class MyClient(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
# A CommandTree is a special type that holds all the application command
# state required to make it work. This is a separate class because it
# allows all the extra state to be opt-in.
# Whenever you want to work with application commands, your tree is used
# to store and work with them.
# Note: When using commands.Bot instead of discord.Client, the bot will
# maintain its own tree instead.
self.tree = discord.app_commands.CommandTree(self)
client = MyClient(intents=intents)
# Event to perform when the bot is ready to start interacting with the server
@client.event
async def on_ready():
print(f'Logged in as {client.user.name}')
def keyvaluedir(obj: object):
print(f' -- Printing directory of instance of "{type(obj).__name__}" -- \n')
_dir = dir(obj)
for i in _dir:
x = eval(f'obj.{i}', {'obj': obj})
print(f'"{i}": {repr(str(x))}')
# Command to say hello
@bot.command()
async def cat_fact(ctx: commands.context.Context):
#keyvaluedir(commands.context.Context)
response = requests.get('https://catfact.ninja/fact', {'max_length': 256})
if response.status_code == 200:
await ctx.send(response.json()['fact'])
else:
await ctx.send('An error has occured while getting a cat fact!')
@client.tree.command(name='ephemeral')
async def ephemeral(interaction: discord.Interaction):
await interaction.response.send_message(content='yo', ephemeral=True)
@client.tree.command()
async def hello(interaction: discord.Interaction):
"""Says hello!"""
await interaction.response.send_message(f'Hi, {interaction.user.mention}')
# Run the bot
client.run(TOKEN) Once i replaced all commands with @client.tree.command instead of @bot.command it started throwing 'command not found' exceptions the moment i send a command. I tried a suggestion from a discussion on Interactions but it still doesn't consider the commands to be real. intents = discord.Intents.default()#(534723950656)
intents.message_content = True
intents.members = True
intents.presences = False
# Create an instance of the bot
bot = commands.Bot(command_prefix='!', intents=intents)
class MyClient(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
# A CommandTree is a special type that holds all the application command
# state required to make it work. This is a separate class because it
# allows all the extra state to be opt-in.
# Whenever you want to work with application commands, your tree is used
# to store and work with them.
# Note: When using commands.Bot instead of discord.Client, the bot will
# maintain its own tree instead.
self.tree = discord.app_commands.CommandTree(self)
client = MyClient(intents=intents)
# Event to perform when the bot is ready to start interacting with the server
@client.event
async def on_ready():
print(f'Logged in as {client.user.name}')
def keyvaluedir(obj: object):
print(f' -- Printing directory of instance of "{type(obj).__name__}" -- \n')
_dir = dir(obj)
for i in _dir:
x = eval(f'obj.{i}', {'obj': obj})
print(f'"{i}": {repr(str(x))}')
# Command to say hello
@bot.command()
async def cat_fact(ctx: commands.context.Context):
#keyvaluedir(commands.context.Context)
response = requests.get('https://catfact.ninja/fact', {'max_length': 256})
if response.status_code == 200:
await ctx.send(response.json()['fact'])
else:
await ctx.send('An error has occured while getting a cat fact!')
@bot.tree.command(name='ephemeral')
async def ephemeral(interaction: discord.Interaction):
await interaction.response.send_message(content='yo', ephemeral=True)
@bot.tree.command()
async def hello(interaction: discord.Interaction):
"""Says hello!"""
await interaction.response.send_message(f'Hi, {interaction.user.mention}')
# Run the bot
bot.run(TOKEN)``` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hello. Commands added to a To have your bot's commands show up inside Discord when you type the As a starting point you can sync the tree either in your bot's After syncing the tree you (and anyone else) should be able to use the commands. You also created both an instance of your custom |
Beta Was this translation helpful? Give feedback.
Hello. Commands added to a
CommandTree
are different from text-based chat commands in that they use Discord's somewhat-new ability for bots to create slash commands that are more integrated into the app.To have your bot's commands show up inside Discord when you type the
/
(and command name) you will need to sync your tree to Discord after adding commands to it using the CommandTree.sync method. You will need to do this if you add, change, or remove any of your commands.As a starting point you can sync the tree either in your bot's
setup_book
like the example (though this is not really recommended), or create a regular text command that only you as the bot owner can use whenever you hav…