簡體   English   中英

Discord.py - 根據頻道檢查用戶是否具有角色

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

所以現在我正在嘗試創建一個命令,該命令基本上創建了阻止特定用戶查看頻道的覆蓋。 我唯一遇到的問題是機器人驗證用戶是否“擁有”頻道的部分。 有一些頻道是玩家擁有的,它們的角色與頻道的名稱相匹配。

舉個例子:

#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 -> 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. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM