【问题标题】:Why won't Discord bot recognize commands?为什么 Discord bot 无法识别命令?
【发布时间】:2020-08-15 13:48:18
【问题描述】:

我正在为口袋妖怪服务器制作一个机器人,并且我正在尝试制作一个命令,将“健身房领袖”角色授予另一个用户。我尝试使用命令,并使用测试命令,但服务器和外壳都没有响应。

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)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您正在混合 botclient 并且您的 client = discord.Client() 正在踩到您的 bot = commands.Bot(...) 声明。由于您想要执行命令和事件,因此您只需使用 commands.Bot(...) 语句。

    删除client = discord.Client() 语句并将@client.event 装饰器更改为@bot.event

    此外,如果您想在测试命令中引用命令上下文,请使用 ctx 参数 async def test(ctx): 更新它。

    这将使您开始使用您的命令,现在输入 b1test 即可。

    请注意,命令声明中的case_insensitive=True 指的是命令名称,而不是前缀。

    【讨论】:

      【解决方案2】:

      你检查了吗:

      • bot 连接 → 制作“on_connect”事件(与“on_ready”不完全相同)以查看您的 bot 是否成功连接到您的服务器(在从 discord 接收数据之前)。如果不是,请尝试再次将您的机器人添加到您的服务器并检查所有令牌是否都是商品。
      • bot 权限(如果您的 bot 有权写入频道、从频道读取消息、管理角色)→ 如果您的 bot 无法读取消息,则他无法读取命令!
      • 角色优先级(您无法管理高于您的角色)→ 转到“服务器设置”>“角色”> 将您的机器人角色置于“健身房领导者”角色之上(如果您不这样做,则置于列表顶部不关心)。

      【讨论】:

        【解决方案3】:

        问题实际上并不是所选答案所暗示的。可能没有理由同时使用commands.Botdiscord.Client,但同时使用两者不会导致该问题。

        问题是因为您只运行client,而不是bot。如果您希望它运行,您还需要运行 bot 实例。

        如果您不想做一些具体的事情,那么无论如何只使用botclient 就足够了,因此所选答案的一部分至少有助于避免该问题。

        【讨论】:

          猜你喜欢
          • 2018-08-15
          • 2022-07-12
          • 2021-11-10
          • 2023-01-22
          • 2021-05-29
          • 1970-01-01
          • 1970-01-01
          • 2019-03-05
          • 2020-12-09
          相关资源
          最近更新 更多