#Checking if message is something in a slash command?

1 messages · Page 1 of 1 (latest)

celest snow
whole olive
#

My code is far more complicated, and I don't want to share it, so I'll just use some example code because it doesn't work there either.

message.channel.contains("something") used to work well, but now it doesn't. Nothing happens, no errors in the terminal. I was forced to use slash commands and have been using it ever since.

Even something as simple as this doesn't work:

import discord

client = discord.Client()
@client.event
async def on_ready():
     print(f"Logged in as {bot.user}")
@client.event
async def on_message(message):
    if message.content == "Duck":
        await message.channel.send("Goose")

So combining it with slash commands obviously doesn't work:

import discord

bot = discord.Bot()
@bot.event
async def on_ready():
    print("Logged in as {bot.user}")
@bot.slash_command(guild_ids=[0123456789])
async def test(ctx):
    if message.content == "Duck":
        await ctx.respond("Goose")

Even tried combinations out of desperation like ctx.message (even if it isn't an attribute), ctx.content, or ctx.interaction.message, nothing happened. Enabling the Message intent in the Developer Portal and adding:

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

to the code didn't work. I need help.

rancid dust
#

You must now explicitly define the messages intent

#

On the portal

#

Have you done that? If not, kick, select, reinvite

whole olive
rancid dust
#

Ah I see the problem

#

Full disclosure, I do NOT know how to fix it, someone else can chime in

#

But I do see it

lone comet
#

did you even tried to print the content as first debug step?

rancid dust
#

Your if message content is within the slash command. But there was no message. It was an interaction

lone comet
rancid dust
#

Yup. If you placed that function within an event, it would work. Not within a slash command.

#

Also your slash command is missing name and description

lone comet
#

except you do something crazy like fetch the last message in the executing channel or even make an int option like content of 5 messages before

#

and that

#

🤣

rancid dust
#

But yes, to Lalas point, I would put various print to console messages, to see exactly where it fails

lone comet
#

duck

meager ferryBOT
lone comet
rancid dust
#

Lol!

#

duck

meager ferryBOT
lone comet
#

it's too easy in c#

rancid dust
#

I’m way too dumb for C#. Lmao

lone comet
#

hahaha i have 13 years experience

#

😅

#

or > i had no friends, so i learned

#
import discord

client = discord.Client()
@client.event
async def on_ready():
     print(f"Logged in as {bot.user}")

@client.event
async def on_message(message):
    print(message.content)
    if message.content == "Duck":
        await message.channel.send("Goose")
lone comet
#

try this first

#

FUCK U NYUW

#

i forgot

#

🤣

meager ferryBOT
#

Goose

lone comet
#

yes shut

#

duck

meager ferryBOT
lone comet
#

fuck

whole olive
#

i was doing something else, then started writing another dummy bot, this did print something

lone comet
#

ok so it finds something

#

hmm

#
Client.MessageCreated += async (s, e) =>
{
        if (e.Channel.Id == 992386142093713418 && e.Message.Content.ToLower().Contains("duck")) 
            await e.Message.RespondAsync("Goose");
};

as my code is, i would suggest you make the string lower and check for the contain then

#

dunno how to make it in python tho 😅

whole olive
#

it works now

#

wtf

lone comet
#

welcome to programing doggolul

#

so this is solved?

whole olive
#

probably not, i'll have to rewrite like 60+ lines

lone comet
#

oof

#

good luck

whole olive
#

since you said it wouldn't work in a slash command, would bot.command() work

#

nvm

whole olive
#

i rewrote several lines and reduced it by about 40 lines, with the added benefit of it not working. i don't think this is possible without a command, which defeats the purpose of having to do all this rewriting

rancid dust
whole olive
#

which is why i sent the "nvm"

rancid dust
#

It’s also important to note that messages.content is case sensitive

#

Which you might know already but figured I would throw it out there

whole olive
#

by that, do you mean that what im typing as a command is case sensitive, or the actual messages.content in the code is case sensitive

rancid dust
#

the variable

#

So, if you do:

whole olive
#

not that im scared that i made a mistake, i already knew python is case sensitive

rancid dust
#

if message.content == "duck":

#

Duck would not trigger it

#

in that case you'd need to do:
if message.content.lower == "duck":

#

Then DUCK or Duck or any combo would trigger

#

To test whether or not it works, I would add a couple of lines to Lala's edit, so you have more visibility:

@client.event
async def on_message(message):
    if message.content == "duck":
    print(f'Success! {message.content} triggered!')
    # Do things here
    else:
    print(f'Failure! {message.content} was not duck')
#

that way even if it fails, you can see on console what its actually comparing to your variable.

#

You hopefully see something such as :

Failure! Duck was not duck

Then you know its the lower problem.

keen torrent
rancid dust
keen torrent
#

Yeah the new invite will update the scopes

ivory bloom
whole olive
#

it is impossible to accomplish what i intend to, I'll just have to use slash commands without message.content

#

i have no choice

ivory bloom
#

I got a bit lost what are you trying to do?

#

You want to see if a channel contains a message?

whole olive
#

yeah, except that doesn't work for slash commands. the slash command does a while loop then i wanted a way to stop it without restarting the bot, but that isn't possible

rancid dust
#

stopping a while loop? Just use a counter.

#

then again, I still don't know what you're trying to accomplish. lol

#

You can use a counter with time ( seconds ) or with "attempts" but if we knew more details, we could help better.

keen torrent
#

You can use an async iterator over the channel's history

whole olive
rancid dust
#

idk, too little detail to go on here. I mean, based on this vague data, I'd say add something in there to check a json file as well, then your "admin" command changes that json file, effectively telling all other loops to end, but this is a shot in the dark considering the info you've provided.