python - 为什么这个 @bot.command() 在我的 Discord 机器人中不起作用
问题描述
我正在尝试创建一个多功能机器人。主要功能之一是根据 ubisoft 端点检查用户名的可用性。但是该命令不起作用,因为我没有得到任何输出。我在这里做错了什么,我忽略了什么?
@bot.command()
async def check(ctx, *, arg):
headers["Authorization"] = "Basic " + base64.b64encode(bytes(open("external/credentials.txt", "r").readline(), "utf-8")).decode("utf-8")
r = requests.post("https://public-ubiservices.ubi.com/v3/profiles/sessions", json={"Content-Type":"application/json"}, headers=headers)
if r.status_code == 200:
if r.json()["ticket"]:
token = "Ubi_v1 t=" + r.json()["ticket"]
headers['Authorization'] = token
url = f"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={arg}&platformType=uplay"
req = requests.get(url, headers = {
'Method':'GET',
'Authority':'public-ubiservices.ubi.com',
'referer':'https://lb-prod-acc_ount-pdc.ubisoft.com',
'Ubi-AppId':'c5393f10-7ac7-4b4f-90fa-21f8f3451a04',
'Authorization': token,
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'Ubi-RequestedPlatformType':'uplay'})
if len(req.json()['profiles']) != 0:
embedVar = discord.Embed(title="", description=f"{arg} is taken", color=0xb0ccb0)
embedVar.set_author(name="Gxzs' Slave", url='https://github.com/gxzass', icon_url='https://64.media.tumblr.com/3e33d5bb1e9a74f4bf66d0100a96d2a8/3b23a519a865c5eb-8c/s400x600/fea2d95ff38041dc048644449c8ae9d68b08acb9.jpg')
await ctx.send(embed=embedVar)
else:
embedVar = discord.Embed(title="", description=f"{arg} is available", color=0xb0ccb0)
embedVar.set_author(name="Gxzs' Slave", url='https://github.com/gxzass', icon_url='https://64.media.tumblr.com/3e33d5bb1e9a74f4bf66d0100a96d2a8/3b23a519a865c5eb-8c/s400x600/fea2d95ff38041dc048644449c8ae9d68b08acb9.jpg')
await ctx.send(embed=embedVar)
机器人的其他功能是这样调用的@bot.event on_message
:
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('.help general'):
embedVar = discord.Embed(title="", description="General commands", color=0xb0ccb0)
embedVar.set_author(name="Gxzs' Slave", url='https://github.com/gxzass', icon_url='https://64.media.tumblr.com/3e33d5bb1e9a74f4bf66d0100a96d2a8/3b23a519a865c5eb-8c/s400x600/fea2d95ff38041dc048644449c8ae9d68b08acb9.jpg')
embedVar.add_field(name='.Coinflip', value="Flips a coin", inline=False)
embedVar.add_field(name='.Poll {message}', value="Create a poll", inline=False)
embedVar.add_field(name='.Check {name}', value="Check uplay name availability", inline=False)
embedVar.add_field(name='.Help general', value="Shows general commands", inline=False)
embedVar.add_field(name='.Help music', value="Shows music commands", inline=False)
await message.channel.send(embed=embedVar)
解决方案
阅读discord.py文档的完整解释, 但简而言之,on_message 在命令处理程序可以到达之前覆盖了消息,这可以通过添加一个来解决
await bot.process_commands(message)
到你的 on_message 事件的远端
这使得 on_message 忽略来自机器人的任何命令,因此 bot.command() 可以处理它
推荐阅读
- angular - Angular canActivate Guard 使用 API
- amazon-web-services - 如何在 CloudFormation 中将字符串列表作为参数传递?
- android - Android 多语言支持:由于占位符计数不同导致的字符串格式问题
- django - django paypal ipn - 我怎么知道付款流程已经开始?
- c++ - 在方法定义中显式指定命名空间
- maven - 为什么没有用于扩展的 maven extensionManagement 元素?
- php - Laravel Eloquent 获取没有的地方,不为空时搜索有的地方
- angular - 如何使用 Jasmine 在 Angular 2 中对警报进行单元测试?
- java - 无法从增量中返回 int
- java - 如何在带有数组的 for 循环中使用我的 if/else 语句?