未检查
我正在尝试实现一个白名单,让只有特定角色能够使用这个命令,并且它会把某个角色记录为“他们的”。我已经通过打印角色等信息来调试,现在连检查都没走到,好像它会打印出管理员表,但随后就不会进入到把数据添加到数据库/发送消息的检查,它什么都不做。
cogs/roles.py
import discord
from discord.ext import commands
from utils.databases import roles_db
from utils.variables import admin
class Roles(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_group()
async def custom(self, ctx: commands.Context):
return
@custom.command(name="register", description="Registers a custom role to a user")
async def register(self, ctx: commands.Context, role: discord.Role, member: discord.Member):
print(admin)
if any(r.id in admin for r in ctx.author.roles):
roles_db.insert_one({"user_id": member.id, "role_id": role.id})
await ctx.send(":white_check_mark: Role registered!")
else:
await ctx.send(":x: You do not have the required role.")
async def setup(bot):
await bot.add_cog(Roles(bot))
utils/variables.py
admin = [
1408442799925235794,
1445017368123277460
1079504634772738078
1272691594591473695
1379137477192843264
]
解决方案
问题:
- 在
utils/variables.py中的语法错误 - ID之间缺少逗号 - 在
print(admin)之后代码崩溃,永远无法进入到if的检查
修复 utils/variables.py:
admin = [
1408442799925235794,
1445017368123277460,
1079504634772738078,
1272691594591473695,
1379137477192843264
]
更新后的命令(带调试):
@custom.command(name="register", description="Registers a custom role to a user")
async def register(self, ctx: commands.Context, role: discord.Role, member: discord.Member):
print(f"Author roles: {[r.id for r in ctx.author.roles]}")
if any(r.id in admin for r in ctx.author.roles):
roles_db.insert_one({"user_id": member.id, "role_id": role.id})
await ctx.send(":white_check_mark: Role registered!")
else:
await ctx.send(":x: You do not have the required role.")
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。