async def black_list(self, ctx): """black_list_help""" if ctx.invoked_subcommand is None: if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "automod") is False: return await ctx.send( Translator.translate(ctx.guild, "black_list_disabled", _emote="NO", prefix=ctx.prefix)) _censor_list = [ x.strip().lower() for x in DBUtils.get( db.configs, "guildId", f"{ctx.guild.id}", "censored_words") if x != "--------------" ] if len(_censor_list) < 1: return ctx.send( Translator.translate(ctx.guild, "black_list_empty", _emote="NO", prefix=ctx.prefix)) await ctx.send( Translator.translate(ctx.guild, "censor_list", guild_name=ctx.guild.name, words=_censor_list))
async def rank(self, ctx, user: discord.Member = None): """rank_help""" if user is None: user = ctx.author if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem") is False: return await ctx.send(Translator.translate(ctx.guild, "lvlsystem_disabled", _emote="NO")) try: level_id = f"{ctx.guild.id}-{user.id}" xp = DBUtils.get(db.ranks, "levelId", level_id, "xp") lvl = DBUtils.get(db.ranks, "levelId", level_id, "lvl") needed = 10 if lvl <= 1: needed_xp = needed + 1 return await ctx.send(embed=await self._rank(ctx, user, xp, lvl, needed_xp)) counter = 0 while counter < lvl: counter += 1 needed += 40 if counter >= lvl: break needed_xp = needed + 2 return await ctx.send(embed=await self._rank(ctx, user, xp, lvl, needed_xp)) except Exception: await ctx.send(Translator.translate(ctx.guild, "not_ranked", _emote="NO", user=user))
async def show(self, ctx): """show_help""" try: enabled_modules, disabled_modules = DBUtils.get_module_config( ctx.guild.id) general, messages, members = DBUtils.get_log_channels(ctx.guild.id) e = discord.Embed(color=discord.Color.blurple(), title="Configuration for {}".format( ctx.guild.name)) e.set_thumbnail(url=ctx.guild.icon_url) e.add_field(name=Translator.translate(ctx.guild, "log_channels"), value=Translator.translate(ctx.guild, "log_actions", general=general, messages=messages, members=members), inline=False) e.add_field( name=Translator.translate(ctx.guild, "enabled_modules"), value=", ".join( enabled_modules if len(enabled_modules) > 0 else ["None"]), inline=False) e.add_field( name=Translator.translate(ctx.guild, "disabled_modules"), value=", ".join(disabled_modules if len(disabled_modules) > 0 else ["None"]), inline=False) await ctx.send(embed=e) except Exception: pass
async def ranks(self, ctx): """ranks_help""" if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem") is False: return await ctx.send(Translator.translate(ctx.guild, "lvlsystem_disabled", _emote="NO")) level_roles = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "level_roles") if len(level_roles) < 1: return await ctx.send(Translator.translate(ctx.guild, "no_lvl_roles", _emote="NO")) embed = discord.Embed( color=discord.Color.blurple(), title="{} {}".format(ctx.guild.name, Translator.translate(ctx.guild, "lvl_roles")), ) embed.set_thumbnail( url=ctx.guild.icon_url ) for r in level_roles: lvl = r.split("-")[0] _id = r.split("-")[1] if not str(_id) in [str(x.id) for x in ctx.guild.roles]: role = Translator.translate(ctx.guild, "deleted_role") else: role = "<@&{}>".format(_id) embed.add_field( name="**Level {}**".format(lvl), value="{}".format(role), inline=False ) await ctx.send(embed=embed)
async def add_to_censor_list(self, ctx, *, text: str): if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "automod") is False: return await ctx.send( Translator.translate(ctx.guild, "black_list_disabled", _emote="NO", prefix=ctx.prefix)) _censor_list = [ x for x in DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "censored_words") if x != "--------------" ] if text.lower() in [ x.strip().lower() for x in DBUtils.get( db.configs, "guildId", f"{ctx.guild.id}", "censored_words") if x != "--------------" ]: return await ctx.send( Translator.translate(ctx.guild, "already_on_black_list", _emote="NO", word=text)) _censor_list.append(str(text)) DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "censored_words", _censor_list) await ctx.send( Translator.translate(ctx.guild, "added_to_black_list", _emote="YES", word=text))
async def handle_spam(self, guild, member, msg): if DBUtils.get(db.configs, "guildId", f"{guild.id}", "antispam") is False: return # anti spam isn't enabled for this guild c = self.spam_checker[guild.id] if not c.is_spamming(msg): return self.handling.append(member.id) try: await guild.kick(user=member, reason="[AutoMod] Spam") except discord.HTTPException: log.info(f"[Anti Spam] Error while trying to kick {member} ({member.id}) from server {guild} via the anti spam system") else: log.info(f"[Anti Spam] Kicked {member} ({member.id}) from server {guild} via the anti spam system") case = DBUtils.new_case() timestamp = datetime.datetime.utcnow().strftime("%d/%m/%Y %H:%M") mod = discord.utils.get(guild.members, id=self.bot.user.id) DBUtils.insert(db.inf, new_infraction(case, msg.guild.id, member, mod, timestamp, "Kick", "[AutoMod] Spam")) on_time = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") await Logging.log_to_guild(guild.id, "memberLogChannel", Translator.translate(guild, "log_spam", _emote="SHOE", on_time=on_time, user=member, user_id=member.id, moderator=self.bot.user, moderator_id=self.bot.user.id, channel=msg.channel.mention)) finally: self.handling.remove(member.id)
async def on_message(self, message: discord.Message): if message.guild is None: return if DBUtils.get(db.configs, "guildId", f"{message.guild.id}", "lvlsystem") is False: return user = message.author if user.id in self.cooldown_cache: return prefix = DBUtils.get(db.configs, "guildId", f"{message.guild.id}", "prefix") if user.bot is True or f"{prefix}rank" in message.content or f"{prefix}lb" in message.content or f"{prefix}leaderboard" in message.content: return self.cooldown_cache.append(user.id) lvl_id = f"{message.guild.id}-{user.id}" await self._update_data(message.guild.id, lvl_id, user.id) await self._add_xp(lvl_id, 2) await self._level_up(message, lvl_id, user) await asyncio.sleep(5) # 5 seconds cache lifetime self.cooldown_cache.remove(user.id)
async def add(self, ctx, lvl: RangedInt(2, 200), role: discord.Role): "add_help" if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem") is False: return await ctx.send(Translator.translate(ctx.guild, "lvlsystem_disabled", _emote="NO")) automod = await Utils.get_member(self.bot, ctx.guild, self.bot.user.id) if role.position >= automod.top_role.position: return await ctx.send(Translator.translate(ctx.guild, "role_too_high")) level_roles = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "level_roles") roles = [x.split("-")[1] for x in level_roles] levels = [x.split("-")[0] for x in level_roles] if str(lvl) in levels: return await ctx.send(Translator.translate(ctx.guild, "already_role_for_lvl", _emote="NO", lvl=lvl)) if str(role.id) is roles: return await ctx.send(Translator.translate(ctx.guild, "already_lvl_role", _emote="NO", role=role)) if len(level_roles) > 10: return await ctx.send(Translator.translate(ctx.guild, "max_lvl_roles", _emote="NO")) level_roles.append(f"{lvl}-{role.id}") DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "level_roles", level_roles) await ctx.send(Translator.translate(ctx.guild, "added_lvl_role", _emote="YES", role=role, lvl=lvl))
async def _automod(self, ctx): DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "automod", False) await ctx.send( Translator.translate(ctx.guild, "disabled_module", _emote="YES", module="automod"))
async def _member_logging(self, ctx): DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "memberLogging", False) await ctx.send( Translator.translate(ctx.guild, "disabled_module", _emote="YES", module="join_leave_logging"))
async def lvlsystem(self, ctx): DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem", True) await ctx.send( Translator.translate(ctx.guild, "enabled_module_no_channel", _emote="YES", module="rank_system"))
async def _lvlsystem(self, ctx): DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem", False) await ctx.send( Translator.translate(ctx.guild, "disabled_module", _emote="YES", module="rank_system"))
async def welcome_off(self, ctx): """welcome_off_help""" DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "welcomeChannel", "") DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "welcomeMessage", "") await ctx.send( Translator.translate(ctx.guild, "off_success", _emote="YES"))
async def welcome_msg(self, ctx, *, msg: str): """welcome_msg_help""" if len(msg) > 1500: return await ctx.send( Translator.translate(ctx.guild, "msg_too_long")) DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "welcomeMessage", f"{msg}") await ctx.send( Translator.translate(ctx.guild, "msg_success", _emote="YES"))
async def welcome_channel(self, ctx, channel: discord.TextChannel): """welcome_channel_help""" DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "welcomeChannel", channel.id) await ctx.send( Translator.translate(ctx.guild, "channel_success", _emote="YES", channel=channel.mention))
async def action_log(self, ctx, channel: discord.TextChannel): """action_log_help""" DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "memberLogChannel", int(channel.id)) await ctx.send( Translator.translate(ctx.guild, "log_mod_actions", _emote="YES", channel=channel.mention))
async def member_logging(self, ctx, channel: discord.TextChannel): DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "joinLogChannel", int(channel.id)) DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "memberLogging", True) await ctx.send( Translator.translate(ctx.guild, "enabled_module_channel", _emote="YES", module="join_leave_logging", channel=channel.mention))
async def on_message_delete(self, message: Message): await asyncio.sleep( 1 ) # sleep a bit, we don't log message deletions from the censor module if message.id in self.bot.running_msg_deletions: self.bot.running_msg_deletions.remove(message.id) return c = message.channel channel_id = c.id if isinstance(c, DMChannel): return if c is None or str(channel_id) == str( DBUtils.get( db.configs, "guildId", f"{c.guild.id}", "memberLogChannel")) or not isinstance(c, TextChannel): return if message.author.id == self.bot.user.id: return if c.guild is None: return if DBUtils.get(db.configs, "guildId", f"{c.guild.id}", "messageLogging") is False: return ignored_users = DBUtils.get(db.configs, "guildId", f"{c.guild.id}", "ignored_users") if ignored_users is None: on_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") await Logging.log_to_guild( c.guild.id, "messageLogChannel", Translator.translate(message.guild, "log_message_deletion", _emote="BIN", user=message.author, user_id=message.author.id, channel=c.mention, on_time=on_time, content=message.content)) else: if message.author.id in ignored_users: return else: on_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") await Logging.log_to_guild( c.guild.id, "messageLogChannel", Translator.translate(message.guild, "log_message_deletion", _emote="BIN", user=message.author, user_id=message.author.id, channel=c.mention, on_time=on_time, content=message.content))
async def on_guild_join(bot, guild: Guild): if guild.id in Utils.from_config("BLOCKED_GUILDS"): await Logging.bot_log( bot, f"Someone tried adding me to blocked guild {guild.name} ({guild.id})", None) await guild.leave() else: bot.missing_guilds.add(guild.id) await guild.chunk(cache=True) bot.missing_guilds.remove(guild.id) DBUtils.insert(db.configs, Schemas.guild_schema(guild)) await Logging.guild_log( bot, f"I was added to a new guild: {guild.name} ({guild.id}).")
async def on_member_remove(self, member: Member): await asyncio.sleep( 1) # sleep a bit, so we don't log an unban made with the bot if member.id in self.bot.running_removals: self.bot.running_removals.remove(member.id) return if DBUtils.get(db.configs, "guildId", f"{member.guild.id}", "memberLogging") is True: joined = (datetime.fromtimestamp(time.time()) - member.joined_at).days try: on_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") await Logging.log_to_guild( member.guild.id, "joinLogChannel", Translator.translate(member.guild, "log_leave", _emote="LEAVE", user=member, user_id=member.id, on_time=on_time, joined=joined)) except Exception: pass else: return
async def generate_help_pages(ctx, bot): pages = [] out = [] valid_cogs = [ c for c in bot.cogs if not str(c) in ["Admin", "AntiSpam", "Censor", "GlobalListeners"] ] for cog in valid_cogs: commands = [ " {}{}{}".format(x.name, " " * abs(18 - len(x.name)), Translator.translate(ctx.guild, x.short_doc)) for x in bot.get_cog(cog).get_commands() if x.hidden is False ] output = f"[ {cog} ]\n" + "\n".join(commands) + "\n" out.append(output) for page in Pages.paginate("{}".format("\n".join(out)), prefix="```ini\n", suffix=Translator.translate( ctx.guild, "help_suffix", prefix=DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "prefix"))): pages.append(page) return pages
async def on_message(self, message: discord.Message): if message.author.bot or message.webhook_id is not None: return if not hasattr(message.channel, "guild") or message.channel.guild is None: return automod = message.guild.me if automod is None: await Utils.get_member(self.bot, message.guild, self.bot.user.id) perms = message.channel.permissions_for(automod) if automod is None: return if not (perms.read_messages and perms.send_messages and perms.embed_links): return if message.author.id == self.bot.user.id: return prefix = DBUtils.get(db.configs, "guildId", f"{message.guild.id}", "prefix") try: cmds = self.command_cache[str(message.guild.id)] except KeyError: return if message.content.startswith(prefix, 0) and len(cmds) > 0: for entry in cmds: trigger = entry["trigger"] if message.content.lower() == prefix + trigger or (message.content.lower().startswith(trigger, len(prefix)) and message.content.lower()[len(prefix + trigger)] == " "): reply = entry["reply"] return await message.channel.send(f"{reply}")
async def on_member_join(self, member: Member): if DBUtils.get(db.configs, "guildId", f"{member.guild.id}", "memberLogging") is True: created = (datetime.fromtimestamp(time.time()) - member.created_at).days try: on_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") await Logging.log_to_guild( member.guild.id, "joinLogChannel", Translator.translate(member.guild, "log_join", _emote="JOIN", user=member, user_id=member.id, on_time=on_time, age=created)) except Exception: pass msg = DBUtils.get(db.configs, "guildId", f"{member.guild.id}", "welcomeMessage") welcome_id = DBUtils.get(db.configs, "guildId", f"{member.guild.id}", "welcomeChannel") if msg is None or msg == "": return if welcome_id is None or welcome_id == "": return try: welcome_channel = await self.bot.fetch_channel(int(welcome_id)) except Exception: return members = len(member.guild.members ) # the guilds member count after the member joined mention = member.mention # mentions the new member member = member.name # displays the new members name without the discrim try: await welcome_channel.send( str(msg).format(members=members, member=member, mention=mention)) except Exception: return
async def _remove(self, ctx, user: discord.User): ignored_users = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "ignored_users") if not user.id in ignored_users: return await ctx.send( Translator.translate(ctx.guild, "not_existing_ignored_user", _emote="NO", user=user.name)) ignored_users.remove(int(user.id)) DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "ignored_users", ignored_users) await ctx.send( Translator.translate(ctx.guild, "ignored_user_removed", _emote="YES", user=user.name))
async def _add(self, ctx, member: discord.Member): ignored_users = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "ignored_users") if member.id in ignored_users: return await ctx.send( Translator.translate(ctx.guild, "already_ignored_user", _emote="YES", user=member.name)) ignored_users.append(int(member.id)) DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "ignored_users", ignored_users) await ctx.send( Translator.translate(ctx.guild, "ignored_user_added", _emote="YES", user=member.name))
async def _fill_rank_cache(self): await asyncio.sleep(1) # wait a bit, we got time valid_guilds = [x for x in self.bot.guilds if DBUtils.get(db.configs, "guildId", f"{x.id}", "lvlsystem") is True] while len(self.cached_guilds) < len(valid_guilds): for g in valid_guilds: if g.id in self.cached_guilds: pass else: for m in g.members: self.rank_cache[m.id] = f"{m.name}#{m.discriminator}" self.cached_guilds.add(g.id)
async def create(self, ctx, trigger: str, *, reply: str = None): """create_help""" if len(trigger) == 0: await ctx.send(Translator.translate(ctx.guild, "no_trigger", _emote="THINK")) elif reply is None or reply == "": await ctx.send(Translator.translate(ctx.guild, "no_reply", _emote="THINK")) elif len(trigger) > 20: await ctx.send(Translator.translate(ctx.guild, "trigger_too_long")) else: trigger = trigger.lower() if trigger in ["{}".format(x["cmdId"].split("-")[1]) for x in db.commands.find() if x["cmdId"].split("-")[0] == str(ctx.guild.id)]: await ctx.send(Translator.translate(ctx.guild, "command_already_exists")) else: DBUtils.insert(db.commands, Schemas.command_schema(ctx.guild, trigger, reply, ctx.message.author)) try: self.command_cache[str(ctx.guild.id)].append({"trigger": trigger, "reply": reply}) except Exception: self.command_cache[str(ctx.guild.id)] = [{"trigger": trigger, "reply": reply}] finally: await ctx.send(Translator.translate(ctx.guild, "command_added", _emote="YES", command=trigger))
async def remove(self, ctx, role: discord.Role): "remove_help" if DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "lvlsystem") is False: return await ctx.send(Translator.translate(ctx.guild, "lvlsystem_disabled", _emote="NO")) level_roles = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "level_roles") roles = [x.split("-")[1] for x in level_roles] levels = [x.split("-")[0] for x in level_roles] if len(level_roles) < 1: return await ctx.send(Translator.translate(ctx.guild, "no_lvl_roles", _emote="NO")) if not str(role.id) in roles: return await ctx.send(Translator.translate(ctx.guild, "invalid_lvl_role", _emote="NO", role=role)) lvl = levels[roles.index(str(role.id))] level_roles.remove(f"{lvl}-{role.id}") DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "level_roles", level_roles) await ctx.send(Translator.translate(ctx.guild, "removed_lvl_role", _emote="YES", role=role))
async def complex_cleaning( ctx, search): # also cleans messages that have the bots prefix in them prefix = DBUtils.get(db.configs, "guildId", f"{ctx.guild.id}", "prefix") def check(m): return m.author == ctx.me or m.content.startswith(prefix) deleted = await ctx.channel.purge(limit=search, check=check, before=ctx.message) return len(deleted)
async def prefix(self, ctx, new_prefix: str = None): """prefix_help""" if new_prefix is None: await ctx.send( Translator.translate(ctx.guild, "current_prefix", prefix=DBUtils.get( db.configs, "guildId", f"{ctx.guild.id}", "prefix"))) elif len(new_prefix) > 15: await ctx.send( Translator.translate(ctx.guild, "prefix_too_long", _emote="NO")) else: DBUtils.update(db.configs, "guildId", f"{ctx.guild.id}", "prefix", f"{new_prefix}") await ctx.send( Translator.translate(ctx.guild, "prefix_updated", _emote="YES", prefix=new_prefix))