Skip to content

Commit 54469b3

Browse files
committed
refactor(mail.py, random.py, export.py): simplify function definitions and calls for better readability
feat(random.py): add hybrid_group for random generation commands to improve command organization style(random.py): update custom responses in eight_ball function for consistency fix(random.py): add guild_only decorator to random command group to restrict usage to guild channels
1 parent bd1f57f commit 54469b3

File tree

3 files changed

+47
-67
lines changed

3 files changed

+47
-67
lines changed

tux/cogs/admin/mail.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class Mail(commands.Cog):
13-
def __init__(self, bot: commands.Bot):
13+
def __init__(self, bot: commands.Bot) -> None:
1414
self.bot = bot
1515
self.api_url = CONST.MAILCOW_API_URL
1616
self.headers = {
@@ -35,9 +35,7 @@ def __init__(self, bot: commands.Bot):
3535

3636
@mail.command(name="register", description="Registers a user for mail.")
3737
@app_commands.checks.has_any_role("Root", "Admin", "Mod")
38-
async def register(
39-
self, interaction: discord.Interaction, member: discord.Member, username: str
40-
) -> None:
38+
async def register(self, interaction: discord.Interaction, member: discord.Member, username: str) -> None:
4139
if not username.isalnum():
4240
await interaction.response.send_message(
4341
"Username must be alphanumeric and contain no spaces.", ephemeral=True
@@ -63,11 +61,7 @@ async def register(
6361
mailbox_data["password2"] = password
6462

6563
# Ensure tags are copied correctly and member ID is added
66-
tags = (
67-
self.default_options["tags"]
68-
if isinstance(self.default_options["tags"], list)
69-
else []
70-
)
64+
tags = self.default_options["tags"] if isinstance(self.default_options["tags"], list) else []
7165
tags = tags.copy() # Ensure it's a fresh copy of the list
7266
tags.append(str(member.id))
7367
mailbox_data["tags"] = tags
@@ -77,9 +71,7 @@ async def register(
7771

7872
async with httpx.AsyncClient(timeout=10.0) as client:
7973
try:
80-
response = await client.post(
81-
api_endpoint, headers=self.headers, json=mailbox_data
82-
)
74+
response = await client.post(api_endpoint, headers=self.headers, json=mailbox_data)
8375
if response.status_code == 200:
8476
result = response.json()
8577
logger.info(f"Response JSON: {result}")
@@ -149,5 +141,5 @@ async def register(
149141
)
150142

151143

152-
async def setup(bot: commands.Bot):
144+
async def setup(bot: commands.Bot) -> None:
153145
await bot.add_cog(Mail(bot))

tux/cogs/fun/random.py

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import random
22

3-
import discord
43
from discord import app_commands
54
from discord.ext import commands
6-
from loguru import logger
75

86
from tux.utils.embeds import EmbedCreator
97

@@ -12,35 +10,39 @@ class Random(commands.Cog):
1210
def __init__(self, bot: commands.Bot) -> None:
1311
self.bot = bot
1412

15-
@app_commands.command(name="coinflip", description="Flip a coin.")
16-
async def coinflip(self, interaction: discord.Interaction) -> None:
13+
@commands.hybrid_group(name="random", description="Random generation commands.")
14+
@commands.guild_only()
15+
async def random(self, ctx: commands.Context[commands.Bot]) -> None:
16+
if ctx.invoked_subcommand is None:
17+
await ctx.send_help("random")
18+
19+
@random.command(name="coinflip", description="Flip a coin.")
20+
async def coinflip(self, ctx: commands.Context[commands.Bot]) -> None:
1721
"""
1822
Flip a coin.
1923
2024
Parameters
2125
----------
22-
interaction : discord.Interaction
23-
The discord interaction object.
26+
ctx : commands.Context[commands.Bot]
27+
The context object for the command.
2428
"""
25-
await interaction.response.send_message(
26-
content="You got heads!" if random.choice([True, False]) else "You got tails!"
27-
)
2829

29-
logger.info(f"{interaction.user} used the coinflip command in {interaction.channel}.")
30+
await ctx.send(content="You got heads!" if random.choice([True, False]) else "You got tails!")
3031

31-
@app_commands.command(name="8ball", description="Ask the magic 8ball a question.")
32+
@random.command(name="8ball", description="Ask the magic 8ball a question.")
3233
@app_commands.describe(question="The question to ask the 8ball.")
33-
async def eight_ball(self, interaction: discord.Interaction, question: str) -> None:
34+
async def eight_ball(self, ctx: commands.Context[commands.Bot], *, question: str) -> None:
3435
"""
3536
Ask the magic 8ball a question.
3637
3738
Parameters
3839
----------
39-
interaction : discord.Interaction
40-
The discord interaction object.
40+
ctx : commands.Context[commands.Bot]
41+
The context object for the command.
4142
question : str
4243
The question to ask the 8ball.
4344
"""
45+
4446
responses = [
4547
# Standard responses
4648
"It is certain",
@@ -61,22 +63,25 @@ async def eight_ball(self, interaction: discord.Interaction, question: str) -> N
6163
"Don't count on it",
6264
"My reply is no",
6365
"My sources say no",
66+
"Probably",
6467
"Outlook not so good",
6568
"Very doubtful",
66-
# Custom responses
6769
"Why the hell are you asking me lmao",
68-
"what???",
69-
"hell yeah",
70-
"hell no",
70+
"What???",
71+
"Hell yeah",
72+
"Hell no",
7173
"When pigs fly",
7274
"Ask someone else for once, I'm sick and tired of answering your questions you absolute buffoon.",
7375
"I dont know, ask me later",
76+
"I'm not sure",
77+
"Ask your mom",
78+
"Ask Puffy or Beastie",
79+
"Absolutely",
7480
]
75-
7681
choice = random.choice(responses)
7782

7883
response = f"""Response to "{question}":
79-
{"_" * len(choice)}
84+
{"_" * len(choice)}
8085
< {choice} >
8186
{"-" * len(choice)}
8287
\\ ^__^
@@ -85,73 +90,58 @@ async def eight_ball(self, interaction: discord.Interaction, question: str) -> N
8590
||----w |
8691
|| ||
8792
"""
93+
await ctx.reply(content=f"```{response}```")
8894

89-
await interaction.response.send_message(content=f"```{response}```")
90-
91-
logger.info(f"{interaction.user} used the 8ball command in {interaction.channel}.")
92-
93-
@app_commands.command(name="dice", description="Roll a dice.")
95+
@random.command(name="dice", description="Roll a dice.")
9496
@app_commands.describe(sides="The number of sides on the dice. (default: 6)")
95-
async def dice(self, interaction: discord.Interaction, sides: int = 6) -> None:
97+
async def dice(self, ctx: commands.Context[commands.Bot], sides: int = 6) -> None:
9698
"""
9799
Roll a dice.
98100
99101
Parameters
100102
----------
101-
interaction : discord.Interaction
102-
The discord interaction object.
103+
ctx : commands.Context[commands.Bot]
104+
The context object for the command.
103105
sides : int, optional
104106
The number of sides on the dice, by default 6.
105107
"""
108+
106109
if sides < 2:
107-
await interaction.response.send_message(content="The dice must have at least 2 sides.")
110+
await ctx.reply(content="The dice must have at least 2 sides.", ephemeral=True)
108111
return
109112

110113
embed = EmbedCreator.create_info_embed(
111114
title=f"Dice Roll (D{sides})",
112115
description=f"You rolled a {random.randint(1, sides)}!",
113-
interaction=interaction,
116+
ctx=ctx,
114117
)
115118

116-
await interaction.response.send_message(embed=embed)
119+
await ctx.reply(embed=embed)
117120

118-
logger.info(f"{interaction.user} used the dice command in {interaction.channel}.")
119-
120-
@app_commands.command(name="randomnumber", description="Generate a random number.")
121+
@random.command(name="number", description="Generate a random number.")
121122
@app_commands.describe(
122123
minimum="The minimum value of the random number. (default: 0)",
123124
maximum="The maximum value of the random number. (default: 100)",
124125
)
125-
async def random_number(
126-
self, interaction: discord.Interaction, minimum: int = 0, maximum: int = 100
127-
) -> None:
126+
async def random_number(self, ctx: commands.Context[commands.Bot], minimum: int = 0, maximum: int = 100) -> None:
128127
"""
129128
Generate a random number.
130129
131130
Parameters
132131
----------
133-
interaction : discord.Interaction
134-
The discord interaction object.
132+
ctx : commands.Context[commands.Bot]
133+
The context object for the command.
135134
minimum : int, optional
136135
The minimum value of the random number, by default 0.
137136
maximum : int, optional
138137
The maximum value of the random number, by default 100.
139138
"""
139+
140140
if minimum > maximum:
141-
await interaction.response.send_message(
142-
content="The minimum value must be less than the maximum value."
143-
)
141+
await ctx.reply(content="The minimum value must be less than the maximum value.", ephemeral=True)
144142
return
145143

146-
embed = EmbedCreator.create_info_embed(
147-
title="Random Number",
148-
description=f"Your random number is: {random.randint(minimum, maximum)}",
149-
interaction=interaction,
150-
)
151-
152-
await interaction.response.send_message(embed=embed)
153-
154-
logger.info(f"{interaction.user} used the randomnumber command in {interaction.channel}.")
144+
await ctx.reply(content=f"Your random number is: {random.randint(minimum, maximum)}")
155145

156146

157147
async def setup(bot: commands.Bot) -> None:

tux/cogs/guild/export.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ async def export_banned(
3838
return await interaction.response.send_message(embed=embed)
3939

4040
if flags and "--help" not in flags:
41-
file = await exports.get_ban_list_csv(
42-
interaction, bans, valid_flags, flags.split(sep=" ") if flags else []
43-
)
41+
file = await exports.get_ban_list_csv(interaction, bans, valid_flags, flags.split(sep=" ") if flags else [])
4442
return await interaction.response.send_message(file=file)
4543

4644
title = f"Total Bans in {interaction.guild}: {len(bans)}"

0 commit comments

Comments
 (0)