Skip to content
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
2 changes: 2 additions & 0 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class _Channels(EnvConfig, env_prefix="channels_"):
incidents: int = 714214212200562749
incidents_archive: int = 720668923636351037
mod_alerts: int = 473092532147060736
# Actually a thread, but for bootstrap purposes we treat it as a channel
rule_alerts: int = 1386490058739290182
mod_meta: int = 775412552795947058
mods: int = 305126844661760000
nominations: int = 822920136150745168
Expand Down
40 changes: 39 additions & 1 deletion bot/exts/info/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from discord.ext.commands import BucketType, Cog, Context, command, group, has_any_role
from discord.utils import escape_markdown
from pydis_core.site_api import ResponseCodeError
from pydis_core.utils.channel import get_or_fetch_channel
from pydis_core.utils.members import get_or_fetch_member
from pydis_core.utils.paste_service import PasteFile, PasteTooLongError, PasteUploadError, send_to_paste_service

Expand Down Expand Up @@ -602,6 +603,37 @@ async def _set_rules_command_help(self) -> None:

self.rules.help = help_string

async def _send_rules_alert(self, ctx: Context, requested_rules: list[int]) -> None:
"""Send an alert to the Rule Alerts thread when a non-staff member uses the rules command."""
rules_thread = await get_or_fetch_channel(self.bot, constants.Channels.rule_alerts)

if rules_thread is None:
log.error("Failed to find the rules alert thread channel.")
return

rule_desc = None

if len(requested_rules) == 1:
rule_desc = f"Rule **{requested_rules[0]}** was requested by {ctx.author.mention} in {ctx.channel.mention}."
elif len(requested_rules) > 1:
rule_desc = (
f"Rules **{', '.join(map(str, requested_rules))}** were requested "
f"by {ctx.author.mention} in {ctx.channel.mention}."
)

warning_embed = Embed(
title="Rules Command Alert",
description=rule_desc,
color=Colour.red(),
url=ctx.message.jump_url
)

warning_embed.set_footer(text="Warnings are sent for invocations from non-staff members.")

warning_embed.set_author(name=f"{ctx.author} ({ctx.author.id})", icon_url=ctx.author.display_avatar.url)

await rules_thread.send(embed=warning_embed)

@command(aliases=("rule",))
async def rules(self, ctx: Context, *, args: str | None) -> set[int] | None:
"""
Expand Down Expand Up @@ -651,10 +683,16 @@ async def rules(self, ctx: Context, *, args: str | None) -> set[int] | None:
final_rule_numbers = {keyword_to_rule_number[keyword] for keyword in keywords}
final_rule_numbers.update(rule_numbers)

for rule_number in sorted(final_rule_numbers):
sorted_rules = sorted(final_rule_numbers)

for rule_number in sorted_rules:
self.bot.stats.incr(f"rule_uses.{rule_number}")
final_rules.append(f"**{rule_number}.** {full_rules[rule_number - 1][0]}")

if constants.Roles.helpers not in {role.id for role in ctx.author.roles}:
# If the user is not a helper, send an alert to the rules thread.
await self._send_rules_alert(ctx, sorted_rules)

await LinePaginator.paginate(final_rules, ctx, rules_embed, max_lines=3)

return final_rule_numbers
Expand Down
3 changes: 3 additions & 0 deletions tests/bot/exts/info/test_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ def setUp(self) -> None:
)
]
self.bot.api_client.get.return_value = self.full_rules
# Patch get_channel to handle the rule alerts being sent to a thread for non-staff (as our mock user is).
self.bot.get_channel.return_value = helpers.MockTextChannel(id=50, name="rules")
Comment thread
jb3 marked this conversation as resolved.


async def test_return_none_if_one_rule_number_is_invalid(self):

Expand Down
Loading