GithubHelp home page GithubHelp logo

Error when I start the server about dislash.py HOT 6 OPEN

equenos avatar equenos commented on September 4, 2024
Error when I start the server

from dislash.py.

Comments (6)

MisileLab avatar MisileLab commented on September 4, 2024

Can you give me full code?

from dislash.py.

YoanGab avatar YoanGab commented on September 4, 2024

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)

from dislash.py.

EQUENOS avatar EQUENOS commented on September 4, 2024

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

from dislash.py.

YoanGab avatar YoanGab commented on September 4, 2024

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

from dislash.py.

EQUENOS avatar EQUENOS commented on September 4, 2024

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

from dislash.py.

YoanGab avatar YoanGab commented on September 4, 2024

What I don't understand is that it doesn't happen when I use @discord.commandinstead of @slash.command

from dislash.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.