问题
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