- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在设置一个机器人,并且正在测试它。我有一个小功能是踢,禁止和取消禁止用户,设置在一个齿轮中,如下所示:
import discord
from discord.ext import commands
class Moderator(commands.Cog):
def __init__(self, bot):
self.bot = bot
#KICK command
@commands.command()
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, member : discord.Member, *, reason=None):
('About to kick')
await member.kick(reason = reason)
@commands.command()
@commands.has_permissions(kick_members=False)
async def kick(self, ctx, member : discord.Member, *, reason=None):
await ctx.send(f'You do not have permission to kick any member, {ctx.message.author.mention}!')
#BAN command
@commands.command()
@commands.has_permissions(ban_members=True)
async def ban(self, ctx, member : discord.Member, *, reason=None):
await member.ban(reason = reason)
await ctx.send(f'Banned {member.mention}')
@commands.command()
@commands.has_permissions(kick_members=False)
async def ban(self, ctx, member : discord.Member, *, reason=None):
await ctx.send(f'You do not have permission to ban any member, {ctx.message.author.mention}!')
#UNBAN command
@commands.command()
@commands.has_permissions(ban_members=True)
async def unban(self, ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'Unbanned {user.mention}')
return
@commands.command()
@commands.has_permissions(kick_members=False)
async def unban(self, ctx, member : discord.Member, *, reason=None):
await ctx.send(f'You do not have permission to unban any member, {ctx.message.author.mention}!')
#CLEAR MESSAGES
@commands.command()
@commands.has_permissions(manage_messages=True)
async def clear(self, ctx, amount=2):
await ctx.channel.purge(limit=amount)
@commands.command()
@commands.has_permissions(manage_messages=False)
async def clear(self, ctx, amount=2):
await ctx.send(f'You do not have permission to delete messages in this way, {ctx.message.author.mention}!')
def setup(bot):
bot.add_cog(Moderator(bot))
现在我已经用一些空格对上面的代码进行了格式化,使其适合一个代码块,因此如果您将其复制粘贴到其他地方,您可能会遇到缩进错误。
继续前进,bot 本身具有管理员权限以及单独的踢出和禁止权限。它也被放置在角色层次结构的顶部,这被视为:
我的机器人的名字是 JunkBot。
现在,每当我作为服务器所有者尝试使用命令 .kick @user 时,它都会弹出以下错误:
错误的文本形式是:
Ignoring exception in command kick:
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await self.prepare(ctx)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 777, in prepare
if not await self.can_run(ctx):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1087, in can_run
return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\utils.py", line 348, in async_all
for elem in gen:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1087, in <genexpr>
return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 1790, in predicate
raise MissingPermissions(missing)
discord.ext.commands.errors.MissingPermissions: You are missing Kick Members permission(s) to run this command.
ban、unban 和 clear messages 命令会引发类似的错误。
有趣的是,作为所有者的我遇到了这个错误,但假设另一个用户,我的一个 friend ,没有踢出、禁止或消息管理角色,完美地运行了他们的代码行,其中她从机器人那里得到消息说她没有踢出、禁止或清除消息的权限。附上截图。
我不知道我错在哪里。请帮我调试一下。
最佳答案
您的问题在于重写函数。我假设你希望制作一个 errorhandler .但是,这不是这样做的方法。
不是用不同的 has_permissions
参数重写函数,您需要制作一个正确版本的错误处理程序。
首先,删除所有重复的函数。这些是在 has_permissions
参数上需要 False
的函数。
接下来,制作一个错误处理函数。您可以只为 Cog 命令、所有命令或特定命令制作一个。如果它适用于所有命令,您将使用事件 on_command_error
,(我建议在主文件中使用它或作为监听器)。如果它用于 Cog,那么您将不得不使用 cog_command_error
.如果它是针对特定命令的,您应该制作 Command.error
装饰器。
我将展示特定于 Cog 的处理程序,但来回切换不会花费太长时间。
# Indent into Cog level
async def cog_command_error(self, ctx, error):
# Allows us to check for original exceptions raised and sent to CommandInvokeError.
# If nothing is found. We keep the exception passed to on_command_error.
error = getattr(error, 'original', error)
if isinstance(error, commands.BotMissingPermissions): # Check if bot is missing permissions
await ctx.send(f'I am missing these permissions to do this command:\n{self.lts(error.missing_perms)}')
elif isinstance(error, commands.MissingPermissions): # Check if user is missing permissions
await ctx.send(f'You are missing these permissions to do this command:\n{self.lts(error.missing_perms)}')
@staticmethod
def lts(list_: list): # Used to make reading the list of permissions easier.
"""List to string.
For use in `self.on_command_error`"""
return ', '.join([obj.name if isinstance(obj, discord.Role) else str(obj).replace('_', ' ') for obj in list_])
这是您可以在处理程序中使用的异常列表:Exceptions
仅供引用,处理程序不需要我拥有的 lts
函数。它用于使权限列表在一个字符串中可读。您可以随时删除它或根据需要更改它。
关于discord - 尽管已授予 Bot 管理员权限,但仍要求踢成员(member)许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67594883/
如何授予 VSTS (TFVC) 用户从分支创建新分支的权限?我已经授予他对 ParentBranch 的“管理分支”和“合并”权限。 当他尝试从“ParentBranch”创建一个名为“ChildB
我在我的 oracle 过程中使用 dbms_crypto.encrypt 函数来加密密码。我已连接到 oracle 为: connect sqlplus as sysdba 然后授予权限: gran
如何在不连接到每一台计算机的情况下将我的 android 设备的 adb 访问权限授予我的所有计算机并按允许? 最佳答案 通过 adb 连接到设备时,每台计算机都会将代码写入 android 设备,以
我有一个 package A ,它在另一个 package B 中使用了一些变量和过程在 相同的架构 .现在想搬家package A到 新架构 .我应该授予 new schema 哪些权限用于使用 p
我需要获取数百个 MS Access DB 的元数据,因此我需要自动化元数据收集过程。 我想查询描述的内容 here ,但在查询数据库之前,我需要对 MSysObjects 进行读取 Access 表
我有一个 JApplet,我想授予它读写打印等权限。该小程序仅从本地文件系统加载 我已经阅读了java教程的控制小程序部分,并成功创建了一个策略文件,授予权限并指定代码库 http://docs.or
我正在使用 phpmyadmin 来调整用户的权限。 我删除了用户“root”的所有访问权限。现在我看不到表格了。 我尝试用另一个用户登录,但它不允许我。 此时我有什么选择? 第 1 步。 //Sto
Mysql 服务器允许从本地主机访问匿名用户。我也想将此权限扩展到其他机器...即,使用没有任何密码的匿名用户从机器 B 的 mysql 客户端访问在机器 A 中运行的 mysql_server。 我
为所有想要连接的新用户授予 MySQL 数据库访问权限的命令是什么? 考虑这个陈述: CREATE USER 'java'@'localhost' IDENTIFIED BY 'password';
我在尝试通过 t 为 mysql 用户授予数据库权限时遇到了一个问题。我认为这是因为数据库名称中的特殊字符。 这个正在工作: /usr/bin/mysql -uroot -pXz5eaCqwvsT0p
我可以在 GRANT 语句中使用静态数据库名称授予权限。 GRANT SELECT,INSERT ON database_name.table_name TO 'username'@'localhos
授予 Facebook 应用查看我的赞的权限是否允许该应用的所有其他用户也看到我的赞? 或者同一应用的其他用户能否看到我的点赞取决于我的点赞隐私设置? 例如,假设我只允许好友看到我的赞。这是否意味着该
我在这里敲了几个小时的脑袋。 我正在向数据库添加用户和密码,同时尝试授予权限。 GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TA
我正在尝试在 Flask 中创建一个自定义错误页面,我想让错误处理程序访问生成导致错误的 API 调用的请求,以便它返回的错误页面可以更改,具体取决于情况。例如,假设有两个端点: (1) @app.r
我已经编写了一个简短的快速代码来向查找器添加一个按钮,该按钮通过系统 touch 调用在当前目录中创建一个新的空白文件。该扩展可以很好地获取当前目录(通过 FIFinderSyncController
当我运行 hdf namenode -format 时,它想要删除目录 /home/hadoop/hadooptmpdata/dfs/name/current 但它没有权限执行此操作。如何授予它权限?
我正在用 C# 编写一个应用程序(对于我的一个 friend 来说,这是非常基础的),但是我有一个 StreamWriter 对象,它在 C: 中创建一个本地文件。我必须以管理员身份运行,它工作正常,
我已经安装了 Centos5,安装了 Web 服务器并设置了虚拟主机。主机几乎是这样设置的: > adduser user1 > mkdir -p /home/user1/public_html/do
我正从 MySQL 迁移到 PostgreSQL,但在用户权限方面遇到了瓶颈。我习惯于使用以下命令为用户分配对数据库所有表的所有权限: # MySQL grant all privileges on
我试图为特定用户授予对 UNC 路径的 NTFS 权限,但我看到了不同的行为,具体取决于 UNC 路径。下面是我用来授予权限的代码(来自 MSDN)以及每种情况下的结果, static void Gi
我是一名优秀的程序员,十分优秀!