【问题标题】:Discord bot not recognizing commandsDiscord bot 无法识别命令
【发布时间】:2018-08-15 01:29:03
【问题描述】:

我是 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")

【问题讨论】:

  • 你的函数名是命令名
  • 好吧,我觉得自己像个白痴。谢谢。

标签: python bots discord


【解决方案1】:

在函数中你需要有 ctx 变量

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

我认为... 告诉你它是否对你有帮助

【讨论】:

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