简体   繁体   English

Discord.py - 根据频道检查用户是否具有角色

[英]Discord.py - Checking if a user has a role based on the channel

so right now I'm trying to create a command which basically creates overrides that blocks a specific user from viewing the channel.所以现在我正在尝试创建一个命令,该命令基本上创建了阻止特定用户查看频道的覆盖。 The only thing I am having trouble with is the part where the bot verifies if the user "owns" the channel.我唯一遇到的问题是机器人验证用户是否“拥有”频道的部分。 There are a few channels which are player owned and they have a role that matches up with the channel's name.有一些频道是玩家拥有的,它们的角色与频道的名称相匹配。

So as an example:举个例子:

#space-invaders

@Space Invaders OP

The issue in the code is that while trying to convert the roles into string, it fails to do so.代码中的问题是,在尝试将角色转换为字符串时,它没有这样做。 So I need an alternative to this and I have no clue what else I could do.所以我需要一个替代方案,我不知道我还能做什么。

@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")

Any suggestions would greatly help!任何建议都会有很大帮助!

You would check if the role is in the author roles.您将检查该角色是否属于作者角色。

First: Change the channel name into the format of the role space-invaders -> Space Invaders OP .首先:将频道名称改为角色space-invaders -> Space Invaders OP的格式。 Which means replace , title and OP at the end这意味着replacetitleOP在最后

Secondly: Get the role from the guild using discord.utils.get and check if the author has it.其次:使用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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM