【问题标题】:Why doesn't this @bot.command() work in my Discord bot [duplicate]为什么这个@bot.command() 在我的 Discord 机器人中不起作用 [重复]
【发布时间】:2021-11-22 11:33:10
【问题描述】:

我正在尝试创建一个多功能机器人。主要功能之一是根据 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)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    阅读discord.py docs上的完整说明 但简而言之,on_message 在命令处理程序可以到达之前覆盖了消息,这可以通过添加一个来解决

    await bot.process_commands(message)
    

    到你的 on_message 事件的远端

    这使得 on_message 忽略来自机器人的任何命令,因此 bot.command() 可以处理它

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2021-12-12
      • 2020-12-31
      • 2020-04-07
      • 2020-09-27
      • 2018-05-04
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多