Пример #1
0
 async def on_guild_join(self, guild):
     members = guild.members
     members_ids = [member.id for member in members]
     server_id = guild.id
     with Database() as db:
         db.execute(
             'INSERT INTO "default".servers(discord_server_id) VALUES(%s) RETURNING id',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'INSERT INTO "default".servers_languages_and_vibes(server_id) VALUES(%s)',
             [db_server_id])
         db.execute(
             'INSERT INTO "default".servers_chats(server_id) VALUES (%s)',
             [db_server_id])
         for user_id in members_ids:
             if user_id == self.bot.user.id:
                 continue
             db.execute(
                 'SELECT id FROM "default".users WHERE discord_user_id = %s',
                 [user_id])
             if db.fetchone() is None:
                 db.execute(
                     'INSERT INTO "default".users(discord_user_id) VALUES(%s) RETURNING id',
                     [user_id])
             db.execute(
                 'SELECT id FROM "default".users WHERE discord_user_id = %s',
                 [user_id])
             db_user_id = db.fetchone()[0]
             db.execute(
                 'INSERT INTO "default".connect(server_id, user_id) VALUES(%s, %s)',
                 [db_server_id, db_user_id])
             db.execute(
                 'INSERT INTO "default".users_levels(server_id, user_id, level, xp) VALUES (%s, %s, %s, %s)',
                 [db_server_id, db_user_id, 0, 0])
Пример #2
0
    async def add_birthday(self, ctx, year: int, month: int, day: int):
        vocabulary = speech_setting(ctx.guild.id).birthdays
        if not (0 < year < datetime.date.today().year and 0 < month < 13
                and 0 < day < 32):
            return await ctx.send(
                choice(vocabulary.add_birthday.incorrect_date))
        user_id = ctx.message.author.id
        user_date = datetime.date(year, month, day)
        month, day = user_date.month, user_date.day
        with Database() as db:
            db.execute(
                'SELECT id FROM "default".users WHERE discord_user_id = %s',
                [user_id])
            db_user_id = db.fetchone()[0]
            db.execute(
                'SELECT date FROM "default".birthdays WHERE user_id = %s',
                [db_user_id])
            db_date = db.fetchone()
            if db_date:
                db_date = str(db_date[0]).replace("-", ".")
                return await ctx.send(
                    choice(vocabulary.add_birthday.date_already_exosts).format(
                        db_date))
            db.execute(
                'INSERT INTO "default".birthdays(user_id, date) VALUES (%s, %s)',
                [db_user_id, user_date.isoformat()])

            await ctx.send(
                choice(vocabulary.add_birthday.success).format(month, day))
Пример #3
0
 async def update_birthdays(self, ctx, year: int, month: int, day: int):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     if not (0 < year < datetime.date.today().year and 0 < month < 13
             and 0 < day < 32):
         return await ctx.send(
             choice(vocabulary.update_birthday.incorrect_date))
     user_id = ctx.message.author.id
     user_date = datetime.date(year, month, day)
     month, day = user_date.month, user_date.day
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".users WHERE discord_user_id = %s',
             [user_id])
         db_user_id = db.fetchone()[0]
         db.execute(
             'SELECT date FROM "default".birthdays WHERE user_id = %s',
             [db_user_id])
         db_date = db.fetchone()
         if db_date is None:
             return await ctx.send(
                 choice(vocabulary.update_birthday.date_does_not_exist_yet).
                 format(month, day))
         if db_date[0] == user_date:
             return await ctx.send(
                 choice(vocabulary.update_birthday.the_same_date))
         db.execute(
             'UPDATE "default".birthdays SET date = %s WHERE user_id = %s',
             [user_date, db_user_id])
         await ctx.send(choice(vocabulary.update_birthday.success))
