[V3 Modlog] add events for modlog cases (#1717)

* Give modlog case objects the bot as an attribute

* Dispatch modlog_case_create and modlog_case_edit events

* case.bot, not just bot

* fix a couple more issues resulting from refactor

* Case.edit doesn't need the bot parameter lol

* Make create_case return the case object (because tests)

* Modify create_case docstring

* Fix a docstring
This commit is contained in:
palmtree5 2018-05-27 21:18:50 -08:00 committed by GitHub
parent 6ae02d2d02
commit 05ad3fcd5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 43 deletions

View File

@ -1422,7 +1422,7 @@ class Mod:
)
else:
try:
case = await modlog.create_case(
await modlog.create_case(
self.bot,
guild,
message.created_at,
@ -1515,6 +1515,34 @@ class Mod:
except RuntimeError as e:
print(e)
async def on_modlog_case_create(self, case: modlog.Case):
"""
An event for modlog case creation
"""
mod_channel = await modlog.get_modlog_channel(case.guild)
if mod_channel is None:
return
use_embeds = await case.bot.embed_requested(mod_channel, case.guild.me)
case_content = await case.message_content(use_embeds)
if use_embeds:
msg = await mod_channel.send(embed=case_content)
else:
msg = await mod_channel.send(case_content)
await case.edit({"message": msg})
async def on_modlog_case_edit(self, case: modlog.Case):
"""
Event for modlog case edits
"""
if not case.message:
return
use_embed = await case.bot.embed_requested(case.message.channel, case.guild.me)
case_content = await case.message_content(use_embed)
if use_embed:
await case.message.edit(embed=case_content)
else:
await case.message.edit(content=case_content)
async def get_audit_entry_info(self, guild: discord.Guild, action: int, target):
"""Get info about an audit log entry.

View File

@ -146,5 +146,5 @@ class ModLog:
if case_before.moderator != author:
to_modify["amended_by"] = author
to_modify["modified_at"] = ctx.message.created_at.timestamp()
await case_before.edit(self.bot, to_modify)
await case_before.edit(to_modify)
await ctx.send(_("Reason has been updated."))

View File

@ -43,6 +43,7 @@ class Case:
def __init__(
self,
bot: Red,
guild: discord.Guild,
created_at: int,
action_type: str,
@ -56,6 +57,7 @@ class Case:
modified_at: int = None,
message: discord.Message = None,
):
self.bot = bot
self.guild = guild
self.created_at = created_at
self.action_type = action_type
@ -69,32 +71,21 @@ class Case:
self.case_number = case_number
self.message = message
async def edit(self, bot, data: dict):
async def edit(self, data: dict):
"""
Edits a case
Parameters
----------
bot: Red
The bot instance
data: dict
The attributes to change
Returns
-------
"""
for item in list(data.keys()):
setattr(self, item, data[item])
use_embed = await bot.embed_requested(self.message.channel, self.guild.me)
case_content = await self.message_content(use_embed)
if use_embed:
await self.message.edit(embed=case_content)
else:
await self.message.edit(case_content)
await _conf.guild(self.guild).cases.set_raw(str(self.case_number), value=self.to_json())
self.bot.dispatch("modlog_case_edit", self)
async def message_content(self, embed: bool = True):
"""
@ -240,6 +231,7 @@ class Case:
amended_by = guild.get_member(data["amended_by"])
case_guild = bot.get_guild(data["guild"])
return cls(
bot=bot,
guild=case_guild,
created_at=data["created_at"],
action_type=data["action_type"],
@ -437,7 +429,9 @@ async def create_case(
channel: discord.TextChannel = None,
) -> Union[Case, None]:
"""
Creates a new case
Creates a new case.
This fires an event :code:`on_modlog_case_create`
Parameters
----------
@ -459,26 +453,7 @@ async def create_case(
The time the action is in effect until
channel: `discord.TextChannel` or `discord.VoiceChannel`
The channel the action was taken in
Returns
-------
Case
The newly created case
Raises
------
RuntimeError
If the mod log channel doesn't exist
"""
mod_channel = None
if hasattr(guild, "owner"):
# Fairly arbitrary, but it doesn't really matter
# since we don't need the modlog channel in tests
try:
mod_channel = await get_modlog_channel(guild)
except RuntimeError:
raise RuntimeError("No mod log channel set for guild {}".format(guild.name))
case_type = await get_casetype(action_type, guild)
if case_type is None:
return None
@ -489,6 +464,7 @@ async def create_case(
next_case_number = int(await get_next_case_number(guild))
case = Case(
bot,
guild,
int(created_at.timestamp()),
action_type,
@ -502,15 +478,8 @@ async def create_case(
modified_at=None,
message=None,
)
if hasattr(mod_channel, "send"): # Not going to be the case for tests
use_embeds = await bot.embed_requested(mod_channel, guild.me)
case_content = await case.message_content(use_embeds)
if use_embeds:
msg = await mod_channel.send(embed=case_content)
else:
msg = await mod_channel.send(case_content)
case.message = msg
await _conf.guild(guild).cases.set_raw(str(next_case_number), value=case.to_json())
bot.dispatch("modlog_case_create", case)
return case