Skip to content

Commit

Permalink
Add notifications helper to message bot managers
Browse files Browse the repository at this point in the history
  • Loading branch information
TSRBerry committed Sep 12, 2023
1 parent 8fc0165 commit bac8705
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions robocop_ng/helpers/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import json
from typing import Optional, Union

from discord import Message, MessageReference, PartialMessage


MessageReferenceTypes = Union[Message, MessageReference, PartialMessage]


async def notify_management(
bot, message: str, reference_message: Optional[MessageReferenceTypes] = None
):
log_channel = await bot.get_channel_safe(bot.config.botlog_channel)
bot_manager_role = log_channel.guild.get_role(bot.config.bot_manager_role_id)

notification_message = f"{bot_manager_role.mention}:\n"

if reference_message is not None and reference_message.channel != log_channel:
notification_message += f"Message reference: {reference_message.jump_url}\n"
notification_message += message

return await log_channel.send(notification_message)
else:
notification_message += message

return await log_channel.send(
notification_message,
reference=reference_message,
mention_author=False,
)


async def report_critical_error(
bot,
error: BaseException,
reference_message: Optional[MessageReferenceTypes] = None,
additional_info: Optional[dict] = None,
):
message = "⛔ A critical error occurred!"

if additional_info is not None:
message += f"""
```json
{json.dumps(additional_info)}
```"""

message += f"""
Exception:
```
{error}
```"""

return await notify_management(bot, message, reference_message)

0 comments on commit bac8705

Please sign in to comment.