Skip to content

Commit

Permalink
lockdown.py: Try to avoid race condition when trying to lock the same…
Browse files Browse the repository at this point in the history
… channel when lockdown_channels is already running
  • Loading branch information
FrozenChen committed Jul 8, 2022
1 parent 771e0a5 commit 0e0a4f5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cogs/lockdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, bot: Kurisu):
self.bot: Kurisu = bot
self.emoji = discord.PartialEmoji.from_str('🔒')
self.configuration = self.bot.configuration
self.locking_down = False

async def cog_check(self, ctx: KurisuContext):
if ctx.guild is None:
Expand Down Expand Up @@ -80,6 +81,11 @@ async def lockdown_channels(self, ctx: GuildContext, *, channels: list[Union[dis
async def lockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[discord.TextChannel, discord.VoiceChannel]]):
"""Lock message sending in the channel. Staff only."""

if self.locking_down:
return await ctx.send("The bot is already locking down channels.")

self.locking_down = True

author = ctx.author

if not channels:
Expand All @@ -91,6 +97,7 @@ async def lockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[disc
locked_down = await self.lockdown_channels(ctx, channels=channels, level=2, top_role=self.bot.roles['Staff'],
message="🔒 Channel locked down. Only staff members may speak."
" Do not bring the topic to other channels or risk disciplinary actions.")
self.locking_down = False

if locked_down:
msg = f"🔒 **Lockdown**: {ctx.author.mention} locked down channels | {author}\n📝 __Channels__: {', '.join(c.mention for c in locked_down)}"
Expand All @@ -101,6 +108,10 @@ async def lockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[disc
async def slockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[discord.TextChannel, discord.VoiceChannel]]):
"""Lock message sending in the channel for everyone. Owners only."""

if self.locking_down:
return await ctx.send("The bot is already locking down channels.")
self.locking_down = True

author = ctx.author

if not channels:
Expand All @@ -112,6 +123,7 @@ async def slockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[dis
locked_down = await self.lockdown_channels(ctx, channels=channels, level=3, top_role=self.bot.roles['Owner'],
message="🔒 Channel locked down. Only the server Owners may speak."
" Do not bring the topic to other channels or risk disciplinary actions.")
self.locking_down = False

if locked_down:
msg = f"🔒 **Lockdown**: {ctx.author.mention} locked down channels | {author}\n📝 __Channels__: {', '.join(c.mention for c in locked_down)}"
Expand All @@ -122,6 +134,11 @@ async def slockdown(self, ctx: GuildContext, channels: commands.Greedy[Union[dis
async def softlock(self, ctx: GuildContext, channels: commands.Greedy[Union[discord.TextChannel, discord.VoiceChannel]]):
"""Lock message sending in the channel, without the "disciplinary action" note. Staff and Helpers only."""

if self.locking_down:
return await ctx.send("The bot is already locking down channels.")

self.locking_down = True

author = ctx.author

if not channels:
Expand All @@ -138,6 +155,7 @@ async def softlock(self, ctx: GuildContext, channels: commands.Greedy[Union[disc
role = self.bot.roles['Helpers'] if is_helper else self.bot.roles['Staff']

locked_down = await self.lockdown_channels(ctx, channels=channels, level=1, top_role=role)
self.locking_down = False

if locked_down:
msg = f"🔒 **Lockdown**: {ctx.author.mention} locked down channels | {author}\n📝 __Channels__: {', '.join(c.mention for c in locked_down)}"
Expand Down

0 comments on commit 0e0a4f5

Please sign in to comment.