Discord Python Bot-用户输入 [英] Discord Python Bot - User input
本文介绍了Discord Python Bot-用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我想制作一个采用用户输入的不和谐python机器人。
我想做的是计算某事的百分比。
So I want to make a discord python bot that takes user inputs. what I want to do is count % of something.
这就是我想做的:
在启动Discord机器人之前,我已经编写了这段代码。但我不知道该如何接受用户输入。
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)
推荐答案
您可以使用client.wait_for()方法进行事件和检查,因此
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')
这篇关于Discord Python Bot-用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文