Open YoanGab opened 3 years ago
Can you give me full code?
I receive this error only on DMs, the 2nd message to send gives me the error but ctx["channel"]
is None at the beginning of the function.
import os
import discord
from discord.ext import commands
from dislash import SlashClient, SelectMenu, SelectOption, Option, Type
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from orm.tables import Player
from classes.classes import Card
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD_ID = int(os.getenv('GUILD_ID'))
CONNECT_STRING = os.getenv('CONNECT_STRING')
engine = create_engine(CONNECT_STRING)
Session = sessionmaker(bind=engine)
session = Session()
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='!', intents=intents)
slash = SlashClient(bot)
@slash.command(
name="get_player",
options=[
Option("name", "Specify any player", Type.STRING, required=True)
]
)
async def get_player(ctx, name):
players = session.query(Player).filter(Player.name.like("%" + name + "%")).limit(5).all()
msgs = []
if len(players) == 0:
await ctx.send(embed=discord.Embed(
title="No player with name"
))
return
elif len(players) == 1:
chosen_card = Card(name=players[0].name, slug=players[0].slug)
else:
for player in players:
emb = discord.Embed(
title=f"{player.name}",
color=ctx.author.color
)
card = Card(name=player.name, slug=player.slug)
if card.image:
emb.set_image(url=card.image)
# Error here when sending the 2nd embed
msgs.append(await ctx.reply(embed=emb))
msg = await ctx.send(
"Choose a player",
components=[
SelectMenu(
custom_id="choose_player",
placeholder="Choose a player",
max_values=1,
options=[
SelectOption(player.name, player.slug)
for player in players
]
)
]
)
def check(inter):
return ctx.author == inter.author
# Wait for a menu click under the message you've just sent
inter = await msg.wait_for_dropdown(check)
# Tell which options you received
option = inter.select_menu.selected_options[0]
for message in msgs:
await message.delete()
await msg.delete()
chosen_card = Card(name=option.label, slug=option.value)
embed = discord.Embed(
title=f"Chosen card is {chosen_card.name}"
)
embed.set_image(url=chosen_card.image)
await ctx.send(embed=embed)
await ctx.send(embed=discord.Embed(
title="Error here when len(players) == 1"
))
bot.run(TOKEN)
Okay, here's the problem:
ctx.reply
(as well as ctx.send
(alias for reply
), ctx.respond
and ctx.create_response
(alias for respond
)) can never be called more than once, because these methods create a response for a given interaction. One interaction can only have one response.
Instead of attempting to create multiple responses, you can use ctx.channel.send
, which can be used an unlimited amount of times
I tried that but it doesn't even send 1 message, because ctx.channel is None. Moreover, ctx.send works when it's not the 1st time that someone interacts with the bot
ctx.channel being None is super weird, maybe it's related to some caching reasons
Speaking of ctx.send working more than once, it indeed should work like that, because it automatically detects whether to use create_response
or channel.send
, I forgot to mention that
What I don't understand is that it doesn't happen when I use @discord.command
instead of @slash.command
I get this error when I start the server. The 1st request doesn't work. When I print the ctx, the channel is None for the 1st request but isn't for the next ones. Then I have this error when I use :
await ctx.send(...)
I tried with bot.command and it works
But not when I use slash.command