当前位置: 首页 > 知识库问答 >
问题:

不一致的bot无法识别命令

太叔超英
2023-03-14

我是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个答案

宋洲
2023-03-14

在函数中,您需要有ctx变量

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

我想...告诉我这对你有没有帮助

 类似资料:
  • 这是我写的,只是为了启动我的discord机器人,但是每当我尝试使用这个命令时?嗨,我收到了错误信息 忽略命令无异常:discord.ext.commands.errors.命令没有找到:命令"hi"没有找到" 我试过几次,但我是新手,没有任何效果。 下面的答案有助于解决这个问题,这是我的新代码,因为机器人现在不会给出错误消息,但不会响应命令

  • 我在我的discord机器人上遇到了一个问题。命令不起作用。我认为代码不是问题。除了命令外,一切都很完美。不和谐,如果我使用!在这里测试mytext,什么都不会发生。 提前谢谢你!

  • 问题内容: 好的,我正在Windows(7)上运行节点。使用npm,我刚刚将模块安装到d:\目录。因此,我的文件结构如下所示: 但是,当我在此“ myproject”目录中时,例如,我似乎无法运行“ express”: 我做错什么了吗? 问题答案: 我的猜测是您没有全局安装Express。您可以使用以下命令(请参阅http://expressjs.com/guide.html)在全球范围内安装Ex

  • 如前所述。我正在尝试为我的Discord机器人创建一个临时静音命令。我遇到的问题是,它创建了一个静音的角色,但即使我更改了烫发,用户仍然可以编写消息。除此之外,我还收到了以下弃用警告: (节点:15956)弃用警告:集合#查找:改为传递函数

  • 我对discord机器人和python有点陌生,所以我在理解哪里出了问题时遇到了一些困难。基本上我想让我的机器人锁定一条消息。我找到了一个python代码的文档,上面说要使用pin_message(),所以我有: 但我得到以下错误AttributeError:“TextChannel”对象没有属性“pin_message” 当我查看Discord文档时,它会显示PUT/channels/{chan

  • 当一个字段名的第二个字母大写时,杰克逊似乎有问题。 取值映射: 我使用Jackson的创建了一个Java对象。下面是一段Java代码: 我得到一条包含所有18个字段的错误消息。请注意,当大写为第二个字母时,camel大小写失败: 如果我将更改为,则该命令通过,而Jackson在上失败。 对于其他堆栈溢出文章,我已经验证了字段名和getter/setter匹配(是,是)。 如果重要的话,下面是的创建