Purge messages command by basil-squared · Pull Request #110 · allthingslinux/tux · GitHub
Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ Tux is a Discord bot for the All Things Linux Discord server. It is designed to
```bash
cp .env.example .env
```
7. Review all useful CLI commands by visiting the [useful CLI commands](docs/CLI.md) file.
7. Run {prefix}sync in the server to enable slash commands.

8. Review all useful CLI commands by visiting the [useful CLI commands](docs/CLI.md) file.


## License
Expand Down
37 changes: 37 additions & 0 deletions config/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
Comment thread
basil-squared marked this conversation as resolved.
"Permissions": {
"Owner": 1172248516370894869,
"Admin": [
1172248516370894869,
1182064847052091484
],
"Mod": [
1172248516370894869,
1182064847052091484,
1172333934051336264
],
"Jr_Mod": [
1172248516370894869,
1182064847052091484,
1172333934051336264,
1172643385425793156
],
"Testing": [
1223693346699214999,
1224170906847416430
]
},
"Feature_Permissions": {
"load": "Admin",
"unload": "Admin",
"sync": "Admin",
"clear": "Admin",
"reload": "Admin",
"ban": "Mod",
"kick": "Jr_Mod",
"ping": "Everyone",
"server": "Everyone",
"rolecount": "Everyone",
"help": "Everyone"
}
}
File renamed without changes.
107 changes: 107 additions & 0 deletions tux/cogs/moderation/purge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import json
import os
from pathlib import Path

import discord
from discord import app_commands
from discord.ext import commands
from loguru import logger

from tux.utils.constants import Constants as CONST

config_file = Path("config/settings.json")
config = json.loads(config_file.read_text())
role_ids = CONST.USER_IDS
testing_role_id = role_ids["TESTING"] if os.getenv("STAGING") == "True" else "foobar"


class Purge(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

async def send_embed(
Comment thread
basil-squared marked this conversation as resolved.
self,
interaction: discord.Interaction,
title: str,
description: str,
color: discord.Colour,
error_info: str | None = None,
) -> None:
embed = discord.Embed(
title=title, description=description, color=color, timestamp=interaction.created_at
)
if error_info:
embed.add_field(name="Error Details", value=f"`{error_info}`", inline=False)
embed.set_footer(
text=f"Requested by {interaction.user.display_name}",
icon_url=interaction.user.display_avatar.url,
)

await interaction.followup.send(embed=embed) # Send the embed to the interaction

@app_commands.checks.has_any_role(
role_ids["ADMIN"],
role_ids["OWNER"],
role_ids["MOD"],
role_ids["JR MOD"],
*testing_role_id,
)
@app_commands.command(
name="purge", description="Deletes a set number of messages in a channel."
)
@app_commands.describe(number_messages="The number of messages to be purged.")
async def purge_messages(
self, interaction: discord.Interaction, number_messages: int = 10
) -> None:
if not interaction.channel or interaction.channel.type != discord.ChannelType.text:
return None

if number_messages <= 0:
await interaction.response.defer(ephemeral=True)
return await self.send_embed(
Comment thread
basil-squared marked this conversation as resolved.
interaction,
"Error",
"The number of messages to purge must be greater than 0.",
discord.Colour.red(),
)

embed = discord.Embed

try:
await interaction.response.defer(ephemeral=True)
await interaction.edit_original_response(content="Purging messages...")

deleted = await interaction.channel.purge(limit=number_messages)
Comment thread
basil-squared marked this conversation as resolved.
description = f"Deleted {len(deleted)} messages in {interaction.channel.mention}"

await self.send_embed(interaction, "Success!", description, discord.Colour.blue())

logger.info(
f"{interaction.user} purged {len(deleted)} messages from {interaction.channel.name}"
)

except discord.Forbidden as e:
logger.error(f"Failed to purge messages in {interaction.channel.name}: {e}")

await self.send_embed(
interaction,
"Permission Denied",
"Failed to purge messages due to insufficient permissions.",
discord.Colour.red(),
error_info=str(e),
)
embed.timestamp = interaction.created_at # Add timestamp to the error embed

except discord.HTTPException as e:
logger.error(f"Failed to purge messages in {interaction.channel.name}: {e}")

await self.send_embed(
interaction,
"Error",
f"An error occurred while purging messages: {e}",
discord.Colour.red(),
)


async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Purge(bot))
14 changes: 13 additions & 1 deletion tux/utils/constants.py