如何从不和谐中获取用户输入并将其存储为变量以在方程式中使用?不和谐.py
Posted
技术标签:
【中文标题】如何从不和谐中获取用户输入并将其存储为变量以在方程式中使用?不和谐.py【英文标题】:How do i take user input from discord and store it as a variable to use in an equation? Discord.py 【发布时间】:2022-01-04 18:41:26 【问题描述】:好的,所以我对 discord.py 不是很流利,但这是我尝试使用的代码:
from discord import channel
from discord.embeds import Embed
import discord
from discord.ext import *
import os
import random
import string
from discord.ext import commands
from dotenv import load_dotenv
from discord.ext import commands
import requests
import sys
import threading
from discord.utils import get
import discord
import asyncio
from threading import Thread
from keep_alive import keep_alive
load_dotenv()
bot = commands.Bot(command_prefix = '.')
def init():
loop = asyncio.get_event_loop()
loop.create_task(bot.run(token))
Thread(target=loop.run_forever).start()
@bot.event
async def on_ready():
print('Bot is up and running')
@bot.command()
async def p(ctx):
# MESSAGE 1, should have user input stored in variable "place"
embed=discord.Embed()
embed.add_field(name="Where did you place?", value="** **")
sent = await ctx.send(embed=embed)
# MESSAGE 2, should have user input stored in variable "kills"
embed=discord.Embed()
embed.add_field(name="How many kills did you get?", value="** **")
sent = await ctx.send(embed=embed)
# MESSAGE 3, should have combined input of place + kills AFTER running PLACEMENT CALC in variable "total""
embed=discord.Embed()
embed.add_field(name="Total Points:", value=total)
sent = await ctx.send(embed=embed)
bot.run("OTA0MDc5MzY5NzU1MDU4MjA2.YX2Thg.S4A2e9SqZUvYZpyTzBW44W4Vumk")
keep_alive()
init()
我想从 discord.py 的第一个和第二个消息中获取用户响应,并将它们输入到此代码的前 2 个问题中,这样我就可以计算总数:
import sys
# Placement input, should be response to MESSAGE 1 In discord.py
print ("Where did you place? ")
place = int(input('| \n'))
#Checking if place is in range from 20-1, Should respond with the message in an embed in discord
if place >20 or place <1:
place = 0
# Kill input, response to MESSAGE 2 In discord.py
print ("How many kills did you get?")
kills = int(input('| \n '))
# Assigning total / final variable
total = 0
tplace = 0
# ranges for placement variables
top20 = (18,19,20) #Top 20
top17 = (16,17) #Top 17
top15 = (13,14,15) #Top 15
top12 = (11,12) #Top 12
top10 = (6,7,8,9,10) #Top 10
#Total kills calculations
kills = kills * 10
#Total placement calculations
if place in top20: #Top 20
tplace = 10
if place in top17: #Top 17
tplace = 20
if place in top15: #Top 15
tplace = 35
if place in top12: #Top 12
tplace = 50
if place in top10: #Top 10
tplace = 60
if place == 5: #Top 5
tplace = 80
if place == 4: #Top 4
tplace = 90
if place == 3: #Top 3
tplace = 100
if place == 2: #Top 2
tplace = 125
if place == 1: #Top 1 / vic roy
tlace = 175
#Output, should be MESSAGE 3 In discord.py
total = kills + tplace
print ("Total Points: ")
print (total)
在计算总数之后,我只是在 emebd 中发送变量“total”,我只是不知道如何将响应存储到我需要的变量中的 2 个单独的不和谐消息中。 它应该运行为
Bot: "Where did you place?"
you: 10 (Stored in variable "place")
Bot:"How many kills did you get?"
you: 1 (Stored in variable "kills")
---------calculating the total points (should be 70)-------
Bot: "Total points: 70"
我知道我可能很愚蠢,但我在网上找到的 rlly 没有任何帮助
【问题讨论】:
您应该生成一个新令牌,因为现在任何人都可以使用这个令牌。 【参考方案1】:您可以使用wait_for()功能。 我在下面写了一个函数,可以让你从用户那里获取一些数据。
import asyncio
async def prompt(ctx, message: str, timeout: float) -> str:
await ctx.send(message)
message = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=timeout)
return message.content
让我们将它用于您的命令:
@bot.command()
async def calculate(ctx):
try:
place = await prompt(ctx, "Where did you place?", timeout=10)
kills = await prompt(ctx, "How many kills did you get?", timeout=10)
except asyncio.TimeoutError:
await ctx.send("You are too slow.") # if time limit exceeded
else:
await ctx.send(calculate_score(place, kills)) # now you can calculate total score and send the result using input data (`place`, `kills`)
【讨论】:
我会将它放在我当前的 discord.py 代码中的什么位置?我需要用什么东西代替它还是把它放在什么东西之后? 你应该把我的代码而不是你的命令叫做p
。
当我用它替换它时,我得到“discord.ext.commands.errors.CommandInvokeError:命令引发异常:NameError:名称'提示'未定义”我需要在某处定义提示跨度>
是的,你需要在其他代码之前复制并粘贴我的prompt
函数。【参考方案2】:
我为您的嵌入添加了一些功能以使其看起来更好,并使用wait_for
等待用户输入。
def calculate_points(place, kills):
place_pt = 0
if place in (18,19,20): #Top 20
place_pt = 10
elif place in (16,17): #Top 17
place_pt = 20
elif place in (13,14,15): #Top 15
place_pt = 35
elif place in (11,12): #Top 12
place_pt = 50
elif place in (6,7,8,9,10): #Top 10
place_pt = 60
elif place == 5: #Top 5
place_pt = 80
elif place == 4: #Top 4
place_pt = 90
elif place == 3: #Top 3
place_pt = 100
elif place == 2: #Top 2
place_pt = 125
elif place == 1: #Top 1
place_pt = 175
kills_pt = kills * 10
return place_pt + kills_pt # returning total points
async def message(ctx, message : str, timeout: float):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
question = await ctx.send(message)
response = await bot.wait_for("message", check=check, timeout=timeout)
await question.delete()
await response.delete()
return int(response.content)
@bot.command()
async def p(ctx):
await ctx.message.delete()
try:
place = await message(ctx, f"ctx.author.mention Where did you place?", timeout=60.0)
kills = await message(ctx, f"ctx.author.mention How many kills did you get?", timeout=60.0)
except Exception as e: # error handler
if isinstance(e, asyncio.TimeoutError): # if timeout limit is exceeded
await ctx.send(f"ctx.author.mention You were too slow to answer!")
elif isinstance(e, ValueError): # if you use something else than number (when asked for kills and place)
await ctx.send("Incorrect value!")
else:
total = calculate_points(place, kills) # invoking function that calculates place points
embed=discord.Embed(title=f"ctx.author's points")
embed.set_thumbnail(url=ctx.author.avatar_url)
embed.add_field(name="Place:", value=place, inline=True)
embed.add_field(name="Kills:", value=kills, inline=True)
embed.add_field(name="Total points:", value=total, inline=False)
await ctx.send(embed=embed)
结果:
【讨论】:
有没有办法删除命令使用年限? 我不太确定你的意思?您还想如何启动功能并提供输入? 我的意思是当我使用 !p 命令启动函数时,当函数完成时,我们删除 !p 的使用,以减少有多少聊天来清理它 是的,你可以。您是否也想删除其他消息(问题和答案)并只保留最后嵌入的内容? 是的,这样看起来更干净,我们可以添加日期/时间以上是关于如何从不和谐中获取用户输入并将其存储为变量以在方程式中使用?不和谐.py的主要内容,如果未能解决你的问题,请参考以下文章