from discord import Embed, Forbidden, Member, Guild from discord.ext import commands from discord.ext.commands import BadArgument from administrator import db from administrator.check import is_enabled from administrator.logger import logger from administrator.utils import time_pars, seconds_to_time_string extension_name = "warn" logger = logger.getChild(extension_name) class Warn(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot def description(self): return "Send warning to user and make custom action after a number of warn" @staticmethod async def check_warn(ctx: commands.Context, target: Member): s = db.Session() c = s.query(db.Warn).filter(db.Warn.guild == ctx.guild.id, db.Warn.user == target.id).count() a = s.query(db.WarnAction).filter(db.WarnAction.guild == ctx.guild.id, db.WarnAction.count == c).first() if a: reason = f"Action after {c} warns" if a.action == "kick": await target.kick(reason=reason) elif a.action == "ban": await target.ban(reason=reason) elif a.action == "mute": pass # Integration with upcoming ban & mute extension @staticmethod def get_target(ctx: commands.Context, user: str) -> Member: users = {str(m): m for m in ctx.guild.members} if user not in users: raise BadArgument() return users[user] @commands.group("warn", pass_context=True) @is_enabled() @commands.guild_only() #@commands.has_permissions(manage_roles=True, kick_members=True, ban_members=True, mute_members=True) async def warn(self, ctx: commands.Context): if ctx.invoked_subcommand is None: await ctx.invoke(self.warn_help) @warn.group("help", pass_context=True) async def warn_help(self, ctx: commands.Context): embed = Embed(title="Warn help") embed.add_field(name="add ", value="Send a warn to a user", inline=False) embed.add_field(name="remove ", value="Remove a number of warn to a user", inline=False) embed.add_field(name="purge ", value="Remove all warn of a user", inline=False) embed.add_field(name="list [user#discriminator|actions]", value="List warn of the guild or a specified user\n" "If you specify `actions` instead of a user, " "all the actions of the guild will be listed", inline=False) embed.add_field(name="action ", value="Set an action for a count of warn\n" "Actions: `mute