Ask coding questions

← Back to all posts
How do you make a custom command where the bot returns with a message.
EvanDowding (0)

For example: prefixtest

Bot returns: {your message}

Sorry, I am new to repl.it. I'm using discord.py. I already have my bot running it just doesn't do anything at the moment. This is the first step I can take to become a good bot coder.

Thanks!

Comments
hotnewtop
EvanDowding (0)
async def on_message(message):
    if message.author == client.user:

      if message.content.startswith('$test'):
        await message.channel.send('Hello!')```
EvanDowding (0)

This is in cog_example.py

Coder100 (17018)

Sure! try this:

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$test'):
        await message.channel.send('Hello!')

where client is the client. Then, go on to wherever your bot works, and do $test. Yay!

EvanDowding (0)

@Coder100 And I put this under my line of code in main.py?

Coder100 (17018)

yep
maybe send repl so i know exactly where @EvanDowding

EvanDowding (0)

Alright, I will. I will delete this because it will show my bot token. Sending now... @Coder100

LukeWright (114)

you can put your bot token in a .env file so that people cannot see it. @EvanDowding

EvanDowding (0)

rip. wish i knew that sooner. if people use it ill jsut make another token. @LukeWright

Coder100 (17018)

@EvanDowding

just indent this part to be correct and you are all set!

EvanDowding (0)

What do you mean indent? Delete? @Coder100

Coder100 (17018)

no, the indentation does not match the function indentation remember? @EvanDowding

EvanDowding (0)

I am not sure what you mean. I started this coding thing yesterday. @Coder100

LukeWright (114)

instead of

  @client.event
async def on_message(message):
    if message.author == client.user:

do

@client.event
async def on_message(message):
    if message.author == client.user:
EvanDowding (0)

@LukeWright
i decided to put this in main.py. does that matter?

Coder100 (17018)

yeah it does, it depends on where you define client @EvanDowding