From 17f4c47ae21b92c40c11f56a5d18e64657ef92ee Mon Sep 17 00:00:00 2001 From: Learath Date: Tue, 13 Jun 2023 18:24:06 +0200 Subject: [PATCH] Add support for multiple categories --- cogs/map_testing/__init__.py | 16 ++++++++------- cogs/map_testing/map_channel.py | 36 ++++++++++++++++++++++----------- cogs/map_testing/submission.py | 4 ++-- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/cogs/map_testing/__init__.py b/cogs/map_testing/__init__.py index edcaa4d..1b20b88 100644 --- a/cogs/map_testing/__init__.py +++ b/cogs/map_testing/__init__.py @@ -1,4 +1,5 @@ import io +import itertools import logging import re from datetime import datetime, timedelta @@ -14,9 +15,10 @@ log = logging.getLogger(__name__) -CAT_MAP_TESTING = 449352010072850443 -CAT_WAITING_MAPPER = 746076708196843530 -CAT_EVALUATED_MAPS = 462954029643989003 +CAT_GROUP_MAP_TESTING = [449352010072850443, 1118205044353941554] +CAT_GROUP_WAITING_MAPPER = [746076708196843530] +CAT_GROUP_EVALUATED_MAPS = [462954029643989003] +CAT_GROUP_TESTING = itertools.chain(CAT_GROUP_MAP_TESTING, CAT_GROUP_WAITING_MAPPER, CAT_GROUP_EVALUATED_MAPS) CHAN_ANNOUNCEMENTS = 420565311863914496 CHAN_INFO = 455392314173554688 CHAN_SUBMIT_MAPS = 455392372663123989 @@ -28,7 +30,7 @@ def is_testing(channel: discord.TextChannel) -> bool: - return isinstance(channel, discord.TextChannel) and channel.category_id in (CAT_MAP_TESTING, CAT_WAITING_MAPPER, CAT_EVALUATED_MAPS) + return isinstance(channel, discord.TextChannel) and channel.category_id in CAT_GROUP_TESTING def is_staff(member: discord.Member) -> bool: return any(r.id in (ROLE_ADMIN, ROLE_TESTER) for r in member.roles) @@ -66,7 +68,7 @@ def cog_unload(self): async def load_map_channels(self): await self.bot.wait_until_ready() - for category_id in (CAT_MAP_TESTING, CAT_WAITING_MAPPER, CAT_EVALUATED_MAPS): + for category_id in CAT_GROUP_TESTING: category = self.bot.get_channel(category_id) for channel in category.text_channels: if channel.id in (CHAN_INFO, CHAN_SUBMIT_MAPS): @@ -372,7 +374,7 @@ async def auto_archive(self): query = 'SELECT channel_id FROM waiting_maps WHERE timestamp < CURRENT_TIMESTAMP - INTERVAL \'60 days\';' records = await self.bot.pool.fetch(query) - deleted_waiting_maps_ids = [r['channel_id'] for r in records] + deleted_waiting_maps_ids = [r['channel_id'] for r in records] to_archive = [] for map_channel in self.map_channels: @@ -390,7 +392,7 @@ async def auto_archive(self): continue to_archive.append(map_channel) - + for map_channel in to_archive: testlog = await TestLog.from_map_channel(map_channel) archived = await self.archive_testlog(testlog) diff --git a/cogs/map_testing/map_channel.py b/cogs/map_testing/map_channel.py index a48fe4f..ce8fcb7 100644 --- a/cogs/map_testing/map_channel.py +++ b/cogs/map_testing/map_channel.py @@ -1,16 +1,13 @@ import enum import re -from typing import List +from typing import List, Optional import discord from cogs.map_testing.submission import InitialSubmission from utils.text import human_join, sanitize -CAT_MAP_TESTING = 449352010072850443 -CAT_WAITING_MAPPER = 746076708196843530 -CAT_EVALUATED_MAPS = 462954029643989003 - +from . import CAT_GROUP_MAP_TESTING, CAT_GROUP_WAITING_MAPPER, CAT_GROUP_EVALUATED_MAPS class MapState(enum.Enum): TESTING = '' @@ -69,6 +66,18 @@ def preview_url(self) -> str: def topic(self) -> str: return '\n'.join((self.details, self.preview_url, self.mapper_mentions)) + + async def get_category_with_free_slot(self, cat_group: list[int]) -> discord.CategoryChannel: + for c in cat_group: + category = self.guild.get_channel(c) + if not isinstance(category, discord.CategoryChannel): + continue # Perhaps log this as this should be impossible + + if len(category.channels) < 50: + return category + + raise RuntimeError("Testing category full") + async def update(self, name: str=None, mappers: List[str]=None, server: str=None): prev_details = self.details @@ -89,21 +98,23 @@ async def set_state(self, *, state: MapState): if state == self.state: return - self.state = state if state is MapState.TESTING: - category_id = CAT_MAP_TESTING + cat_group_target = CAT_GROUP_MAP_TESTING elif state is MapState.WAITING: - category_id = CAT_WAITING_MAPPER + cat_group_target = CAT_GROUP_WAITING_MAPPER else: - category_id = CAT_EVALUATED_MAPS + cat_group_target = CAT_GROUP_EVALUATED_MAPS options = {'name': str(self)} - if category_id != self.category_id: - options['category'] = category = self.guild.get_channel(category_id) + if self.category_id not in cat_group_target: + options['category'] = category = await self.get_category_with_free_slot(cat_group_target) options['position'] = category.channels[-1].position + 1 if state is MapState.TESTING else 0 await self.edit(**options) + # Only change the state if nothing fails + self.state = state + @classmethod async def from_submission(cls, isubm: InitialSubmission, **options): self = cls.__new__(cls) @@ -112,5 +123,6 @@ async def from_submission(cls, isubm: InitialSubmission, **options): self.server = isubm.server self.state = MapState.TESTING self.mapper_mentions = isubm.author.mention - self._channel = await isubm.channel.category.create_text_channel(str(self), topic=self.topic, **options) + category = self.get_category_with_free_slot(CAT_GROUP_MAP_TESTING) + self._channel = await category.create_text_channel(str(self), topic=self.topic, **options) return self diff --git a/cogs/map_testing/submission.py b/cogs/map_testing/submission.py index 5f94e1a..f0f9f7c 100644 --- a/cogs/map_testing/submission.py +++ b/cogs/map_testing/submission.py @@ -4,7 +4,7 @@ import os import re from io import BytesIO -from typing import Optional +from typing import Optional, Tuple import discord @@ -94,7 +94,7 @@ async def debug_map(self) -> Optional[str]: return output - async def edit_map(self, *args: str) -> (str, Optional[discord.File]): + async def edit_map(self, *args: str) -> Tuple[str, Optional[discord.File]]: if "--mapdir" in args: return "Can't save as MapDir using the discord bot", None tmp = f'{self.DIR}/tmp/{self.message.id}.map'