Skip to content

Commit

Permalink
[Warnings] Add setting for warn channel and DMing warns toggle (#2929)
Browse files Browse the repository at this point in the history
* Initial Commit

* Add changelog

* Review changes

* Changed to use contextlib.suppress

* Update warnings.py

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* 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 <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: Draper <[email protected]>

* Update redbot/cogs/warnings/warnings.py

Co-Authored-By: jack1142 <[email protected]>

* 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 <[email protected]>
Co-authored-by: Draper <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2020
1 parent 4afe1ff commit 2e5dc82
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelog.d/warnings/2929.feature.rst
Original file line number Diff line number Diff line change
@@ -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).
99 changes: 89 additions & 10 deletions redbot/cogs/warnings/warnings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
from collections import namedtuple
from typing import Union, Optional

Expand All @@ -23,7 +24,14 @@
class Warnings(commands.Cog):
"""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": {}}

Expand Down Expand Up @@ -75,6 +83,55 @@ async def allowcustomreasons(self, ctx: commands.Context, allowed: bool):
else:
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.guild_only()
@checks.guildowner_or_permissions(administrator=True)
Expand Down Expand Up @@ -261,6 +318,8 @@ async def warn(
`<reason>` can be a registered reason if it exists or a custom one
is created by default.
"""
channel = ctx.channel
guild = ctx.guild
if user == ctx.author:
await ctx.send(_("You cannot warn yourself."))
return
Expand Down Expand Up @@ -301,20 +360,41 @@ async def warn(
await member_settings.total_points.set(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(
title=_("Warning from {user}").format(user=ctx.author),
description=reason_type["description"],
)
em.add_field(name=_("Points"), value=str(reason_type["points"]))
await user.send(
_("You have received a warning in {guild_name}.").format(
guild_name=ctx.guild.name
),
embed=em,
with contextlib.suppress(discord.HTTPException):
await user.send(
_("You have received a warning in {guild_name}.").format(
guild_name=ctx.guild.name
),
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:
pass
em.add_field(name=_("Points"), value=str(reason_type["points"]))
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:
reason_msg = _(
"{reason}\n\nUse `{prefix}unwarn {user} {message}` to remove this warning."
Expand All @@ -339,7 +419,6 @@ async def warn(
)
except RuntimeError:
pass
await ctx.send(_("User {user} has been warned.").format(user=user))

@commands.command()
@commands.guild_only()
Expand Down

0 comments on commit 2e5dc82

Please sign in to comment.