Пример #4
0
 async def update_chat_birthday(self, ctx, chat: discord.TextChannel):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     server_id = ctx.guild.id
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'SELECT birthdays_chat FROM "default".servers_chats WHERE server_id = %s',
             [db_server_id])
         birthdays_chat_id = db.fetchone()[0]
         if not birthdays_chat_id:
             return await ctx.send(
                 choice(vocabulary.update_chat_birthday.chat_does_not_exist)
             )
         if birthdays_chat_id[0] == chat.id:
             return await ctx.send(
                 choice(vocabulary.update_chat_birthday.the_same_data))
         db.execute(
             'UPDATE "default".servers_chats SET birthdays_chat = %s WHERE server_id = %s',
             [chat.id, db_server_id])
     await ctx.send(
         choice(vocabulary.update_chat_birthday.success).format(
             chat.mention))
Пример #5
0
 async def show_birthdays(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     server = ctx.guild
     with Database() as db:
         output_embed = db.execute(
             '''SELECT discord_user_id, date 
                                      FROM "default".users, "default".birthdays 
                                      WHERE birthdays.user_id = users.id 
                                      AND users.id IN (
                                         SELECT user_id FROM "default".connect 
                                         WHERE server_id = (
                                             SELECT id FROM "default".servers 
                                             WHERE discord_server_id = %s 
                                         )
                                      ) ORDER BY date''',
             [server.id]).fetchall()
     if not output_embed:
         return await ctx.send(choice(vocabulary.show_birthdays.no_info))
     embed = discord.Embed(
         title=vocabulary.show_birthdays.title.format(server.name),
         description="\n".join([
             f"{pos + 1}. {get(server.members, id=value[0]).mention} - **{str(value[1]).replace('-', '.')}**"
             for pos, value in enumerate(output_embed)
         ]),
         colour=discord.Colour.purple())
     embed.set_thumbnail(url=server.icon_url)
     await ctx.send(embed=embed)
Пример #6
0
 async def set_chat_birthday(self, ctx, chat: discord.TextChannel):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     server_id = ctx.guild.id
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'SELECT birthdays_chat FROM "default".servers_chats WHERE server_id = %s',
             [db_server_id])
         birthdays_chat_id = db.fetchone()[0]
         if birthdays_chat_id:
             up, delete = commands_names.up_chat, commands_names.del_chat
             birthdays_chat = self.bot.get_channel(
                 birthdays_chat_id).mention
             return await ctx.send(
                 choice(vocabulary.set_chat_birthday.chat_already_exist).
                 format(birthdays_chat, up, delete))
         db.execute(
             'UPDATE "default".servers_chats SET birthdays_chat = %s WHERE server_id = %s',
             [chat.id, db_server_id])
         return await ctx.send(
             choice(vocabulary.set_chat_birthday.success).format(
                 chat.mention))
Пример #7
0
def speech_setting(server_id):
    with Database() as db:
        language, vibe = db.execute(
            'SELECT language, vibe FROM "default".servers_languages_and_vibes '
            'WHERE server_id = (SELECT id FROM "default".servers WHERE discord_server_id = %s)',
            [server_id]).fetchone()
        return getattr(vb[language], vibe)
Пример #8
0
def current_vibe(server_id):
    with Database() as db:
        vibe = db.execute(
            'SELECT vibe FROM "default".servers_languages_and_vibes '
            'WHERE server_id = (SELECT id FROM "default".servers WHERE discord_server_id = %s)',
            [server_id]).fetchone()[0]
        return vibe
