예제 #1
0
def nickname_delay(guild_id):
    select = ("select   delay " + "       , type_delay " +
              "from     config_delay " + "where " +
              "          type_delay='nickname' " + "      and " +
              f"          guild_id='{guild_id}' " + ";" + "")
    try:
        fetched = database.fetch_one_line(select)
    except Exception as e:
        logger("Utils::nickname_delay", f"{type(e).__name__} - {e}")
    if fetched:
        return fetched[0]
    return None
예제 #2
0
def set_log_channel(table: str, channel_id: int, guild_id: int):
    sql = f"SELECT channel_id FROM {table} WHERE guild_id='{guild_id}'"
    is_already_set = database.fetch_one_line(sql)
    if is_already_set:
        sql = f"UPDATE {table} SET channel_id='{channel_id}' WHERE guild_id='{guild_id}'"
    else:
        sql = f"INSERT INTO {table} VALUES ('{channel_id}', '{guild_id}') ;"
    try:
        database.execute_order(sql, [])
    except Exception as e:
        logger("Utils::set_log_channel", f"{type(e).__name__} - {e}")
        return False
    return True
예제 #3
0
def get_roles_modo(guild_id):
    all_roles = []
    select = ("select   role_id " + "from     config_role " + "where " +
              "          permission=1 " + "      and " +
              f"          guild_id='{guild_id}' " + ";" + "")
    try:
        fetched = database.fetch_all_line(select)
    except Exception as e:
        logger("Utils::get_roles_modo", f"{type(e).__name__} - {e}")
    if fetched:
        for role_fetched in fetched:
            all_roles.append(int(role_fetched[0]))
    return all_roles
예제 #4
0
def format_time(timestamp):
    timer = [["j", 86400], ["h", 3600], ["m", 60], ["s", 1]]
    current = timestamp
    logger("Utils::format_time", f"current: {current}")
    to_ret = ""
    for obj_time in timer:
        if math.floor(current / obj_time[1]) > 0:
            to_ret += str(math.floor(
                current / obj_time[1])) + obj_time[0] + " "
            current = current % obj_time[1]
    if not len(to_ret):
        logger("Utils::format_time", "to ret is empty")
    return to_ret.strip()
예제 #5
0
def is_banned_role(command, member, guild_id):
    select = f"select until,role_id from ban_command_role where guild_id='{guild_id}' and command='{command}' ;"
    fetched = database.fetch_one_line(select)
    if fetched:
        try:
            role_id = int(fetched[1])
        except Exception as e:
            logger("Utils::is_banned_role", f"{type(e).__name__} - {e}")
            return True
        if has_role(role_id, member):
            try:
                until = int(fetched[0])
            except Exception as e:
                logger("Utils::is_banned_role", f"{type(e).__name__} - {e}")
                return True
            return until > math.floor(time.time())  # still ban
    return False
예제 #6
0
def is_authorized(member, guild_id):
    # Test server bypasses
    if guild_id == 494812563016777729:
        return True
    # admin can't be blocked
    try:
        if is_admin(member):
            return True
    except Exception as e:
        logger("Utils::is_authorized", f"{type(e).__name__} - {e}")
        return False
    # if perm
    try:
        if is_allowed(member, guild_id):
            return True
    except Exception as e:
        logger("Utils::is_authorized", f"{type(e).__name__} - {e}")
        return False
예제 #7
0
def convert_str_to_time(time_string):
    time_array = time_string.split(" ")
    logger("Utils::convert_str_to_time", f"time_array: {time_array}")
    timestamp = 0
    current = 0
    for element in time_array:
        logger("Utils::convert_str_to_time", f"element: {element}")
        if element.isnumeric():
            current = int(element)
            logger("Utils::convert_str_to_time",
                   f"isnumeric = current: {current}")
        else:
            if element == "months":
                current = current * 28 * 24 * 3600
            elif element == "weeks":
                current = current * 7 * 24 * 3600
            logger("Utils::convert_str_to_time", f"else current: {current}")
            timestamp = timestamp + current
            current = 0
    logger("Utils::convert_str_to_time", f"timestamp: {timestamp}")
    return timestamp
예제 #8
0
def is_banned(command, member, guild_id):
    # admin can't be blocked
    if is_admin(member):
        return False
    # ban user
    select = f"select until from ban_command_user where guild_id='{guild_id}' and user_id='{member.id}' and command='{command}' ;"
    fetched = database.fetch_one_line(select)
    if fetched:
        try:
            until = int(fetched[0])
        except Exception as e:
            logger("Utils::is_banned", f"{type(e).__name__} - {e}")
            return True
        if until > math.floor(time.time()):  # still ban
            return True
    # ban role
    select = f"select until,role_id from ban_command_role where guild_id='{guild_id}' and command='{command}' ;"
    fetched = database.fetch_all_line(select)
    if fetched:
        for line in fetched:
            try:
                role_id = int(line[1])
            except Exception as e:
                logger("Utils::is_banned", f"{type(e).__name__} - {e}")
                return True
            if has_role(role_id, member):
                try:
                    until = int(line[0])
                except Exception as e:
                    logger("Utils::is_banned", f"{type(e).__name__} - {e}")
                    return True
                if until > math.floor(time.time()):  # still ban
                    return True
    # neither
    return False
예제 #9
0
 async def decorated(*args, **kwargs):
     ctx = args[1]
     if 'authorized' in required:
         if not is_authorized(ctx.author, ctx.guild.id):
             logger("Utils::require::is_authorized",
                    "Missing permissions")
             return
     if 'not_banned' in required:
         if is_banned(ctx.command, ctx.author, ctx.guild.id):
             await ctx.send(
                 "Vous n'êtes pas autorisé à utiliser cette commande pour le moment."
             )
             await ctx.message.add_reaction('❌')
             return
     if 'cog_loaded' in required:
         if not is_loaded(ctx.cog.qualified_name.lower(), ctx.guild.id):
             if is_authorized(ctx.author, ctx.guild.id):
                 await ctx.send(
                     get_text(ctx.guild.id, "not_loaded").format(
                         ctx.command, ctx.cog.qualified_name.lower()))
                 await ctx.message.add_reaction('❌')
             return False
     return await f(*args, **kwargs)
예제 #10
0
def is_loaded(cog, guild_id):
    if (guild_id is None):
        logger("Utils::is_loaded", "is_loaded({0}, {1})".format(cog, guild_id))
    try:
        guild_id = int(guild_id)
        select = ("select   status "
                  "from     config_cog " + "where " + "cog=? " + " and " +
                  "guild_id=? ;" + "")
        try:
            fetched = database.fetch_one_line(select, [str(cog), guild_id])
        except Exception as e:
            logger("Utils::is_loaded", f"{type(e).__name__} - {e}")
        # logger ("Utils::is_loaded", f"In fetched for {str(cog)}: {fetched}")
        return (fetched and fetched[0] == 1) or (cog in [
            "configuration", "help", "loader", "logs"
        ])
    except Exception as e:
        logger("Utils::is_loaded", f"{type(e).__name__} - {e}")
        return False
예제 #11
0
async def confirm_command(message: discord.Message, status: bool):
    reaction = emojize(':white_check_mark:') if status else emojize(':x:')
    try:
        await message.add_reaction(reaction)
    except Exception as e:
        logger("Utils::confirm_command", f"{type(e).__name__} - {e}")