#ctx.message with slash command returns None. How can I get parse extra info into my app?

1 messages · Page 1 of 1 (latest)

unique moat
#

So i'm trying to make a /setup (channel1) (channel2) command. But when I run the code ctx.message returns None.
Here's my code:

import random, SendEmail, json, discord
from discord.ext import commands
from pprint import pprint

Intents = discord.Intents.default()
Intents.members = True
Intents.messages = True
client = commands.Bot(intents=Intents)

@client.event
async def on_ready():
    print(f'Logged on as {client.user}')

@client.slash_command(name="setup",guild_ids=[my guild id is really here])
async def setup(ctx):
    pprint(vars(ctx))
    print(ctx.message)
    await ctx.respond("Slash commands was executed")

if __name__ == "__main__":
    key = json.load(open('botToken.json','r'))['token']
    client.run(key)
#

Oh and pprint(vars(ctx)) returns:

{'_state': <discord.state.ConnectionState object at 0x000001F4310AB940>,
 'bot': <discord.ext.commands.bot.Bot object at 0x000001F42D208640>,
 'command': <discord.commands.SlashCommand name=setup>,
 'focused': None,
 'interaction': <discord.interactions.Interaction object at 0x000001F431092180>,
 'options': None,
 'value': None}
tidal vale
#

b!rtfm pyc ApplicationContext.message

harsh mesaBOT
unique moat
tidal vale
#

options?

cursive gateBOT
#

Here's the slash options example.

unique moat
#

yes! that's it thank you very much!

#

Is there a way I could let the user chose a channel? Or is it easier to just let them type in the channel name/id?

tidal vale
#

typeghint discord.TextChannel

unique moat
#

Sorry, but I don't understand it.

pale fossil
#

a type hint is like: ```py
def funcName(param: typeHintHere):
functionHere

unique moat
#

I don't understand the type hint part. How can I make it show the channels?

pale fossil
#

what does your command look like rn

#
@bot.slash_command(name="attach_file")
@option(
    "attachment",
    discord.Attachment,
    description="A file to attach to the message",
    required=False  # The default value will be None if the user doesn't provide a file.
)
async def say(
    ctx: discord.ApplicationContext,
    attachment: discord.Attachment,
):
    """This demonstrates how to attach a file with a slash command."""
    if attachment:
        file = await attachment.to_file()
        await ctx.respond("Here's your file!", file=file)
    else:
        await ctx.respond("You didn't give me a file to reply with! :sob:")
#

theres one of the official examples

#

see where it says discord.Attachment?

#

thats a typehint

#

so you need to do that but with discord.TextChannel

unique moat
#

this returns an error

#

oh wait

#

nvm

#

i missed an await

pale fossil
#

you have channel = discord.TextChannel

#

you need channel: discord.TextChannel

unique moat
#

oh

#

my bad

#

and what do I need to input, or is there something supposed to pop up when i type in /setup?

#

Because i currently get a reply "None"

pale fossil
#

there should be something pop up

#

just make sure youre typing your command then either a space or hit tab

unique moat
pale fossil
#

weird

#

possibly being ratelimited?

#

is that a global command or does it have debug_guilds

unique moat
#

I haven't set up debug_guilds

#

but the bot is only in 2 other servers, without any activity

#

now it's only in the testing server and still the same result

pale fossil
#

if its a global command, it doesnt matter how many servers there are, it can take up to an hour

unique moat
#

since the first launch of the bot with the slash command? Or since the bot is active

#

even with the guild added as a debug guild, it's still the same result

#
client = commands.Bot(debug_guilds=[int(json.load(open('botToken.json','r'))['guildID'])],intents=Intents)
#

yeah i found it

#

it works now

#
@client.slash_command(name="setup",guild_ids=[guildID])
async def setup(
    ctx:discord.ApplicationContext,
    welcomechannel: Option(discord.TextChannel),
    loggingchannel: Option(discord.TextChannel),
    welcomemessage: Option(str,"Enter the message to display")
    ):
    await ctx.respond(f"Selected welcome Channel: {welcomechannel}\nSelected logging channel {loggingchannel}\nWelcome message: {welcomemessage}")
#

Option(Discord.TextChannel) is required.