本文介绍了Discord.py @client.commands() 无法执行但@client.events 可以执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While making a bot for the video game VALORANT out of fun, I realised I could not get the client.commands to work at all while the client.event for on_message still works

其实我也不知道怎么回事

I actually have no idea what is wrong

我尝试做的事情:

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

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

So far nothing works, is there sth I missed on a fundamental level?

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")

推荐答案

on_message事件结束时需要process_commands.

阅读更多:这里

以下是修改后的代码:

@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)

这篇关于Discord.py @client.commands() 无法执行但@client.events 可以执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:11