Skip to content

Commit

Permalink
[Starboard] 2.5.5 Separate channel and role whitelist/blacklist more …
Browse files Browse the repository at this point in the history
…cleanly

This should fix #261 #266 #267 #269 and #276
  • Loading branch information
TrustyJAID committed Dec 30, 2021
1 parent 7f92b8d commit d463313
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion starboard/starboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Starboard(StarboardEvents, commands.Cog):
Create a starboard to *pin* those special comments indefinitely
"""

__version__ = "2.5.4"
__version__ = "2.5.5"
__author__ = "TrustyJAID"

def __init__(self, bot):
Expand Down
52 changes: 35 additions & 17 deletions starboard/starboard_entry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import asyncio
import discord
import logging
from dataclasses import dataclass
from typing import List, Dict, Optional, Union
from typing import Dict, List, Optional, Union

from redbot import version_info, VersionInfo
import discord
from redbot import VersionInfo, version_info
from redbot.core.bot import Red
from redbot.core.utils import AsyncIter

Expand Down Expand Up @@ -72,18 +73,25 @@ def check_roles(self, member: Union[discord.Member, discord.User]) -> bool:
# this will account for non-members reactions and still count
# for the starboard count
return True
user_roles = set([role.id for role in member.roles])
if self.whitelist:
for role in self.whitelist:
if role in user_roles:
guild = member.guild
whitelisted_roles = [
guild.get_role(rid).id for rid in self.whitelist if guild.get_role(rid) is not None
]
blacklisted_roles = [
guild.get_role(rid).id for rid in self.blacklist if guild.get_role(rid) is not None
]
if whitelisted_roles:
# only count if the whitelist contains actual roles
for role in whitelisted_roles:
if role in member.roles:
return True
return False
# Since we'd normally return True
# if there is a whitelist we want to ensure only whitelisted
# roles can starboard something
elif self.blacklist:
for role in self.blacklist:
if role in user_roles:
if blacklisted_roles:
for role in blacklisted_roles:
if role in member.roles:
return False

return True
Expand All @@ -109,18 +117,28 @@ def check_channel(self, bot: Red, channel: discord.TextChannel) -> bool:
guild = bot.get_guild(self.guild)
if channel.is_nsfw() and not guild.get_channel(self.channel).is_nsfw():
return False
if self.whitelist:
if channel.id in self.whitelist:
whitelisted_channels = [
guild.get_channel(cid).id
for cid in self.whitelist
if guild.get_channel(cid) is not None
]
blacklisted_channels = [
guild.get_channel(cid).id
for cid in self.blacklist
if guild.get_channel(cid) is not None
]
if whitelisted_channels:
if channel.id in whitelisted_channels:
return True
if channel.category_id and channel.category_id in self.whitelist:
if channel.category_id and channel.category_id in whitelisted_channels:
return True
return False
else:
if channel.id in self.blacklist:
if blacklisted_channels:
if channel.id in blacklisted_channels:
return False
if channel.category_id and channel.category_id in self.blacklist:
if channel.category_id and channel.category_id in blacklisted_channels:
return False
return True
return True

async def to_json(self) -> dict:
return {
Expand Down

0 comments on commit d463313

Please sign in to comment.