예제 #1
0
    def predicate(role, rank):
      role = get_guild_role(guild, role)

      if isinstance(role, discord.Role) and role.name != "@everyone":
        return {"rank": rank, "id": role.id, "name": role.name}
      else:
        return None
예제 #2
0
        def predicate(role):
            role = get_guild_role(guild, role)

            if role:
                return [role.id, role.name]
            else:
                return None
예제 #3
0
    async def remove_role(self, ctx, *roles):
        try:
            assert len(roles) > 0

            roles = list(
                filter(lambda r: r is not None,
                       [get_guild_role(ctx.guild, role) for role in roles]))
            excp = [
                role for role in guild_config(ctx.bot.db, ctx.guild.id).get(
                    "exceptions", [])
            ] + [role.id for role in roles]
            guild_config(ctx.bot.db, ctx.guild.id,
                         {"exceptions": list(set(excp))})

        except Exception:
            await try_react(ctx, "❗")
            print_exc()

        else:
            await try_react(ctx, "✅")
예제 #4
0
    async def unallow_role(self, ctx, *roles):
        try:
            assert len(roles) > 0

            roles = list(
                filter(lambda r: r is not None,
                       [get_guild_role(ctx.guild, role) for role in roles]))
            allw = [
                role
                for role in guild_config(ctx.bot.db, ctx.guild.id)["allowed"]
                if role not in [role.id for role in roles]
            ]
            guild_config(ctx.bot.db, ctx.guild.id, {"allowed": allw})

        except Exception:
            await try_react(ctx, "❗")
            print_exc()

        else:
            await try_react(ctx, "✅")
예제 #5
0
  async def rank(self, ctx, *, guild_id: int=0):
    if await ctx.bot.is_owner(ctx.author):
      try:
        guild = ctx.bot.get_guild(guild_id) or ctx.guild
        assert isinstance(guild, discord.Guild)

      except Exception:
        await try_delete(ctx.message)
        print_exc()
        return

    else:
      if not (ctx.guild and has_permissions(ctx)):
        return

      guild = ctx.guild

    roles = []
    config = {}

    for role in guild_config(ctx.bot.db, guild.id).get("roles", []):
      rank = 0

      for member in guild.members:
        if get_guild_role(guild, role) in member.roles:
          rank += 1

      config[role] = rank

    def predicate(role, rank):
      role = get_guild_role(guild, role)

      if isinstance(role, discord.Role) and role.name != "@everyone":
        return {"rank": rank, "id": role.id, "name": role.name}
      else:
        return None

    for key, val in config.items():
      role = predicate(key, val)
      if role:
        roles += [role]

    roles = json.dumps(sorted(roles, key=itemgetter("rank"), reverse=True), sort_keys=True, indent=2)

    # Beautify and condense decoded dictionary further
    roles = re.sub(r'\,\s*\n\s*"name"\,', ', "name"', roles)
    roles = re.sub(r'\}\,\n\s+\{', '}, {', roles)

    if len(f"```json\n{roles}\n```") < CHAR_LIMIT:
      try:
        await ctx.send("```json\n" + roles + "\n```")
      except (discord.Forbidden, discord.HTTPException):
        print_exc()

    else:
      with tempfile.TemporaryFile(mode="w+", encoding="utf-8") as fp:
        fp.write(roles)
        fp.seek(0)

        try:
          await ctx.send(file=discord.File(fp, filename="rank.json"))
        except (discord.Forbidden, discord.HTTPException):
          print_exc()