【问题标题】:Discord.py @client.commands() unable to execute but @client.events can executeDiscord.py @client.commands() 无法执行但@client.events 可以执行
【发布时间】:2020-11-09 13:26:57
【问题描述】:

在为视频游戏 VALORANT 制作机器人时,我意识到我根本无法让 client.commands 工作,而 on_message 的 client.event 仍然有效

其实我也不知道怎么回事

我尝试做的事情:

  • 将 command_prefix 更改为单个变量(最初它有多个 command_prefix)
  • 使用来自可以工作的机器人的代码。 (Ping 命令)
  • 去开发者门户看看我是否给了机器人足够的权限(我给了它管理员)
  • 别名数量减少
  • 将别名更改为名称
  • 导入异步
  • 输入打印函数以确定等待是否是无效的(它没有打印出我设置的文本,我认为机器人无法完全识别命令)

到目前为止,没有任何效果,我是否从根本上错过了什么?

import discord
import random

from discord.ext import commands

client = commands.Bot(command_prefix=['v:'])

@client.event
async def on_ready():
    await client.change_presence(activity = discord.Game(name = "VALORANT"))
    print("Initiated!")

# commands that cannot work
@client.command()
async def ping(ctx):
    print("if this shows up then it works lmao")
    await ctx.send(f"Pong! {round(client.latency * 1000)}ms")

@client.command(aliases = ['RAgent', 'ragent', 'RandomAgent', 'randomagent'])
async def Random_Agent_Selection(ctx):
    print("if this shows up then it works lmao")
    Agents = ['Breach',
              'Brimstone',
              'Cypher',
              'Jett',
              'Killjoy',
              'Omen',
              'Phoenix',
              'Raze',
              'Reyna',
              'Sage',
              'Skye',
              'Sova',
              'Viper']
    await ctx.channel.send(f"Random Agent: {random.choice(Agents)}")

# event that DOES work
@client.event
async def on_message(message):
    if "valorant" in message.content.lower() and message.author.id != 750663559695958016: 
    #id = bot id so it wont infitely loop the command
        channel = message.channel
        await channel.send("The Valorant corporation is waiting for you")

client.run("TOKEN")

【问题讨论】:

    标签: python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    on_message 事件结束时需要 process_commands

    阅读更多:Here

    以下是修改后的代码:

    @client.event
    async def on_message(message):
        if "valorant" in message.content.lower() and message.author.id != 750663559695958016: 
        #id = bot id so it wont infitely loop the command
            channel = message.channel
            await channel.send("The Valorant corporation is waiting for you")
        await client.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多