Discord Slash Commands

Question:
Error trying slash commands

Repl link:
https://replit.com/@nullyih/MoonNodesBot#main.py

import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
from replit import db

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)
slash = SlashCommand(bot, sync_commands=True)

# ID del canal donde se mostrará el estado de los nodos
status_channel_id = 8346834634

# Estados posibles para los nodos
node_states = {
    1: "🟢 - Activo",
    2: "🟡 - Mantenimiento",
    3: "🔴 - Nodo Apagado"
}

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

@slash.slash(
    name="changestatus",
    description="Cambia el estado de un nodo",
    options=[
        create_option(
            name="node",
            description="Número del nodo",
            option_type=4,  # Integer type
            required=True,
            choices=[
                create_option(
                    name=str(node),
                    value=node,
                )
                for node in node_states.keys()
            ]
        ),
        create_option(
            name="new_status",
            description="Nuevo estado del nodo",
            option_type=4,  # Integer type
            required=True,
            choices=[
                create_option(
                    name=str(state),
                    value=state,
                )
                for state in node_states.values()
            ]
        )
    ]
)
async def change_status(ctx: SlashContext, node: int, new_status: int):
    global status_channel_id

    if node in node_states and new_status in node_states:
        db[f"node_{node}_status"] = new_status
        
        status_channel = bot.get_channel(status_channel_id)
        status_message = await status_channel.fetch_message(status_channel.last_message_id)
        
        # Actualizar el mensaje en el canal de estado
        embed = discord.Embed(
            title="Moon Hosting - Estado de los Nodos",
            description="Estado actual de los nodos:",
            colour=0x04f500
        )
        
        for node, state in node_states.items():
            embed.add_field(name=f"Node {node}", value=state, inline=False)
        
        embed.set_footer(text="Bot by alte87 • Hecho para Moon Hosting")

        if status_message:
            await status_message.edit(embed=embed)
        else:
            status_message = await status_channel.send(embed=embed)
            status_channel_id = status_channel.id  # Actualizar el ID del canal en caso de que el mensaje sea nuevo
            
        await ctx.send(f"Estado del Nodo {node} cambiado a {node_states[new_status]} y actualizado en el canal designado.")
    else:
        await ctx.send("Nodo o estado inválido")

bot.run('TOKEN')

Can anyone help me?
idk how to create slassh commands, i just started discord py i used discordjs

Hello @nullyih and welcome to the community!

I’ll be listing some things I noticed feel free to ask any questions.

First of all:
You should hide your channel_id and Token with Secrets function in replit.
See more here: https://docs.replit.com/programming-ide/workspace-features/secrets

Now about your code:

  1. You are using create_option but it’s not imported in your code. You can fix this by adding to your imports
from discord_slash.utils.manage_commands import create_option
  1. You’re using node_states.values() for the new_status option’s choices. The new_status choices should be node_states.keys() because the values are strings like “:green_circle: - Activo” and you’re expecting an integer.

So change this:

create_option(
    name="new_status",
    description="Nuevo estado del nodo",
    option_type=4,  # Integer type
    required=True,
    choices=[
        create_option(
            name=str(state),
            value=state,
        )
        for state in node_states.values()
    ]
)

To this:

create_option(
    name="new_status",
    description="Nuevo estado del nodo",
    option_type=4,  # Integer type
    required=True,
    choices=[
        create_option(
            name=node_states[state],  # change this too
            value=state,
        )
        for state in node_states.keys() #changed here
    ]
)
  1. Last thing is about intents, be sure to check what intent is need for your bot.
    There’s a good guide here about which intents you should be enabling:
    A Primer to Gateway Intents
1 Like

but i need to update the discord.ext part or not?

No need. Why? Is something happening?

yeah, says that is no module named discord.ext

See if the discord.py package is installed

Where i can send you a invitation repl.

Just invite the user “WindLother” to your repl


your error is about discord_slash. From what I can see discord_slash was renamed to interactions
https://interactions-py.github.io/interactions.py/

2 Likes

Totally forgot about the interactions and I said this like last month :man_facepalming:

Hello @nullyih. What you can do is check in the packages section of the repl to see if discord.py is installed. You can search through the packages to find discord.py and maybe discord.ext. I have never used discord.ext but I do have a functional discord bot of my own.

Also at the end you have it running your token without a secret. What you may want to do is put it in a secret unless this is a private repl and also if you have already put the token in a public repl like that you may want to reset your token.

I hope this all helps!
-MilesWK