Thornily
Thornily

Reputation: 582

Take in an argument when sending to channel

I am trying to create a discord bot that takes in 2 arguments formats them with text then sends to a channel. Using this code it works but It doesn't send custom stuff. It can only be predefined how would I do this?

    if message.content == '!settrend':
    async def args(arg1, arg2):
        output = ''
        output += f"Changed trend of {arg1} to {arg2}"
        await message.channel.send(output)

    await args("test", "test") # I want this to be able to have custom arguments by the command.

Upvotes: 0

Views: 137

Answers (1)

altermetax
altermetax

Reputation: 706

Instead of checking message.content == '!settrend', you need to first split message.content by spaces, then check that the first token you get is '!settrend'.

So replace the first line with:

arguments = message.content.split()
if arguments[0] == '!settrend':

Then, at the end:

await args(arguments[1], arguments[2])

Please note that this solution requires the two arguments to not include spaces. If you want to be able to have spaced arguments specified by quotes (e.g.: !settrend "first argument" "second argument"), use shlex.split instead of the standard string split method:

import shlex
arguments = shlex.split(message.content)

Upvotes: 3

Related Questions