Discord Python Bot-用户输入

时间:2020-03-08 12:07:55

标签: python discord percentage

因此,我想制作一个采用用户输入的不和谐python机器人。 我想做的是数某事。

这就是我想做的:

enter image description here

在discord bot上启动之前,我已经编写了此代码。但是我不知道如何接受用户输入。

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)

2 个答案:

答案 0 :(得分:1)

您可以使用client.wait_for()方法进行事件和检查,所以

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')

答案 1 :(得分:0)

一种更简单的方法是使用 python 的内置 .split() 方法。

例如当用户输入!percentage 300 60

## this will split your message where there is a space
_, orig_price, discount = message.split(" ")

这将节省时间并创造一个更加用户友好的环境,并且代码更具可读性