#๐Ÿ”’ Discord Bot command help

107 messages ยท Page 1 of 1 (latest)

uneven ore
#

Hi,
I'm trying to create a discord bot slash command.
I know how to create the commands themselves, and add parameters to them, but I'm trying to make optional parameters, and if the option isn't selected, for it to spit out a default response.

Ex:

@bot.tree.command(name='CommandName', description='Command Description.')
@app_commands.describe(Option = 'This is an option for the command.')
async def CommandName(interaction: discord.Interaction, Option: str):
  await interaction.response.send_message(f'{interaction.user.mention} chose {Option}.')

I have the associated libraries installed for a discord bot, I just posted an example for the command involved.

In the "Option" parameter I've created, I tried adding py required = False but I've gotten an error saying I've added an unknown parameter.

round scaffoldBOT
#

@uneven ore

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fresh totem
#

Can you give me the docs to their class?

uneven ore
#

I'm not entirely sure how to do that -- I haven't gotten to understanding how to make classes on my own

#

I'm also RDing to the computer my code is on, and I think the internet there is freaking out, since it just kicked me out of it and not letting me back in, yet

fresh totem
#

I meant to discordpy's class, I'm finding their docs hard to navigate

uneven ore
#

Their docs are kind of stupid

weak crag
#

Pretty sure this is discord-py-interactions, not discord.py

uneven ore
#

I'll tell you the things I've imported in a second

weak crag
fresh totem
#

Basically what I want to see is how they're explaining the Option param

#

Usually I'd just inherit a class and add **kwargs then unpack that

fresh totem
#

Ah, so Option isn't a string

#

It's a class

#

ahh i see

#

Why on earth would anyone want to do it like this?

#

I meant interactions-py, not you

uneven ore
#
import discord
from discord import app_commands
from discord.ext import commands, tasks
from datetime import datetime

import pytz
import asyncio
#

I have these imported

#

Let me get a screenshot of what I have for the bot

fresh totem
#

so, you're not using interactions-py?

#

This looks like you've tried to set up your own code in a similar style to interactions-py library

uneven ore
#

I am not using interactions-py no

fresh totem
#

Where did you learn to do this:

@bot.tree.command(name='CommandName', description='Command Description.')
@app_commands.describe(Option = 'This is an option for the command.')
uneven ore
#

I'm trying to make "the_thing" an optional parameter instead of it being a required field to fill

uneven ore
fresh totem
#

Can I see where you've defined "the_thing" and "the funny"?

uneven ore
#
@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: str):
    await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")
#

This is the code for the specific command I'm trying to run

#

I just typed "the funny" as an example of text you can fill in

fresh totem
#

What happens if you try to do this:

#
@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: str | None = None):
    await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")
uneven ore
#

Let me see

fresh totem
#

Do you use typing library or annotations?

uneven ore
#

I've lacked on annotating my code for this project ๐Ÿ˜…

fresh totem
#

I mean, you'll need to do from __future__ import annotations to use typing like that

#

otherwise do from typing import Optional

#

then change str | None = None to Optional[str]

#

err

#

Yeah, docs suggest that discordpy uses typing so use that instead of my initial one

#
from typing import Optional

@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: Optional[str]):
    await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")
uneven ore
#

Noted, okay

fresh totem
#

Try this, let me know if change

uneven ore
#

The str | None = None actually does work

#

Keyboard mucked up, mb

fresh totem
#

You mean it allows the param to be optional?

uneven ore
#

Yes

fresh totem
#

I mean, both should work provided discordpy has been built that way.

I've never used it so I don't know how it works but if it does make the argument optional it is either checking for the typing or it's checking if the parameter is an arg or kwarg

#

either way, glad that solved it for you ๐Ÿ™‚

#

Out of curiousity

#

Does Optional[str] work?

uneven ore
#

I'm giving it a go right now

#

I'll update you in less than one minute :)

#

It doesn't seem like it

fresh totem
#

Okay

uneven ore
#

The parameter is required now

#

But the None = None works

fresh totem
#

So what the library is doing

#

is it's checking if the arg is an argument or a keyword argument

#

if you do Optional[str] = None it will probably work

#

Typically this is best practice

#

Anyway, if you decide to use str | None = None, just make sure to import from __future__ import annotations as the first line of your code. (It needs to be imported before anything else due to how the library works).

uneven ore
#
@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: Optional[str] = "that"):
    await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")

This works here

fresh totem
#

Better that you default it to None

#

and handle the two behaviors seperately

#
@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: Optional[str] = "that"):
    if the_thing not None:
        await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")
    else:
        do something else
#

that kinda thing

#

gotta brb

uneven ore
#

I was trying to make it that if someone didn't input anything into the_thing, it would send out a default response for the_thing
The way I did it, it has the_thing as whatever someone sets it as, and if they don't choose something, it will default to "that"

uneven ore
fresh totem
#

Ah, then yeah do like this:

#
@bot.tree.command(name="coolwagalicioussauce", description="When something is just so good, you need to express it in its entirety.")
@app_commands.describe(the_thing = "This is for what is coolwagalicioussauce.")
async def coolwagalicioussauce(interaction: discord.Interaction, the_thing: Optional[str] = None):
    if the_thing == None:
        the_thing = "that"
    await interaction.response.send_message(f"{interaction.user.mention} thinks {the_thing} is so coolwagalicioussauce!")
#

Ultimately, it doesn't matter how you do it. I'm just suggesting the pep 'best practice' way

#

afk 10

uneven ore
#

I'll do the best practice way, then
Thank you!

#

Yes, that works perfectly! Thank you again for your help!

left mulch
uneven ore
#

I'm not using a cog

And that was just an example thing, I didn't use a capital for my actual code, just the example ๐Ÿ˜…๐Ÿ˜ญ

uneven ore
fresh totem
uneven ore
#
@bot.tree.command(name='CommandName', description='Command Description.')
@app_commands.describe(Option = 'This is an option for the command.')
async def CommandName(interaction: discord.Interaction, Option: str):
  await interaction.response.send_message(f'{interaction.user.mention} chose {Option}.')
#

The Option: str thing

fresh totem
#

Ah, that. Yeah that bit threw me off initially, however it did help me identify that it was the Option class you were using

uneven ore
#

I will hide from the scary angie man

fresh totem
#

One thing you can do probably

#

is something like

stray bay
#

hello

#

whats the situation now?

fresh totem
#
async def CommandName(interaction: discord.Interaction, option: Option.OptionType.string):
#

or whatever

uneven ore
fresh totem
#

if you're using a class that has typevars

stray bay
uneven ore
fresh totem
#

dw about it, it's such a minor thing and a complete tangent

#

Have fun ๐Ÿ™‚

left mulch
left mulch
#

I can send you documentation on it if youโ€™d like

#

And give an example

uneven ore
left mulch
#

also for easier readability as well you can do:

 python = discord.SlashCommandGroup("python", "Commands about Python", guilds_id=[ID])
 @python.command(name="whatever", description="you want")
 async def whatever(ctx):
    await ctx.respond("example")

another way to format your commands for easier use as well

round scaffoldBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.