Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple categories #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions cogs/map_testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import itertools
import logging
import re
from datetime import datetime, timedelta
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
36 changes: 24 additions & 12 deletions cogs/map_testing/map_channel.py
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
4 changes: 2 additions & 2 deletions cogs/map_testing/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import re
from io import BytesIO
from typing import Optional
from typing import Optional, Tuple

import discord

Expand Down Expand Up @@ -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'
Expand Down