Why won't Discord bot recognize commands?

守給你的承諾、 提交于 2021-01-28 08:51:46

问题


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)

回答1:


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.

Remove the client = discord.Client() statement and change your @client.event decorators to @bot.event.

Also if you want to reference the command context in your test command update it with the ctx parameter async def test(ctx):.

That will get you started using your commands and entering b1test will now work.

Please note that the case_insensitive=True on the commands declaration refers to the command name and not the prefix.




回答2:


Did you check :

  • bot connection → make a "on_connect" event (not exactly the same as "on_ready") to see if your bot successfully connect to your server (before receiving data from discord). If not, try to add your bot again to your server and check if all tokens are goods.
  • bot permissions (if your bot have the right to write in channel, read messages from channels, manage roles) → if your bot cannot read messages, he can't read commands !
  • role priority (you can't manage roles highers thant yours) → go to "server settings" > "roles" > put your bot role above the 'Gym Leader' role (or at the top of the list if you don't care).



回答3:


The problem isn't actually what the selected answer suggests. There is probably no reason to use both commands.Bot and discord.Client but using both won't lead to that issue.

The issue is because you are only running the client, not the bot. You need to also run the bot instance if you want it to function.

If you are not trying to do something specific, then just using either bot or client will suffice anyway, so that part of the selected answer was helpful in avoiding the issue at least.



来源:https://stackoverflow.com/questions/61536253/why-wont-discord-bot-recognize-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!