我的addrole+ Removerole命令?

发布于 2025-02-10 12:37:02 字数 1337 浏览 3 评论 0原文

因此,我最近尝试为我的Discord机器人制作一些附加和外径命令。我在YT上使用了James S的视频(第15集),几乎完全复制了。 我用Python语言在Pycharm工作。

这是代码:

@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def addRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await ctx.send(f"{user.mention} already has that role! ({role})")
    else:
        await user.add_roles(role)
        await ctx.send(f"Added {role} to {user.mention}!")

@addRole.error
async def role_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def removeRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await user.remove_roles(role)
        await ctx.send(f"{user.mention} doesn't have that role! ({role})")
    else:

        await ctx.send(f"Removed {role} from {user.ention}!")

@removeRole.error
async def removeRole_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")

这是我遇到的错误; 忽略命令中的异常无: discord.ext.commands.errors.commandnotfound:找不到命令“ addrole”

So I recently tried to make some AddRole and RemoveRole commands for my Discord Bot. I used a video from James S on YT (episode 15) and almost copied almost exactly.
I work in PyCharm, with the Python language.

This is the code:

@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def addRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await ctx.send(f"{user.mention} already has that role! ({role})")
    else:
        await user.add_roles(role)
        await ctx.send(f"Added {role} to {user.mention}!")

@addRole.error
async def role_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def removeRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await user.remove_roles(role)
        await ctx.send(f"{user.mention} doesn't have that role! ({role})")
    else:

        await ctx.send(f"Removed {role} from {user.ention}!")

@removeRole.error
async def removeRole_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")

This is the error I get;
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "addRole" is not found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-02-17 12:37:02

我什至无法运行您的代码,因为它说我缺少参数,因此我对其进行了重构并进行了更正,并且可以很好地授予/撤销角色。我无法真正重现您遇到的问题。

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")


@bot.command(pass_context=True)
@commands.has_permissions(manage_roles=True)
async def addRole(ctx, user: discord.Member, *, role: discord.Role):
    if role in user.roles:
        await ctx.send(f"{user.mention} already has that role! ({role})")
    else:
        await user.add_roles(role)
        await ctx.send(f"Added {role} to {user.mention}!")


@addRole.error
async def role_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


@bot.command(pass_context=True)
@commands.has_permissions(manage_roles=True)
async def removeRole(ctx, user: discord.Member, *, role: discord.Role):
    if role in user.roles:
        await user.remove_roles(role)
        await ctx.send(f"{user.mention} doesn't have that role! ({role})")
    else:

        await ctx.send(f"Removed {role} from {user.ention}!")


@removeRole.error
async def removeRole_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


bot.run("your token here")

我更改了:

  1. 我已经定义了用于命令的机器人和前缀(我不确定您只粘贴了代码的一部分,所以您这样做了)。
  2. 我已经修复了一些PIP错误(空格,行),这些错误根本不影响代码,但现在看起来更清晰。
  3. 我从必需的参数中删除了self,因为您无论如何都没有使用它。

....然后我做了一切都很好。查看我的代码,让我知道问题是否仍然存在。

I could not even run your code, because it was stating I'm missing parameters, so I refactored and corrected it a bit and it works fine with granting/revoking roles for me. I could not really recreate the problem you have.

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")


@bot.command(pass_context=True)
@commands.has_permissions(manage_roles=True)
async def addRole(ctx, user: discord.Member, *, role: discord.Role):
    if role in user.roles:
        await ctx.send(f"{user.mention} already has that role! ({role})")
    else:
        await user.add_roles(role)
        await ctx.send(f"Added {role} to {user.mention}!")


@addRole.error
async def role_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


@bot.command(pass_context=True)
@commands.has_permissions(manage_roles=True)
async def removeRole(ctx, user: discord.Member, *, role: discord.Role):
    if role in user.roles:
        await user.remove_roles(role)
        await ctx.send(f"{user.mention} doesn't have that role! ({role})")
    else:

        await ctx.send(f"Removed {role} from {user.ention}!")


@removeRole.error
async def removeRole_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


bot.run("your token here")

What I've changed:

  1. I've defined the bot and prefix to use with commands (I'm not sure you did that since you pasted only a part of your code).
  2. I've fixed some pip errors (spaces, lines), that should not affect code at all, but now it looks clearer.
  3. I've removed self from required parameters since you were not using it anyway.

....and after I've done that everything worked fine. Check out my code and let me know if the problem still exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文