Skip to content

Commit

Permalink
Automatic thread archival improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed Dec 18, 2023
1 parent 95675c6 commit f147039
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 8 additions & 5 deletions components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


@component.with_schedule
@tanjun.as_time_schedule(minutes=5)
@tanjun.as_interval(300)
async def TaskArchiveThreads(
config: Dict[str, Any] = tanjun.inject(type=Dict[str, Any]),
bot: GatewayBot = tanjun.inject(type=GatewayBot),
Expand All @@ -54,10 +54,13 @@ async def TaskArchiveThreads(
continue
elif Utility.Elapsed(datetime.now(), thread.created_at) < lifetime:
continue

for role in config["archiveThreads"].get("immuneRoles", []):
if await Utility.UserHasRole(thread.owner_id, role, thread.guild_id, bot):
continue
elif await Utility.UserHasRole(
thread.owner_id,
config["archiveThreads"]["immuneRoles"],
thread.guild_id,
bot,
):
continue

await bot.rest.edit_channel(thread.id, archived=True)

Expand Down
17 changes: 12 additions & 5 deletions helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,22 @@ def FindNumbers(
return results

async def UserHasRole(
userId: int, roleId: int, serverId: int, bot: GatewayBot
userId: int, roleIds: Union[int, List[int]], serverId: int, bot: GatewayBot
) -> bool:
"""
Return a boolean value indicating whether or not a server
member has the specified role.
member has a specified role.
If an array of role IDs is provided, return True upon first
successful match.
"""

user: Optional[Member] = None

# Accept both a singular int or array of integers.
if isinstance(roleIds, int):
roleIds = [roleIds]

try:
user = await bot.rest.fetch_member(serverId, userId)
except NotFoundError as e:
Expand All @@ -135,15 +142,15 @@ async def UserHasRole(

if user:
for role in user.role_ids:
if int(role) == roleId:
if (current := int(role)) in roleIds:
logger.debug(
f"{Responses.ExpandUser(user.user, False)} has role {roleId} in server {serverId}"
f"{Responses.ExpandUser(user.user, False)} has role {current} in server {serverId}"
)

return True

logger.debug(
f"{Responses.ExpandUser(user.user, False)} does not have role {roleId} in server {serverId}"
f"{Responses.ExpandUser(user.user, False)} does not have role {current} in server {serverId}"
)

return False

0 comments on commit f147039

Please sign in to comment.