Search code examples
pythondiscord

discord.py /say more than one word


so I'm trying to make a /say command for my discord bot. The /say command makes the bot say what you said before e.g person: /say hi bot: hi

So far I've made it work but it can only say one word: person: /say hello world bot: hello

so how do I make it so that it can say more than one word? here's my code:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='/')

@bot.command()
async def say(ctx, string: str):
    await ctx.send(string)


bot.run('Token')

Solution

  • When passing in arguments to a discord bot, it takes each argument one at a time.

    For example if I were to write the following command function:

    async def add(ctx, num1: int, num2: int):
    

    Then it would only take in the first two arguments I pass into it, for example

    /add 1 2
    

    When passing in an unknown amount of arguments, you must use *args, the key for variadic arguments in python.

    If you change your function to

    async def say(ctx, *args):
        string = ' '.join(args)
    

    Then it will work as expected. This takes in the rest of the arguments passed into the command and joins them together with spaces.