From 0c9352fe33a2dfb90005ccf083bf0a561fb36412 Mon Sep 17 00:00:00 2001 From: Ave Ozkal Date: Sun, 23 Dec 2018 18:33:59 +0300 Subject: [PATCH] Add ban and kick Also, message escaping stuffs were added to common and meme was updated accordingly --- README.md | 4 +-- cogs/common.py | 5 ++++ cogs/meme.py | 2 +- cogs/mod.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6ac72ff..bfcde95 100755 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ Based on https://gitlab.com/ao/dpybotbase - [ ] Logging: message deletes - [ ] Logging: bans - [ ] Logging: kicks -- [ ] Moderation: ban -- [ ] Moderation: kick +- [x] Moderation: ban +- [x] Moderation: kick - [x] Moderation: userinfo - [ ] Moderation: approve-revoke (community) - [ ] Moderation: addhacker-removehacker diff --git a/cogs/common.py b/cogs/common.py index 69450af..715440c 100644 --- a/cogs/common.py +++ b/cogs/common.py @@ -17,6 +17,7 @@ class Common: self.bot.aioget = self.aioget self.bot.aiogetbytes = self.aiogetbytes self.bot.get_relative_timestamp = self.get_relative_timestamp + self.bot.escape_message = self.escape_message def get_relative_timestamp(self, time_from=None, time_to=None, humanized=False, include_from=False, @@ -98,6 +99,10 @@ class Common: """Turns a given hex color into an integer""" return int("0x" + color_hex.strip('#'), 16) + def escape_message(self, text: str): + """Escapes unfun stuff from messages""" + return text.replace("@", "@ ").replace("#", "# ") + # This function is based on https://stackoverflow.com/a/35435419/3286892 # by link2110 (https://stackoverflow.com/users/5890923/link2110) # modified by Ave (https://github.com/aveao), licensed CC-BY-SA 3.0 diff --git a/cogs/meme.py b/cogs/meme.py index fba07f0..7c79c0e 100644 --- a/cogs/meme.py +++ b/cogs/meme.py @@ -21,7 +21,7 @@ class Meme: @commands.command(hidden=True, name="bam") async def bam_member(self, ctx, user: discord.Member): """Bams a user owo""" - await ctx.send(f"{self.bot.escape_name(user)} is ̶n͢ow b̕&̡.̷ 👍̡") + await ctx.send(f"{self.bot.escape_message(user)} is ̶n͢ow b̕&̡.̷ 👍̡") @commands.check(check_if_staff_or_ot) @commands.command(hidden=True, name="warm") diff --git a/cogs/mod.py b/cogs/mod.py index 9e96e8c..f7a32e9 100644 --- a/cogs/mod.py +++ b/cogs/mod.py @@ -6,10 +6,89 @@ import config class AdminCog: def __init__(self, bot): self.bot = bot + self.modlog_channel = bot.get_channel(config.modlog_channel) def check_if_staff(ctx): return any(r.id in config.staff_role_ids for r in ctx.author.roles) + def check_if_target_is_staff(self, target): + return any(r.id in config.staff_role_ids for r in target.roles) + + @commands.bot_has_permissions(kick_members=True) + @commands.check(check_if_staff) + @commands.command() + async def kick(self, ctx, target: discord.Member, *, reason: str = ""): + """Kicks a user, staff only.""" + if self.check_if_target_is_staff(target): + return await ctx.send("I can't kick this user as " + "they're a member of staff.") + + safe_name = self.bot.escape_message(str(target)) + + dm_message = f"You were kicked from {ctx.guild.name}." + if reason: + dm_message += f" The given reason is: \"{reason}\"." + dm_message += "\n\nYou are able to rejoin the server,"\ + " but please be sure to behave when participating again." + + try: + await target.send(dm_message) + except discord.errors.Forbidden: + # Prevents kick issues in cases where user blocked bot + # or has DMs disabled + pass + + await target.kick(reason=f"{ctx.author}, reason: {reason}") + chan_message = f"👢 **Kick**: {ctx.author.mention} kicked "\ + f"{target.mention} | {safe_name}\n"\ + f"🏷 __User ID__: {target.id}\n" + if reason: + chan_message += f"✏️ __Reason__: \"{reason}\"" + else: + chan_message += "Please add an explanation below. In the future"\ + ", it is recommended to use `.ban [reason]`"\ + " as the reason is automatically sent to the user." + + await self.modlog_channel.send(chan_message) + + @commands.bot_has_permissions(ban_members=True) + @commands.check(check_if_staff) + @commands.command() + async def ban(self, ctx, target: discord.Member, *, reason: str = ""): + """Bans a user, staff only.""" + if self.check_if_target_is_staff(target): + return await ctx.send("I can't ban this user as " + "they're a member of staff.") + + safe_name = self.bot.escape_message(str(target)) + + dm_message = f"You were banned from {ctx.guild.name}." + if reason: + dm_message += f" The given reason is: \"{reason}\"." + dm_message += "\n\nThis ban does not expire." + + try: + await target.send(dm_message) + except discord.errors.Forbidden: + # Prevents kick issues in cases where user blocked bot + # or has DMs disabled + pass + + await target.ban(reason=f"{ctx.author}, reason: {reason}", + delete_message_days=0) + chan_message = f"👢 **Ban**: {ctx.author.mention} banned "\ + f"{target.mention} | {safe_name}\n"\ + f"🏷 __User ID__: {target.id}\n" + if reason: + chan_message += f"✏️ __Reason__: \"{reason}\"" + else: + chan_message += "Please add an explanation below. In the future"\ + ", it is recommended to use `.ban [reason]`"\ + " as the reason is automatically sent to the user." + + await self.modlog_channel.send(chan_message) + await ctx.send(f"{safe_name} is now b&. 👍") + @commands.check(check_if_staff) @commands.command() async def userinfo(self, ctx, *, user: discord.Member):