为什么Discord机器人无法识别命令? [英] Why won't Discord bot recognize commands?
问题描述
我正在为宠物小精灵服务器制作一个机器人,我正在尝试创建一个将"Gym Leader"角色赋予其他用户的命令.我尝试使用命令和test命令,但是服务器和外壳程序都没有响应.
I'm making a bot for a pokemon server, and I'm trying to make a command that will give the 'Gym Leader' role to another user. I try using the command, and using the test command, but there is no response in the server nor the shell.
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='b!', case_insensitive=True)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event #works
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
channel = client.get_channel(697500755766018098)
@client.event #works
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to Pokémon Beast Ball!\n\nThis server utilizes Pokecord and Mewbot.\n\nSay \'pkhelp\' in the server to learn about Pokecord commands.\nSay \';help\' in the server to learn about Mewbot commands.'
)
@bot.command() #doesn't work
async def test(ctx):
print("test recieved")
await ctx.send(ctx)
@bot.command(pass_context=True) #this is the command that really needs help
async def newleader(ctx: discord.User=None):
print("command recieved")
if not user:
await ctx.send("Invalid")
print("1")
else:
role = discord.utils.get(ctx.guild.roles, name="Gym Leader")
role2 = discord.utils.get(ctx.guild.roles, name="Purple UniTurtle Man")
if role in ctx.author.roles or role2 in ctx.author.roles:
print("2")
await ctx.send(f'A new Gym Leader has been appointed')
await user.add_roles(role)
await bot.remove_roles(ctx.author, role)
else:
print("3")
await ctx.send("You do not have permission to use this command")
client.run(TOKEN)
推荐答案
您正在混合 bot
和 client
和您的 client = discord.Client()
踩到您的 bot =命令.Bot(...)
语句.由于要执行命令和事件,因此仅使用 commands.Bot(...)
语句.
You are mixing bot
and client
and your client = discord.Client()
is stepping on your bot = commands.Bot(...)
statement. Since you want to do commands and events you only use the commands.Bot(...)
statement.
删除 client = discord.Client()
语句,然后将您的 @ client.event
装饰器更改为 @ bot.event
.
Remove the client = discord.Client()
statement and change your @client.event
decorators to @bot.event
.
如果您想在测试命令中引用命令上下文,请使用ctx参数 async def test(ctx):
进行更新.
Also if you want to reference the command context in your test command update it with the ctx parameter async def test(ctx):
.
这将使您开始使用命令,现在输入 b1test
即可.
That will get you started using your commands and entering b1test
will now work.
请注意,命令声明中的 case_insensitive = True
是指命令名称,而不是前缀.
Please note that the case_insensitive=True
on the commands declaration refers to the command name and not the prefix.
这篇关于为什么Discord机器人无法识别命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!