diff --git a/changelog.d/mod/2932.bugfix.rst.txt b/changelog.d/mod/2932.bugfix.rst.txt deleted file mode 100644 index 555c1a3753f..00000000000 --- a/changelog.d/mod/2932.bugfix.rst.txt +++ /dev/null @@ -1 +0,0 @@ -Fixed an error when reloading the core mod cog diff --git a/changelog.d/warnings/2929.feature.rst b/changelog.d/warnings/2929.feature.rst new file mode 100644 index 00000000000..0d2367e6a95 --- /dev/null +++ b/changelog.d/warnings/2929.feature.rst @@ -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). diff --git a/redbot/cogs/warnings/warnings.py b/redbot/cogs/warnings/warnings.py index b53fdaf8de2..9739a990518 100644 --- a/redbot/cogs/warnings/warnings.py +++ b/redbot/cogs/warnings/warnings.py @@ -1,3 +1,4 @@ +import contextlib from collections import namedtuple from typing import Union, Optional @@ -24,7 +25,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": {}} @@ -76,6 +84,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) @@ -251,7 +308,7 @@ async def warn( user: discord.Member, points: Optional[int] = 1, *, - reason: str + reason: str, ): """Warn the user for the specified reason. @@ -260,6 +317,8 @@ async def warn( `` 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 @@ -300,20 +359,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." @@ -338,7 +418,6 @@ async def warn( ) except RuntimeError: pass - await ctx.send(_("User {user} has been warned.").format(user=user)) @commands.command() @commands.guild_only()