Discord.py-检查用户是否具有基于频道的角色



所以现在我正在尝试创建一个命令,该命令基本上会创建覆盖,阻止特定用户查看频道。我唯一遇到的问题是机器人验证用户";拥有";频道。有一些频道是玩家所有的,他们的角色与频道名称相匹配。

举个例子:

#space-invaders
@Space Invaders OP

代码中的问题是,当试图将角色转换为字符串时,它失败了。所以我需要一个替代方案,我不知道还能做什么。

@commands.command()
@commands.has_role("Realm OP")
async def block(self, ctx, user: discord.User):
#channel = await ctx.author.create_dm()
channel = ctx.message.channel
author = ctx.message.author
mentions = [role.mention for role in ctx.message.author.roles if role.mentionable]
channel2 = str(channel.name)
channel = channel2.split('-')
if len(channel2) == 2: # #real-emoji
realm, emoji = channel
else: # #realm-name-emoji  
realm, emoji = channel[0], channel[-1]
realmName = realm.replace("-" , " ")
realmName1 = realmName.lower()
rolelist = []
authorRoles = discord.Role.name(author.roles) # Issue here
for role in authorRoles:
rolen = role.lower()
rolelist.append(rolen.mention)
if realmName1 in rolelist:
await ctx.send("true")
else:
await ctx.send("false")

任何建议都将大有帮助!

您需要检查角色是否在作者角色中。

第一:将频道名称更改为角色space-invaders->CCD_ 2。这意味着末端的replacetitleOP

第二:使用discord.utils.get从公会获取角色,并检查作者是否拥有。

@commands.command()
@commands.has_role("Realm OP")
async def block(ctx, user: discord.User):
check_name = f'{ctx.channel.name.replace("-", " ").title()} OP'
check_role = discord.utils.get(ctx.guild.roles, name= check_name)
if check_role not in ctx.author.roles:
return await ctx.send('You do not own this channel')

# code here. 

最新更新