Пример #9
0
 async def check_birthdays(self):
     with Database() as db:
         data = db.execute('''
             SELECT discord_user_id,
             date_part('year', CURRENT_DATE) - date_part('year', birthdays.date),
             discord_server_id,
             birthdays_chat
             FROM "default".users, "default".birthdays, "default".servers, "default".servers_chats
             WHERE users.id = birthdays.user_id
             AND servers.id = servers_chats.server_id
             AND (users.id, servers.id) IN (
                 SELECT user_id, server_id
                 FROM "default".connect
                 )
             AND birthdays_chat != 0
             AND date_part('month', CURRENT_DATE) = date_part('month', date)
             AND date_part('day', CURRENT_DATE) = date_part('day', date)'''
                           ).fetchall()
         for user_id, age, server_id, chat_id in data:
             user = self.bot.get_user(user_id)
             server = self.bot.get_guild(server_id)
             chat = self.bot.get_channel(chat_id)
             vocabulary = speech_setting(server.id).birthdays
             embed = discord.Embed(
                 title=vocabulary.check_birthdays.title.format(user.name),
                 description=vocabulary.check_birthdays.description.format(
                     user.mention, ':heart:', self.bot.user.mention),
                 colour=discord.Colour.purple())
             embed.set_thumbnail(url=user.avatar_url)
             await chat.send(embed=embed)
Пример #10
0
 async def check_level_up(self, message):
     vocabulary = speech_setting(message.guild.id).level_system
     if message.author.bot:
         return
     user = message.author
     user_id = user.id
     server = message.guild
     server_id = server.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_user_id = db.execute('SELECT id FROM "default".users WHERE discord_user_id = %s', [user_id]).fetchone()[0]
         levels = db.execute('SELECT level_id, level_xp FROM "default".servers_levels WHERE server_id = %s ORDER BY level_xp DESC ', [db_server_id]).fetchall()
         if not levels:
             return
         user_xp = db.execute('SELECT xp FROM "default".users_levels WHERE user_id = %s and server_id = %s', [db_user_id, db_server_id]).fetchone()[0]
         user_xp += 1
         db.execute('UPDATE "default".users_levels SET xp = %s WHERE user_id = %s and server_id = %s', [user_xp, db_user_id, db_server_id])
         for db_level, db_xp in levels:
             if user_xp == db_xp:
                 db.execute('UPDATE "default".users_levels SET level = %s WHERE user_id = %s and server_id = %s', [db_level, db_user_id, db_server_id])
                 mention = user.mention
                 role = get(server.roles, id=db_level)
                 await user.remove_roles(*[_role for _role in user.roles if _role in [_db_role for _db_role, _xp in levels]])
                 await user.add_roles(role)
                 embed = discord.Embed(
                     title=vocabulary.check_level_up.title,
                     description=choice(vocabulary.check_level_up.description_start).format(mention) + vocabulary.check_level_up.description_end.format(role.mention, user_xp),
                     colour=discord.Colour.purple()
                 )
                 embed.set_thumbnail(url=user.avatar_url)
                 info_chat = db.execute('SELECT info_chat FROM "default".servers_chats WHERE server_id = %s', [db_server_id]).fetchone()[0]
                 if info_chat:
                     return await self.bot.get_channel(info_chat).send(embed=embed)
                 return await message.channel.send(embed=embed)
Пример #11
0
 async def on_guild_remove(self, guild):
     members = guild.members
     members_ids = [member.id for member in members]
     server_id = guild.id
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'DELETE FROM "default".servers_levels WHERE server_id = %s',
             [db_server_id])
         db.execute(
             'DELETE FROM "default".servers_chats WHERE server_id = %s',
             [db_server_id])
         db.execute(
             'DELETE FROM "default".servers_languages_and_vibes WHERE server_id = %s',
             [db_server_id])
         for user_id in members_ids:
             db.execute(
                 'SELECT id FROM "default".users WHERE discord_user_id = %s',
                 [user_id])
             db_user_id = db.fetchone()[0]
             db.execute(
                 'DELETE FROM "default".connect WHERE server_id = %s and user_id = %s',
                 [db_server_id, db_user_id])
             db.execute(
                 'DELETE FROM "default".users_levels WHERE server_id = %s and user_id = %s',
                 [db_server_id, db_user_id])
         db.execute('DELETE FROM "default".servers WHERE id = %s',
                    [db_server_id])
