# Path: dozer/cogs/actionlogs.py
# class CustomJoinLeaveMessages(db.DatabaseTable):
#     """Holds custom join leave messages"""
#     __tablename__ = 'memberlogconfig'
#     __uniques__ = 'guild_id'
# 
#     @classmethod
#     async def initial_create(cls):
#         """Create the table in the database"""
#         async with db.Pool.acquire() as conn:
#             await conn.execute(f"""
#             CREATE TABLE {cls.__tablename__} (
#             guild_id bigint PRIMARY KEY NOT NULL,	            
#             memberlog_channel bigint NOT NULL,	   
#             name varchar NOT NULL
#             )""")
# 
#     def __init__(self, guild_id, channel_id=None, ping=None, join_message=None, leave_message=None):
#         super().__init__()
#         self.guild_id = guild_id
#         self.channel_id = channel_id
#         self.ping = ping
#         self.join_message = join_message
#         self.leave_message = leave_message
# 
#     @classmethod
#     async def get_by(cls, **kwargs):
#         results = await super().get_by(**kwargs)
#         result_list = []
#         for result in results:
#             obj = CustomJoinLeaveMessages(guild_id=result.get("guild_id"), channel_id=result.get("channel_id"),
#                                           ping=result.get("ping"),
#                                           join_message=result.get("join_message"),
#                                           leave_message=result.get("leave_message"))
#             result_list.append(obj)
#         return result_list
# 
#     async def version_1(self):
#         """DB migration v1"""
#         async with db.Pool.acquire() as conn:
#             await conn.execute(f"""
#             alter table memberlogconfig rename column memberlog_channel to channel_id;
#             alter table memberlogconfig alter column channel_id drop not null;
#             alter table {self.__tablename__} drop column IF EXISTS name;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS ping boolean default False;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS join_message text default null;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS leave_message text default null;
#             """)
# 
#     __versions__ = [version_1]
# 
# Path: dozer/bot.py
# DOZER_LOGGER = logging.getLogger('dozer')


import asyncio
import time
import typing
import discord
import discord.utils
from discord.ext.commands import cooldown, BucketType, has_permissions, BadArgument, guild_only
from discord_slash import cog_ext, SlashContext
from ._utils import *
from .actionlogs import CustomJoinLeaveMessages
from .. import db
from ..bot import DOZER_LOGGER
from ..db import *
        top_restorable = me.top_role.position if me.guild_permissions.manage_roles else 0
        restore = await MissingRole.get_by(guild_id=member.guild.id, member_id=member.id)
        if len(restore) == 0:
            return  # New member - nothing to restore

        valid, cant_give, missing = set(), set(), set()
        for missing_role in restore:
            role = member.guild.get_role(missing_role.role_id)
            if role is None:  # Role with that ID does not exist
                missing.add(missing_role.role_name)
            elif role.position > top_restorable:
                cant_give.add(role.name)
            else:
                valid.add(role)
        for entry in restore:
            # Not missing anymore - remove the record to free up the primary key
            await MissingRole.delete(role_id=entry.role_id, member_id=entry.member_id)

        await member.add_roles(*valid)
        if not missing and not cant_give:
            return

        e = discord.Embed(title='Welcome back to the {} server, {}!'.format(member.guild.name, member),
                          color=discord.Color.blue())
        if missing:
            e.add_field(name='I couldn\'t restore these roles, as they don\'t exist.', value='\n'.join(sorted(missing)))
        if cant_give:
            e.add_field(name='I couldn\'t restore these roles, as I don\'t have permission.',
                        value='\n'.join(sorted(cant_give)))
        try:

# Path: dozer/cogs/actionlogs.py
# class CustomJoinLeaveMessages(db.DatabaseTable):
#     """Holds custom join leave messages"""
#     __tablename__ = 'memberlogconfig'
#     __uniques__ = 'guild_id'
# 
#     @classmethod
#     async def initial_create(cls):
#         """Create the table in the database"""
#         async with db.Pool.acquire() as conn:
#             await conn.execute(f"""
#             CREATE TABLE {cls.__tablename__} (
#             guild_id bigint PRIMARY KEY NOT NULL,	            
#             memberlog_channel bigint NOT NULL,	   
#             name varchar NOT NULL
#             )""")
# 
#     def __init__(self, guild_id, channel_id=None, ping=None, join_message=None, leave_message=None):
#         super().__init__()
#         self.guild_id = guild_id
#         self.channel_id = channel_id
#         self.ping = ping
#         self.join_message = join_message
#         self.leave_message = leave_message
# 
#     @classmethod
#     async def get_by(cls, **kwargs):
#         results = await super().get_by(**kwargs)
#         result_list = []
#         for result in results:
#             obj = CustomJoinLeaveMessages(guild_id=result.get("guild_id"), channel_id=result.get("channel_id"),
#                                           ping=result.get("ping"),
#                                           join_message=result.get("join_message"),
#                                           leave_message=result.get("leave_message"))
#             result_list.append(obj)
#         return result_list
# 
#     async def version_1(self):
#         """DB migration v1"""
#         async with db.Pool.acquire() as conn:
#             await conn.execute(f"""
#             alter table memberlogconfig rename column memberlog_channel to channel_id;
#             alter table memberlogconfig alter column channel_id drop not null;
#             alter table {self.__tablename__} drop column IF EXISTS name;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS ping boolean default False;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS join_message text default null;
#             alter table {self.__tablename__}
#                 add IF NOT EXISTS leave_message text default null;
#             """)
# 
#     __versions__ = [version_1]
# 
# Path: dozer/bot.py
# DOZER_LOGGER = logging.getLogger('dozer')


# Path: dozer/cogs/roles.py
import asyncio
import time
import typing
import discord
import discord.utils
from discord.ext.commands import cooldown, BucketType, has_permissions, BadArgument, guild_only
from discord_slash import cog_ext, SlashContext
from ._utils import *
from .actionlogs import CustomJoinLeaveMessages
from .. import db
from ..bot import DOZER_LOGGER
from ..db import *

        top_restorable = me.top_role.position if me.guild_permissions.manage_roles else 0
        restore = await MissingRole.get_by(guild_id=member.guild.id, member_id=member.id)
        if len(restore) == 0:
            return  # New member - nothing to restore

        valid, cant_give, missing = set(), set(), set()
        for missing_role in restore:
            role = member.guild.get_role(missing_role.role_id)
            if role is None:  # Role with that ID does not exist
                missing.add(missing_role.role_name)
            elif role.position > top_restorable:
                cant_give.add(role.name)
            else:
                valid.add(role)
        for entry in restore:
            # Not missing anymore - remove the record to free up the primary key
            await MissingRole.delete(role_id=entry.role_id, member_id=entry.member_id)

        await member.add_roles(*valid)
        if not missing and not cant_give:
            return

        e = discord.Embed(title='Welcome back to the {} server, {}!'.format(member.guild.name, member),
                          color=discord.Color.blue())
        if missing:
            e.add_field(name='I couldn\'t restore these roles, as they don\'t exist.', value='\n'.join(sorted(missing)))
        if cant_give:
            e.add_field(name='I couldn\'t restore these roles, as I don\'t have permission.',
                        value='\n'.join(sorted(cant_give)))
        try:

            dest_id = await CustomJoinLeaveMessages.get_by(guild_id=member.guild.id)