如何根据用户输入获取和发送嵌入



好的,所以我试图创建一个命令,根据用户输入的内容而变化。例如,用户键入"!peakrating";然后机器人会回答"你的id是什么"。然后,根据用户在嵌入后的回应,将发送不同的内容。下面是我试过的没有成功的代码。

@client.command()
async def peakrating(ctx, msg):
ctx.send("What is your id")
id = msg
url = f"https://api.brawlhalla.com/player/{id}/ranked?api_key=MY_API_KEY"
r = requests.get(url)
udata = r.json()
upeakrating = udata['peak_rating']
premb = discord.Embed(title="PeakRating")
premb.add_field(name="Your peak elo", value=f"{upeakrating}")
await ctx.send(embed=premb)

下面是上面代码后面的错误"discord.ext.commands.errors. missingrequireargument: msg是缺少的必需参数。">

试试这个:

@client.command()
async def peakrating(ctx, id=None):
if not id:
await ctx.send("What is your id?")
return
url = f"https://api.brawlhalla.com/player/{id}/ranked? 
api_key=MY_API_KEY"
r = requests.get(url)
udata = r.json()
upeakrating = udata['peak_rating']
premb = discord.Embed(title="PeakRating")
premb.add_field(name="Your peak elo", value=f"{upeakrating}")
await ctx.send(embed=premb)

你还忘了在你发送的第一条消息中设置await

试试下面的代码:

def check就像一个终端输入

import asyncio

@client.command()
async def peakrating(ctx):
try:
def check(message):
return message.author == ctx.author and message.channel == ctx.channel 
id = await client.wait_for('message', timeout=60, check=check) 
# the member have 60 seconds to answer, you can change it if you want to
url = f"https://api.brawlhalla.com/player/{id}/ranked? 
api_key=MY_API_KEY"
r = requests.get(url)
udata = r.json()
upeakrating = udata['peak_rating']
premb = discord.Embed(title="PeakRating")
premb.add_field(name="Your peak elo", value=f"{upeakrating}")
await ctx.send(embed=premb)
except asyncio.TimeoutError:
return await ctx.send('You did take so long, try again!')

但是如果你不想超时,你需要:

  • 移除import asyncio

  • 取消timeout=60onclient.wait_for功能

  • except的内容替换为:

    except:
    return
    

这应该可以工作!

最新更新