Пример #12
0
    async def what_the_language_game(self, ctx):
        def check(text):
            return text.author != self.bot.user and not text.content.startswith(
                ".")

        vocabulary = speech_setting(ctx.guild.id).translator
        with Database() as db:
            server_id = db.execute(
                'SELECT id FROM "default".servers WHERE discord_server_id = %s',
                [ctx.guild.id]).fetchone()[0]
            language = db.execute(
                'SELECT language FROM "default".detect_language_game WHERE server_id = %s',
                [server_id]).fetchone()[0]
            if language != "no_lang":
                return await ctx.send(
                    choice(vocabulary.what_the_language_game.active_game))

            language = choice(list(self.google_translator_keys.keys()))
            counter = 3
            response = requests.get("https://zenquotes.io/api/random")
            json_data = json.loads(response.text)
            quote = json_data[0]['q']

            db.execute(
                'UPDATE "default".detect_language_game SET language = %s WHERE server_id = %s',
                [language, server_id])
            embed = discord.Embed(
                title=vocabulary.what_the_language_game.title,
                description=GoogleTranslator(source="en",
                                             target=language).translate(quote),
                colour=discord.Colour.purple())
            await ctx.send(embed=embed)
            while counter:
                print(language)
                counter -= 1
                msg = await self.bot.wait_for("message",
                                              check=check,
                                              timeout=30)
                if msg.content == db.execute(
                        'SELECT language FROM "default".detect_language_game WHERE server_id = %s',
                    [server_id]).fetchone()[0]:
                    db.execute(
                        'UPDATE "default".detect_language_game SET language = %s WHERE server_id = %s',
                        ["no_lang", server_id])
                    return await ctx.send(
                        choice(vocabulary.what_the_language_game.win))
                if counter:
                    await ctx.send(
                        vocabulary.what_the_language_game.tries.format(counter)
                    )
            await ctx.send(
                choice(vocabulary.what_the_language_game.lose_start) +
                vocabulary.what_the_language_game.lose_end.format(
                    language, self.google_translator_keys[language]))
            db.execute(
                'UPDATE "default".detect_language_game SET language = %s WHERE server_id = %s',
                ["no_lang", server_id])
Пример #13
0
 async def leave_join_message(self, member, message):
     with Database() as db:
         server_id = member.guild.id
         db.execute(
             'SELECT join_leave_chat FROM "default".servers_chats WHERE server_id = (SELECT id FROM "default".servers WHERE discord_server_id = %s)',
             [server_id])
         chat_id = db.fetchone()
         if chat_id:
             channel = self.bot.get_channel(chat_id[0])
             if channel:
                 await channel.send(member.mention, message)
Пример #14
0
 async def level_add(self, ctx, role: discord.Role, level_xp: int):
     vocabulary = speech_setting(ctx.guild.id).level_system
     level_id = role.id
     server_id = role.guild.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_level_id = db.execute('SELECT level_id FROM "default".servers_levels WHERE server_id = %s and level_id = %s', [db_server_id, level_id]).fetchone()
         db_level_id = db_level_id[0] if isinstance(db_level_id, tuple) else db_level_id
         if db_level_id:
             return await ctx.send(choice(vocabulary.level_add.the_same_data_start) + vocabulary.level_add.the_same_data_end)
         db.execute('INSERT INTO "default".servers_levels(server_id, level_id, level_xp) VALUES (%s, %s, %s)', [db_server_id, level_id, level_xp])
         await ctx.send(choice(vocabulary.level_add.success).format(role.mention))
Пример #15
0
 async def level_delete(self, ctx, role: discord.Role):
     vocabulary = speech_setting(ctx.guild.id).level_system
     level_id = role.id
     server_id = role.guild.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_level_id = db.execute('SELECT level_id FROM "default".servers_levels WHERE server_id = %s and level_id = %s', [db_server_id, level_id]).fetchone()
         db_level_id = db_level_id[0] if isinstance(db_level_id, tuple) else db_level_id
         if not db_level_id:
             return await ctx.send(choice(vocabulary.level_delete.level_does_not_exist_start) + vocabulary.level_delete.level_does_not_exist_end)
         db.execute('DELETE FROM "default".servers_levels WHERE server_id = %s and level_id = %s', [db_server_id, level_id])
         await ctx.send(choice(vocabulary.level_delete.success))
