Discord Python Bot - User input

你。 提交于 2020-04-18 03:09:24

问题


So I want to make a discord python bot that takes user inputs. what I want to do is count % of something.

Here is what i want to do :

I made this code before starting on the discord bot. But I don't know how to take user inputs.

userInputOriginalPrice = float(input("Enter the original price: "))

userInputPercentage = float(input("Enter how much percantage: "))

discount = ( userInputPercentage / 100) * userInputOriginalPrice

finalCost = userInputOriginalPrice - discount

print("You saved", discount, ". Your total is", finalCost)

回答1:


You can use the client.wait_for() method which takes event and check, so

import discord
from discord.ext import commands

client = commands.Bot(commands_prefix='!')

@commands.command()
async def calculate_percentage(ctx):

    await ctx.send('Enter the original price: ')
    message_response = client.wait_for('message', check=lambda m: m.user == ctx.user)
    original_price = float(message_response.content)

    await ctx.send('Enter how much percantage: ')
    message_response = client.wait_for('message', check=lambda m: m.user == ctx.user)
    input_percantage = float(message_response.content)

    # Your calculations
    discount = ( input_percantage / 100) * original_price 
    finalCost = original_price - input_percantage

    await ctx.send(f"You saved {discount}. Your total is, {finalCost}")

client.run('Bot Token')


来源:https://stackoverflow.com/questions/60587303/discord-python-bot-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!