Discord.py @client.commands() 无法执行但@client.events 可以执行
Discord.py @client.commands() unable to execute but @client.events can execute
出于乐趣为电子游戏 VALORANT 制作机器人时,我意识到我根本无法让 client.commands 工作,而 on_message 的 client.event 仍然可以工作
我其实不知道哪里出了问题
我尝试做的事情:
- 将 command_prefix 更改为单个变量(原来它有多个 command_prefixes)
- 使用有效的机器人代码。 (平命令)
- 去开发者门户看看我是否给了机器人足够的权限(我给了它管理员)
- 别名数量减少
- 将别名更改为姓名
- 导入异步
- 输入打印函数来确定等待是否是那个不工作的函数(它没有打印出我设置的文本,我认为是机器人没有完全识别命令)
到目前为止没有任何效果,我是否在基本层面上遗漏了什么?
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
。
阅读更多: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)
出于乐趣为电子游戏 VALORANT 制作机器人时,我意识到我根本无法让 client.commands 工作,而 on_message 的 client.event 仍然可以工作
我其实不知道哪里出了问题
我尝试做的事情:
- 将 command_prefix 更改为单个变量(原来它有多个 command_prefixes)
- 使用有效的机器人代码。 (平命令)
- 去开发者门户看看我是否给了机器人足够的权限(我给了它管理员)
- 别名数量减少
- 将别名更改为姓名
- 导入异步
- 输入打印函数来确定等待是否是那个不工作的函数(它没有打印出我设置的文本,我认为是机器人没有完全识别命令)
到目前为止没有任何效果,我是否在基本层面上遗漏了什么?
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
。
阅读更多: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)