[Warnings] Add setting for warn channel and DMing warns toggle (#2929)

* Initial Commit

* Add changelog

* Review changes

* Changed to use contextlib.suppress

* Update warnings.py

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Rename 2932.bugfix.rst.txt to 2932.bugfix.rst

* Rename 2929.enhance.rst.txt to 2929.enhance.rst

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: Draper <27962761+Drapersniper@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: Draper <27962761+Drapersniper@users.noreply.github.com>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <6032823+jack1142@users.noreply.github.com>

* Rename 2929.enhance.rst to 2929.feature.rst

* Update warnings.py

* Add files via upload

* Update warnings.py

* Black

* Update warnings.py

* black

* Update warnings.py

* Update warnings.py

* Delete 2932.bugfix.rst

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com>
This commit is contained in:
Ianardo DiCaprio 2020-03-12 20:17:59 +00:00 committed by GitHub
parent 4afe1ff569
commit 2e5dc82692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 10 deletions

View File

@ -0,0 +1,2 @@
Added features to mod to be able to toggle warns being sent to both DM's and channel.
Added a command to set the channel the warns get sent to. (defaults to the channel the warn was issued in).

View File

@ -1,3 +1,4 @@
import contextlib
from collections import namedtuple from collections import namedtuple
from typing import Union, Optional from typing import Union, Optional
@ -23,7 +24,14 @@ _ = Translator("Warnings", __file__)
class Warnings(commands.Cog): class Warnings(commands.Cog):
"""Warn misbehaving users and take automated actions.""" """Warn misbehaving users and take automated actions."""
default_guild = {"actions": [], "reasons": {}, "allow_custom_reasons": False} default_guild = {
"actions": [],
"reasons": {},
"allow_custom_reasons": False,
"toggle_dm": True,
"warn_channel": None,
"toggle_channel": False,
}
default_member = {"total_points": 0, "status": "", "warnings": {}} default_member = {"total_points": 0, "status": "", "warnings": {}}
@ -75,6 +83,55 @@ class Warnings(commands.Cog):
else: else:
await ctx.send(_("Custom reasons have been disabled.")) await ctx.send(_("Custom reasons have been disabled."))
@warningset.command()
@commands.guild_only()
async def toggledm(self, ctx: commands.Context):
"""Toggle whether warnings should be sent to users in DMs."""
guild = ctx.guild
toggle = not await self.config.guild(guild).toggle_dm()
await self.config.guild(guild).toggle_dm.set(toggle)
if toggle:
await ctx.send(_("I will now try to send warnings to users DMs."))
else:
await ctx.send(_("Warnings will no longer be sent to users DMs."))
@warningset.command()
@commands.guild_only()
async def warnchannel(self, ctx: commands.Context, channel: discord.TextChannel = None):
"""Set the channel where warnings should be sent to.
Leave empty to use the channel `[p]warn` command was called in.
"""
guild = ctx.guild
if channel:
await self.config.guild(guild).warn_channel.set(channel.id)
await ctx.send(
_("The warn channel has been set to {channel}.").format(channel=channel.mention)
)
else:
await self.config.guild(guild).warn_channel.set(channel)
await ctx.send(_("Warnings will now be sent in the channel command was used in."))
@warningset.command()
@commands.guild_only()
async def togglechannel(self, ctx: commands.Context):
"""
Toggle if warnings should be sent to a channel set with `[p]warningset warnchannel`.
"""
guild = ctx.guild
toggle = await self.config.guild(guild).toggle_channel()
await self.config.guild(guild).toggle_channel.set(not toggle)
channel = self.bot.get_channel(await self.config.guild(guild).warn_channel())
if not toggle:
if channel:
await ctx.send(
_("Warnings will now be sent to {channel}.").format(channel=channel.mention)
)
else:
await ctx.send(_("Warnings will now be sent in the channel command was used in."))
else:
await ctx.send(_("Toggle channel has been disabled."))
@commands.group() @commands.group()
@commands.guild_only() @commands.guild_only()
@checks.guildowner_or_permissions(administrator=True) @checks.guildowner_or_permissions(administrator=True)
@ -261,6 +318,8 @@ class Warnings(commands.Cog):
`<reason>` can be a registered reason if it exists or a custom one `<reason>` can be a registered reason if it exists or a custom one
is created by default. is created by default.
""" """
channel = ctx.channel
guild = ctx.guild
if user == ctx.author: if user == ctx.author:
await ctx.send(_("You cannot warn yourself.")) await ctx.send(_("You cannot warn yourself."))
return return
@ -301,20 +360,41 @@ class Warnings(commands.Cog):
await member_settings.total_points.set(current_point_count) await member_settings.total_points.set(current_point_count)
await warning_points_add_check(self.config, ctx, user, current_point_count) await warning_points_add_check(self.config, ctx, user, current_point_count)
try: dm = await self.config.guild(ctx.guild).toggle_dm()
if dm:
em = discord.Embed( em = discord.Embed(
title=_("Warning from {user}").format(user=ctx.author), title=_("Warning from {user}").format(user=ctx.author),
description=reason_type["description"], description=reason_type["description"],
) )
em.add_field(name=_("Points"), value=str(reason_type["points"])) em.add_field(name=_("Points"), value=str(reason_type["points"]))
await user.send( with contextlib.suppress(discord.HTTPException):
_("You have received a warning in {guild_name}.").format( await user.send(
guild_name=ctx.guild.name _("You have received a warning in {guild_name}.").format(
), guild_name=ctx.guild.name
embed=em, ),
embed=em,
)
toggle_channel = await self.config.guild(guild).toggle_channel()
if toggle_channel:
em = discord.Embed(
title=_("Warning from {user}").format(user=ctx.author),
description=reason_type["description"],
) )
except discord.HTTPException: em.add_field(name=_("Points"), value=str(reason_type["points"]))
pass warn_channel = self.bot.get_channel(await self.config.guild(guild).warn_channel())
if warn_channel:
channel = warn_channel
await ctx.tick()
else:
channel = ctx.channel
if channel.permissions_for(guild.me).send_messages:
with contextlib.suppress(discord.HTTPException):
await channel.send(
_("{user} has been warned.").format(user=user.mention), embed=em
)
else:
await ctx.tick()
try: try:
reason_msg = _( reason_msg = _(
"{reason}\n\nUse `{prefix}unwarn {user} {message}` to remove this warning." "{reason}\n\nUse `{prefix}unwarn {user} {message}` to remove this warning."
@ -339,7 +419,6 @@ class Warnings(commands.Cog):
) )
except RuntimeError: except RuntimeError:
pass pass
await ctx.send(_("User {user} has been warned.").format(user=user))
@commands.command() @commands.command()
@commands.guild_only() @commands.guild_only()