Discord bot无法识别命令

我是discord API的新手,我无法弄清楚为什么我的命令无法识别 . 我已经阅读了文档,但我不确定在哪里查看 . 任何帮助,将不胜感激 . 不要介意硬编码列表 . 我打算将来改变它 . 现在我只想确保它有效 .

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

Client = discord.Client()
client = commands.Bot(command_prefix = "!")

@client.event
async def on_ready():
    print("Bot online!")

@client.command(pass_context=True)
async def add_roles(member, *roles):
    if message.content.startswith("!role"):
        role_list = ["CS101", "CS492", "CS360", "CS213", "CS228", "CS401", "CS440", "CS450", "CS480", "CS410", "CS420", "CS430", "CS108", "CS111", "CS226", "CS312", "CS405", "CS413", "CS435", "CS499", "CS250", "CS475", "CS445"]
        entered_role = message.content[6:].upper()
        role = discord.utils.get(message.server.roles, name=entered_role)

        if role is None or role.name not in role_list:
            # If the role wasn't found by discord.utils.get() or is a role that we don't want to add:
            await client.send_message(message.channel, "Role doesn't exist.")
            return
        elif role in message.author.roles:
            # If they already have the role
            await client.send_message(message.channel, "You already have this role.")
        else:
            try:
                await client.add_roles(message.author, role)
                await client.send_message(message.channel, "Successfully added role {0}".format(role.name))
            except discord.Forbidden:
                await client.send_message(message.channel, "I don't have perms to add roles.")

@client.command(pass_context=True)
async def remove_roles(member, *roles):
    if message.content.startswith("!unassign"):
        role_list = ["CS101", "CS492", "CS360", "CS213", "CS228", "CS401", "CS440", "CS450", "CS480", "CS410", "CS420", "CS430", "CS108", "CS111", "CS226", "CS312", "CS405", "CS413", "CS435", "CS499", "CS250", "CS475", "CS445"]
        roles_cleared = True

        for r in role_list:
            # Check every role
            role = discord.utils.get(message.server.roles, name=r)
            if role in message.author.roles:
                # If they have the role, get rid of it
                try:
                    await client.remove_roles(message.author, role)
                except discord.Forbbiden:
                    await client.send_message(message.channel, "I don't have perms to remove roles.")
                    roles_cleared = False
                    break

        if roles_cleared:
            await client.send_message(message.channel, "Roles successfully cleared.")

client.run("mytoken")

回答(1)

2 years ago

在功能中你需要有ctx变量

@client.comman(pass_context = True)
async def some_command(ctx, bla_bla, and_bla_bla):
      pass

我想......告诉你这是否有用