【发布时间】:2021-08-08 04:51:51
【问题描述】:
所以我正在设置一个机器人,并对其进行测试。我有一个小功能是踢,禁止和取消禁止用户,设置在一个 cog 中,如下所示:
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))
现在我已经用一些空格格式化了上面的代码,以便它适合一个代码块,所以如果你复制粘贴到别处,你可能会遇到缩进错误。
继续前进,机器人本身也具有管理员权限以及单独的踢和禁止权限。它也被放置在角色层次结构的顶部,被视为:
我的机器人的名字是 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 命令也出现类似错误。
有趣的是,作为所有者,我得到了这个错误,但假设另一个用户,我的一个朋友,他没有踢、禁止或消息管理角色,完美地运行他们的代码行,其中她从机器人那里得到消息,她无权踢、禁止或清除消息。附上截图。
我不知道我哪里出错了。请帮我调试一下。
【问题讨论】:
-
您的 kick 命令对我有效(作为我自己服务器的所有者)
-
您确定您使用的代码与我的完全相同吗?你的机器人层次结构和我的有点相似吗?您会分享这些详细信息,例如您授予机器人的哪些权限以及您作为所有者拥有哪些角色以及角色的层次结构?
-
我的机器人拥有管理员权限,我复制并粘贴了您的代码(
Moderator类和kick命令),根据需要修复了缩进。 -
澄清一下,您,服务器的所有者(具有所有权限)不能调用此命令。但是,没有任何权限的人可以使用命令吗? (我的意思是在机器人中使用 as 实际上确实做了它预期做的事情)
-
对,我是所有者,拥有所有权限,我无法调用此命令,但如果没有权限的人调用此命令,机器人会按照应有的方式响应。跨度>
标签: discord discord.py