Search code examples
discorddiscord.py

How to add variables from the user to discord.py?


I need to write a simple bot for discord that will be able to record 3 variables from the user, for example:

`a = int(input('Enter your first number: '))

b = int(input('Enter your second number: '))

с = int(input('Enter your third number: '))

z = a + b + c

print(z)`


Solution

  • You definitely need to read the docs but i will answer it for you.

    Discord.py does not take imputs like that. You have to make a command with parameters. Here is a simple command for what you want.

      @client.command() 
      async def add(ctx, number1, number2, number3):
            num1, num2, num3 = int(number1), int(number2), int(number3)
    
            await ctx.send(num1 + num2+ num3)
    

    if your commands.bot variable is set to bot you can replace the client section with @bot.command(). Basically what this code does is that it creates a command with 3 parameters. The 1st number, 2nd number and the 3rd number. Since we are typing a string as a parameter in discord the num1 num2 and num3 variables will convert them into integers so that we can add them and finally send it to them.