【问题标题】:Discord.py - Checking if a user has a role based on the channelDiscord.py - 根据频道检查用户是否具有角色
【发布时间】:2020-11-29 12:17:02
【问题描述】:

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

举个例子:

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

任何建议都会有很大帮助!

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您将检查该角色是否属于作者角色。

    首先:将频道名称改为角色space-invaders->Space Invaders OP的格式。这意味着最后是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. 
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 2021-03-30
      • 2019-04-04
      • 2021-04-08
      • 2021-03-31
      • 2010-12-22
      • 2019-06-16
      相关资源
      最近更新 更多