Skip to content

Commit

Permalink
feat(timeout): query to purge channel on removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tigattack committed Oct 12, 2022
1 parent ee73101 commit df8b43c
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions timeout/timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import discord
from redbot.core import Config, checks, commands
from redbot.core.utils.menus import start_adding_reactions
from redbot.core.utils.mod import is_mod_or_superior as is_mod
from redbot.core.utils.predicates import ReactionPredicate


class Timeout(commands.Cog):
Expand All @@ -13,7 +15,8 @@ def __init__(self):
default_guild = {
"logchannel": None,
"report": False,
"timeoutrole": None
"timeoutrole": None,
"timeout_channel": None
}
self.config.register_guild(**default_guild)
self.config.register_member(
Expand Down Expand Up @@ -105,6 +108,11 @@ async def timeout_add(self, ctx: commands.Context, user: discord.Member, reason:

async def timeout_remove(self, ctx: commands.Context, user: discord.Member, reason: str):
"""Remove user from timeout"""

# Retrieve timeout channel
timeout_channel_config = await self.config.guild(ctx.guild).timeout_channel()
timeout_channel = ctx.guild.get_channel(timeout_channel_config)

# Fetch and define user's previous roles.
user_roles = []
for role in await self.config.member(user).roles():
Expand Down Expand Up @@ -132,6 +140,17 @@ async def timeout_remove(self, ctx: commands.Context, user: discord.Member, reas
}
await self.report_handler(ctx, user, action_info)

# Ask if user wishes to clear the timeout channel if they've defined one
if timeout_channel:
archive_query = await ctx.send(f"Do you wish to clear the contents of {timeout_channel.mention}?")
start_adding_reactions(archive_query, ReactionPredicate.YES_OR_NO_EMOJIS)

pred = ReactionPredicate.yes_or_no(archive_query, ctx.author)
await ctx.bot.wait_for("reaction_add", check=pred)
if pred.result is True:
purge = await timeout_channel.purge(bulk=True)
await ctx.send(f"Cleared {len(purge)} messages from {timeout_channel.mention}.")

# Commands

@commands.guild_only()
Expand Down Expand Up @@ -160,7 +179,7 @@ async def timeoutset_report(self, ctx: commands.Context, choice: str):
These reports will be sent to the configured log channel as an embed.
The embed will specify the user's details and the moderator who executed the command.
Set log channel with `[p]timeoutset logchannel`.
Set log channel with `[p]timeoutset logchannel` before enabling reporting.
Example:
- `[p]timeoutset report enable`
Expand Down Expand Up @@ -198,6 +217,19 @@ async def timeoutset_role(self, ctx: commands.Context, role: discord.Role):
await self.config.guild(ctx.guild).timeoutrole.set(role.id)
await ctx.tick()

@timeoutset.command(name="timeoutchannel")
@checks.mod()
async def timeoutset_timeout_channel(self, ctx: commands.Context, channel: discord.TextChannel):
"""Set the timeout channel.
This is required if you wish to optionaly purge the channel upon removing a user from timeout.
Example:
- `[p]timeoutset timeoutchannel #timeout`
"""
await self.config.guild(ctx.guild).timeout_channel.set(channel.id)
await ctx.tick()

@timeoutset.command(name="list")
@checks.mod()
async def timeoutset_list(self, ctx: commands.Context):
Expand All @@ -206,6 +238,7 @@ async def timeoutset_list(self, ctx: commands.Context):
log_channel = await self.config.guild(ctx.guild).logchannel()
report = await self.config.guild(ctx.guild).report()
timeout_role = ctx.guild.get_role(await self.config.guild(ctx.guild).timeoutrole())
timeout_channel = await self.config.guild(ctx.guild).timeout_channel()

if log_channel:
log_channel = f"<#{log_channel}>"
Expand All @@ -222,6 +255,11 @@ async def timeoutset_list(self, ctx: commands.Context):
else:
report = "Disabled"

if timeout_channel:
timeout_channel = f"<#{timeout_channel}>"
else:
timeout_channel = "Unconfigured"

# Build embed
embed = discord.Embed(
color=(await ctx.embed_colour())
Expand All @@ -245,6 +283,11 @@ async def timeoutset_list(self, ctx: commands.Context):
value=timeout_role,
inline=True
)
embed.add_field(
name="Timeout Channel",
value=timeout_channel,
inline=True
)

# Send embed
await ctx.send(embed=embed)
Expand Down

0 comments on commit df8b43c

Please sign in to comment.