Пример #16
0
 async def set_vibe(self, ctx, vibe: str):
     vocabulary = speech_setting(ctx.guild.id).glossary
     if vibe not in vb[current_language(ctx.guild.id)].__dict__.keys():
         return await ctx.send(
             choice(vocabulary.set_vibe.incorrect_vibe).format(", ".join(
                 vb[current_language(ctx.guild.id)].__dict__.keys())))
     with Database() as db:
         db.execute(
             'UPDATE "default".servers_languages_and_vibes '
             'SET vibe = %s WHERE server_id = ('
             '    SELECT id FROM "default".servers'
             '    WHERE discord_server_id = %s'
             ')', [vibe, ctx.guild.id])
     vocabulary = speech_setting(ctx.guild.id).glossary
     await ctx.send(choice(vocabulary.set_vibe.success))
Пример #17
0
 async def level_update(self, ctx, role: discord.Role, level_xp: int):
     vocabulary = speech_setting(ctx.guild.id).level_system
     level_id = role.id
     server_id = role.guild.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_level_id = db.execute('SELECT level_id FROM "default".servers_levels WHERE server_id = %s and level_id = %s', [db_server_id, level_id]).fetchone()
         db_level_id = db_level_id[0] if isinstance(db_level_id, tuple) else db_level_id
         if not db_level_id:
             return await ctx.send(choice(vocabulary.level_update.level_does_not_exist_start) + vocabulary.level_update.level_does_not_exist_end)
         db_level_xp = db.execute('SELECT level_xp FROM "default".servers_levels WHERE server_id = %s and level_id = %s', [db_server_id, level_id]).fetchone()[0]
         if db_level_xp == level_xp:
             return await ctx.send(choice(vocabulary.level_update.the_same_data))
         db.execute('UPDATE "default".servers_levels SET level_xp = %s WHERE server_id = %s and level_id = %s', [level_xp, db_server_id, level_id])
         await ctx.send(choice(vocabulary.level_update.success_start) + vocabulary.level_update.success_end.format(role.mention, level_xp))
Пример #18
0
 async def view_status(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).glossary
     with Database() as db:
         language, vibe = db.execute(
             'SELECT language, vibe FROM "default".servers_languages_and_vibes '
             'WHERE server_id = ('
             '   SELECT id FROM "default".servers'
             '   WHERE discord_server_id = %s)', [ctx.guild.id]).fetchone()
         embed = discord.Embed(
             title=vocabulary.view_status.title.format(ctx.guild.name),
             description=vocabulary.view_status.description.format(
                 language, language, vibe),
             colour=discord.Colour.purple())
         embed.set_thumbnail(url=ctx.guild.icon_url)
         await ctx.send(embed=embed)
