Skip to content

Commit 488b683

Browse files
committedJul 31, 2023
Move plugins to use cog_load for startup
1 parent a9980ba commit 488b683

File tree

2 files changed

+7
-35
lines changed

2 files changed

+7
-35
lines changed
 

‎ban_appeals/ban_appeals.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from core import checks
1010
from core.models import PermissionLevel, getLogger
1111
from core.thread import Thread
12-
from .utils import async_tasks, get_or_fetch
12+
from .utils import get_or_fetch
1313

1414
PYDIS_NO_KICK_ROLE_IDS = (
1515
267627879762755584, # Owners in PyDis
@@ -41,12 +41,10 @@ def __init__(self, bot: ModmailBot):
4141

4242
self.db = self.bot.plugin_db.get_partition(self)
4343

44-
self.init_task = async_tasks.create_task(self.init_plugin(), self.bot.loop)
45-
4644
self.user_locks: weakref.WeakValueDictionary[int, asyncio.Lock] = weakref.WeakValueDictionary()
4745
self.ignore_next_remove_event: set[int] = set()
4846

49-
async def init_plugin(self) -> None:
47+
async def cog_load(self) -> None:
5048
"""Initialise the plugin's configuration."""
5149
self.pydis_guild = self.bot.guild
5250
self.appeals_guild = self.bot.get_guild(APPEAL_GUILD_ID)
@@ -58,10 +56,11 @@ async def init_plugin(self) -> None:
5856
self.logs_channel = discord.utils.get(self.appeals_guild.channels, name="logs")
5957

6058
log.info("Plugin loaded, checking if there are people to kick.")
61-
await self._sync_kicks()
59+
await asyncio.create_task(self._sync_kicks())
6260

6361
async def _sync_kicks(self) -> None:
6462
"""Iter through all members in appeals guild, kick them if they meet criteria."""
63+
await asyncio.sleep(5)
6564
for member in self.appeals_guild.members:
6665
await self._maybe_kick_user(member)
6766

@@ -127,8 +126,6 @@ async def _handle_join(self, member: discord.Member) -> None:
127126
If a member rejoins while appealing, it's notified in their
128127
thread.
129128
"""
130-
await self.init_task
131-
132129
if member.guild == self.pydis_guild:
133130
# Join event from PyDis
134131
# Kick them from appeals guild now they're back in PyDis
@@ -175,8 +172,6 @@ async def _handle_remove(self, member: discord.Member) -> None:
175172
176173
An embed is sent in the thread once they leave.
177174
"""
178-
await self.init_task
179-
180175
if not member.guild == self.appeals_guild:
181176
return
182177

@@ -230,7 +225,6 @@ async def appeal_category_management(self, ctx: commands.Context) -> None:
230225
@appeal_category_management.command(name="get")
231226
async def get_categories(self, ctx: commands.Context) -> None:
232227
"""Get the list of appeal categories of commands for managing appeal categories."""
233-
await self.init_task
234228
category_str = ", ".join(map(str, self.appeal_categories)) if self.appeal_categories else "None"
235229

236230
await ctx.send(f"Currently configured appeal categories are: {category_str}")
@@ -239,8 +233,6 @@ async def get_categories(self, ctx: commands.Context) -> None:
239233
@appeal_category_management.command(name="add")
240234
async def add_category(self, ctx: commands.Context, appeal_category: discord.CategoryChannel) -> None:
241235
"""Add a category to the list of ignored categories."""
242-
await self.init_task
243-
244236
if appeal_category.id in self.appeal_categories:
245237
await ctx.send(f":x: {appeal_category} already in the appeal category list.")
246238
return
@@ -258,8 +250,6 @@ async def add_category(self, ctx: commands.Context, appeal_category: discord.Cat
258250
@appeal_category_management.command(name="delete", aliases=("remove", "del", "rem"))
259251
async def del_category(self, ctx: commands.Context, category_to_remove: discord.CategoryChannel) -> None:
260252
"""Remove a category from the list of appeal categories."""
261-
await self.init_task
262-
263253
if category_to_remove.id not in self.appeal_categories:
264254
await ctx.send(f":x: {category_to_remove} isn't in the appeal categories list.")
265255
return
@@ -282,8 +272,6 @@ async def get_useable_appeal_category(self) -> t.Optional[discord.CategoryChanne
282272
@commands.Cog.listener()
283273
async def on_thread_ready(self, thread: Thread, *args) -> None:
284274
"""If the new thread is for an appeal, move it to the appeals category."""
285-
await self.init_task
286-
287275
if await self._is_banned_pydis(thread.recipient):
288276
category = await self.get_useable_appeal_category()
289277
if category:

‎ping_manager/ping_manager.py

+3-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from core import checks
1111
from core.models import PermissionLevel, getLogger
1212
from core.thread import Thread
13-
from .utils import async_tasks
1413

1514
# Remove view perms from this role while pining, so only on-duty mods get the ping.
1615
MOD_TEAM_ROLE_ID = 267629731250176001
@@ -49,9 +48,7 @@ def __init__(self, bot: ModmailBot):
4948
self.ping_tasks: list[PingTask] = None
5049
self.db = bot.api.get_plugin_partition(self)
5150

52-
self.init_task = async_tasks.create_task(self.init_plugin(), self.bot.loop)
53-
54-
async def init_plugin(self) -> None:
51+
async def cog_load(self) -> None:
5552
"""Fetch the current config from the db."""
5653
db_config = await self.db.find_one({"_id": "ping-delay-config"})
5754
db_config = db_config or {}
@@ -66,7 +63,7 @@ async def init_plugin(self) -> None:
6663
log.info("Loaded config: %s", self.config)
6764
log.info("Loaded %d ping tasks", len(self.ping_tasks))
6865
for task in self.ping_tasks:
69-
async_tasks.create_task(self.maybe_ping_later(task), self.bot.loop)
66+
asyncio.create_task(self.maybe_ping_later(task))
7067

7168
@commands.group(invoke_without_command=True)
7269
@checks.has_permissions(PermissionLevel.SUPPORTER)
@@ -84,8 +81,6 @@ async def set_delay(self, ctx: commands.Context) -> None:
8481
@checks.has_permissions(PermissionLevel.OWNER)
8582
async def set_initial(self, ctx: commands.Context, wait_duration: int) -> None:
8683
"""Set the number of seconds to wait after a thread is opened to ping."""
87-
await self.init_task
88-
8984
await self.db.find_one_and_update(
9085
{"_id": "ping-delay-config"},
9186
{"$set": {"initial_wait_duration": wait_duration}},
@@ -98,8 +93,6 @@ async def set_initial(self, ctx: commands.Context, wait_duration: int) -> None:
9893
@checks.has_permissions(PermissionLevel.OWNER)
9994
async def set_delayed(self, ctx: commands.Context, wait_duration: int) -> None:
10095
"""Set the number of seconds to wait after a thread is opened to ping."""
101-
await self.init_task
102-
10396
await self.db.find_one_and_update(
10497
{"_id": "ping-delay-config"},
10598
{"$set": {"delayed_wait_duration": wait_duration}},
@@ -127,8 +120,6 @@ async def ping_string(self, ctx: commands.Context) -> None:
127120
@ping_string.command(name="set")
128121
async def set_ping(self, ctx: commands.Context, ping_string: str) -> None:
129122
"""Set what to send after a waiting for a thread to be responded to."""
130-
await self.init_task
131-
132123
await self.db.find_one_and_update(
133124
{"_id": "ping-delay-config"},
134125
{"$set": {"ping_string": ping_string}},
@@ -153,8 +144,6 @@ async def ping_ignore_categories(self, ctx: commands.Context) -> None:
153144
@ping_ignore_categories.command(name="add", aliases=("set",))
154145
async def set_category(self, ctx: commands.Context, category_to_ignore: discord.CategoryChannel) -> None:
155146
"""Add a category to the list of ignored categories."""
156-
await self.init_task
157-
158147
if category_to_ignore.id in self.config.ignored_categories:
159148
await ctx.send(f":x: {category_to_ignore} already in the ignored categories.")
160149
return
@@ -172,8 +161,6 @@ async def set_category(self, ctx: commands.Context, category_to_ignore: discord.
172161
@ping_ignore_categories.command(name="get")
173162
async def get_category(self, ctx: commands.Context) -> None:
174163
"""Get the list of ignored categories."""
175-
await self.init_task
176-
177164
if not self.config.ignored_categories:
178165
await ctx.send("There are currently no ignored categories.")
179166
return
@@ -185,8 +172,6 @@ async def get_category(self, ctx: commands.Context) -> None:
185172
@ping_ignore_categories.command(name="delete", aliases=("remove", "del", "rem"))
186173
async def del_category(self, ctx: commands.Context, category_to_ignore: discord.CategoryChannel) -> None:
187174
"""Remove a category from the list of ignored categories."""
188-
await self.init_task
189-
190175
if category_to_ignore.id not in self.config.ignored_categories:
191176
await ctx.send(f":x: {category_to_ignore} isn't in the ignored categories list.")
192177
return
@@ -208,7 +193,7 @@ async def add_ping_task(self, task: PingTask) -> None:
208193
upsert=True,
209194
)
210195

211-
async_tasks.create_task(self.maybe_ping_later(task), self.bot.loop)
196+
asyncio.create_task(self.maybe_ping_later(task))
212197

213198
async def remove_ping_task(self, task: PingTask) -> None:
214199
"""Removes a ping task to the internal cache and to the db."""
@@ -289,7 +274,6 @@ async def maybe_ping_later(self, ping_task: PingTask) -> None:
289274
@commands.Cog.listener()
290275
async def on_thread_ready(self, thread: Thread, *args) -> None:
291276
"""Schedule a task to check if the bot should ping in the thread after the defined wait duration."""
292-
await self.init_task
293277
now = datetime.utcnow()
294278
ping_task = PingTask(
295279
when_to_ping=(now + timedelta(seconds=self.config.initial_wait_duration)).isoformat(),

0 commit comments

Comments
 (0)
Failed to load comments.