Пример #19
0
 async def show_chat_birthdays(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     server_id = ctx.guild.id
     with Database() as db:
         birthdays_chat_id = db.execute(
             '''SELECT birthdays_chat FROM "default".servers_chats
                                          WHERE server_id = (
                                              SELECT id FROM "default".servers
                                              WHERE discord_server_id = %s)''',
             [server_id]).fetchone()[0]
         if not birthdays_chat_id:
             return await ctx.send(
                 choice(vocabulary.show_chat_birthdays.no_info))
         birthdays_chat = self.bot.get_channel(birthdays_chat_id).mention
     await ctx.send(
         choice(
             vocabulary.show_chat_birthdays.success).format(birthdays_chat))
Пример #20
0
 async def delete_chat_birthday(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     server_id = ctx.guild.id
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'SELECT birthdays_chat FROM "default".servers_chats WHERE server_id = %s',
             [db_server_id])
         birthdays_chat_id = db.fetchone()[0]
         if not birthdays_chat_id:
             return await ctx.send(choice(vocabulary.delete_chat_birthday))
         db.execute(
             'UPDATE "default".servers_chats SET birthdays_chat = null WHERE server_id = %s',
             [db_server_id])
     await ctx.send(choice(vocabulary.delete_chat_birthday.success))
Пример #21
0
 async def delete_birthdays(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     user_id = ctx.message.author.id
     with Database() as db:
         db.execute(
             'SELECT id FROM "default".users WHERE discord_user_id = %s',
             [user_id])
         db_user_id = db.fetchone()[0]
         db.execute(
             'SELECT date FROM "default".birthdays WHERE user_id = %s',
             [db_user_id])
         db_date = db.fetchone()
         if db_date is None:
             return await ctx.send(
                 choice(vocabulary.delete_birthday.date_does_not_exist_yet))
         db.execute('DELETE FROM "default".birthdays WHERE user_id = %s',
                    [db_user_id])
         await ctx.send(choice(vocabulary.delete_birthday.success))
Пример #22
0
 async def level_dashboard(self, ctx, limit=10):
     vocabulary = speech_setting(ctx.guild.id).level_system
     server = ctx.guild
     server_id = server.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_server_levels = db.execute('SELECT level_id FROM "default".servers_levels WHERE server_id = %s', [db_server_id]).fetchall()
         if not db_server_levels:
             return await ctx.send(choice(vocabulary.level_dashboard.no_info))
         embed = discord.Embed(
             title=vocabulary.level_dashboard.title,
             colour=discord.Colour.purple()
         )
         embed.set_thumbnail(url=server.icon_url)
         db.execute('SELECT user_id, level, xp FROM "default".users_levels WHERE server_id = %s ORDER BY xp DESC LIMIT %s', [db_server_id, limit])
         data = []
         for db_user_id, db_user_level, db_user_xp in db.fetchall():
             user_id = db.execute('SELECT discord_user_id FROM "default".users WHERE id = %s', [db_user_id]).fetchone()[0]
             user = get(server.members, id=user_id)
             print(user, user_id)
             level = vocabulary.level_dashboard.no_level if not get(server.roles, id=db_user_level) else get(server.roles, id=db_user_level).mention
             data.append([user, level, db_user_xp])
         for i in range(min(3, len(data))):
             user, level, xp = data[i]
             embed.add_field(
                 name=f"{i + 1}. {user.name}",
                 value=f"{getattr(vocabulary.level_dashboard.top_three_phrases, str(i + 1))} {user.mention}\n**level: {level}\nxp: {xp}**",
                 inline=False
             )
         data = data[min(3, len(data)):]
         output = []
         for pos, line in enumerate(data):
             user, level, xp = line
             output.append(f"**{pos + 4}.{user.mention}** - {' '.join([level, f'**{xp} xp**'])}")
         if output:
             embed.add_field(
                 name=choice(vocabulary.level_dashboard.embed_field_title),
                 value="\n".join(output),
                 inline=False
             )
         await ctx.send(embed=embed)
Пример #23
0
 async def level(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).level_system
     server = ctx.guild
     server_id = server.id
     user = ctx.message.author
     user_id = user.id
     with Database() as db:
         db_user_id = db.execute('SELECT id FROM "default".users WHERE discord_user_id = %s', [user_id]).fetchone()[0]
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         db_server_levels = db.execute('SELECT level_id FROM "default".servers_levels WHERE server_id = %s', [db_server_id]).fetchall()
         if not db_server_levels:
             return await ctx.send(choice(vocabulary.level.no_info))
         db_user_level, db_user_xp = db.execute('SELECT level, xp FROM "default".users_levels WHERE server_id = %s and user_id = %s', [db_server_id, db_user_id]).fetchone()
         db_user_level = vocabulary.level.no_level if not get(server.roles, id=db_user_level) else get(server.roles, id=db_user_level).mention
         embed = discord.Embed(
             title=vocabulary.level.title.format(user.name),
             description=choice(vocabulary.level.description_start) + vocabulary.level.description_end.format(user.mention, db_user_level, db_user_xp),
             colour=discord.Colour.purple()
         )
         embed.set_thumbnail(url=user.avatar_url)
         await ctx.send(embed=embed)
Пример #24
0
 async def on_member_remove(self, member):
     if member == self.bot.user:
         return
     with Database() as db:
         user_id = member.id
         db.execute(
             'SELECT id FROM "default".users WHERE discord_user_id = %s',
             [user_id])
         db_user_id = db.fetchone()[0]
         server_id = member.guild.id
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'DELETE FROM "default".connect WHERE server_id = %s and user_id = %s',
             [db_server_id, db_user_id])
         db.execute(
             'DELETE FROM "default".users_levels WHERE user_id = %s and server_id = %s',
             [db_user_id, db_server_id])
     await self.leave_join_message(member, "покинул сервер")
Пример #25
0
 async def level_show(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).level_system
     server = ctx.guild
     server_id = server.id
     with Database() as db:
         db_server_id = db.execute('SELECT id FROM "default".servers WHERE discord_server_id = %s', [server_id]).fetchone()[0]
         levels = db.execute('SELECT level_id, level_xp FROM "default".servers_levels WHERE server_id = %s ORDER BY level_xp DESC', [db_server_id]).fetchall()
         if not levels:
             data = choice(vocabulary.level_show.no_info)
         else:
             data = ""
             for level, xp in levels:
                 role = get(server.roles, id=level)
                 data += f"\n**{role.mention}** - **{xp} xp**"
         embed = discord.Embed(
             title=choice(vocabulary.level_show.title),
             description=data,
             colour=discord.Colour.purple()
         )
         embed.set_thumbnail(url=server.icon_url)
         embed.set_footer(text=vocabulary.level_show.footer)
         await ctx.send(embed=embed)
Пример #26
0
 async def on_member_join(self, member):
     if member == self.bot.user:
         return
     with Database() as db:
         user_id = member.id
         db.execute(
             'SELECT id FROM "default".users WHERE discord_user_id = %s',
             [user_id])
         if db.fetchone() is None:
             db.execute(
                 'INSERT INTO "default".users(discord_user_id) VALUES(%s) RETURNING id',
                 [user_id])
         db.execute(
             'SELECT id FROM "default".users WHERE discord_user_id = %s',
             [user_id])
         db_user_id = db.fetchone()[0]
         server_id = member.guild.id
         db.execute(
             'SELECT id FROM "default".servers WHERE discord_server_id = %s',
             [server_id])
         db_server_id = db.fetchone()[0]
         db.execute(
             'SELECT id FROM "default".connect WHERE server_id = %s and user_id = %s',
             [db_server_id, db_user_id])
         if db.fetchone() is None:
             db.execute(
                 'INSERT INTO "default".connect(server_id, user_id) VALUES(%s, %s)',
                 [db_server_id, db_user_id])
         db.execute(
             'SELECT level FROM "default".users_levels WHERE user_id = %s and server_id = %s',
             [db_user_id, db_server_id])
         if db.fetchone() is None:
             db.execute(
                 'INSERT INTO "default".users_levels(server_id, user_id, level, xp) VALUES (%s, %s, %s, %s)',
                 [db_server_id, db_user_id, 0, 0])
     await self.leave_join_message(member, "присоединился к серверу")
Пример #27
0
 async def show_birthday(self, ctx):
     vocabulary = speech_setting(ctx.guild.id).birthdays
     user_id = ctx.message.author.id
     user = ctx.message.author
     with Database() as db:
         db_date = db.execute(
             '''SELECT date FROM "default".birthdays
                                 WHERE user_id = (
                                     SELECT id FROM "default".users
                                     WHERE discord_user_id = %s)''',
             [user_id]).fetchone()
         if db_date is None:
             return await ctx.send(
                 choice(vocabulary.show_birthday.date_does_not_exist_yet))
         db_date = str(db_date[0]).replace("-", ".")
         embed = discord.Embed(
             title=vocabulary.show_birthday.title.format(user.name),
             description=choice(
                 vocabulary.show_birthday.description_start).format(
                     user.mention) +
             vocabulary.show_birthday.description_end.format(db_date),
             colour=discord.Colour.purple())
         embed.set_thumbnail(url=user.avatar_url)
         await ctx.send(embed=embed)
Пример #28
0
 async def on_member_update(self, before, after):
     if before == self.bot.user:
         return
     if before.roles != after.roles:
         with Database() as db:
             db_user_id = db.execute(
                 'SELECT id FROM "default".users WHERE discord_user_id = %s',
                 [after.id]).fetchone()[0]
             db_server_id = db.execute(
                 'SELECT id FROM "default".servers WHERE discord_server_id = %s',
                 [after.guild.id]).fetchone()[0]
             db_levels = db.execute(
                 'SELECT level_id, level_xp FROM "default".servers_levels WHERE server_id = %s',
                 [db_server_id]).fetchall()
             if not db_levels:
                 return
             before_roles, after_roles = set(before.roles), set(after.roles)
             role = before_roles.symmetric_difference(after_roles).pop()
             if role.id not in [level for level, xp in db_levels]:
                 return
             user_level, user_xp = db.execute(
                 'SELECT level, xp FROM "default".users_levels WHERE server_id = %s and user_id = %s',
                 [db_server_id, db_user_id]).fetchone()
             db_xp = db.execute(
                 'SELECT level_xp FROM "default".servers_levels WHERE server_id = %s and level_id = %s',
                 [db_server_id, role.id]).fetchone()[0]
             info_chat = self.bot.get_channel(
                 db.execute(
                     'SELECT info_chat FROM "default".servers_chats WHERE server_id = %s',
                     [db_server_id]).fetchone()[0])
             if not info_chat:
                 return
             if db_xp > user_xp:
                 async for event in before.guild.audit_logs(
                         limit=1,
                         action=discord.AuditLogAction.member_role_update):
                     if event.user.bot:
                         return
                     if event.target.id != before.id:
                         continue
                     levels_to_remove = []
                     for _level, _xp in db_levels:
                         _level = get(after.guild.roles, id=_level)
                         if _level in after.roles and _level != role:
                             levels_to_remove.append(_level)
                     db.execute(
                         'UPDATE "default".users_levels SET level = %s, xp = %s WHERE server_id = %s and user_id = %s',
                         [role.id, db_xp, db_server_id, db_user_id])
                     embed = discord.Embed(
                         title=
                         ":chart_with_upwards_trend: LEVEL UP :chart_with_upwards_trend:",
                         description=
                         f"{after.mention} был повышен модератором {event.user.mention} до уровня {role.mention}. Мои поздравления!\n**level - {role.mention}\nxp - {db_xp}**",
                         colour=discord.Colour.purple())
                     embed.set_thumbnail(url=after.avatar_url)
                     await after.remove_roles(*levels_to_remove)
                     return await info_chat.send(embed=embed)
             elif db_xp <= user_xp:
                 async for event in before.guild.audit_logs(
                         limit=1,
                         action=discord.AuditLogAction.member_role_update):
                     if event.user.bot:
                         return
                     if event.target.id != before.id:
                         continue
                     db.execute(
                         'UPDATE "default".users_levels SET level = %s, xp = %s WHERE server_id = %s and user_id = %s',
                         [0, 0, db_server_id, db_user_id])
                     embed = discord.Embed(
                         title=
                         ":chart_with_downwards_trend: LEVEL DOWN :chart_with_downwards_trend:",
                         description=
                         f"{after.mention} был понижен модератором {event.user.mention}.\n**level - 0\nxp - 0**",
                         colour=discord.Colour.purple())
                     embed.set_thumbnail(url=after.avatar_url)
                     return await info_chat.send(embed=embed)