Beispiel #1
0
    async def channel_link_remove(
        self,
        ctx: SubContext,
        send_from: CrossGuildTextChannelConverter,
        send_to: CrossGuildTextChannelConverter,
    ):
        send_from: discord.TextChannel
        send_to: discord.TextChannel

        self.bot.channel_links[send_from].discard(send_to)

        async with db.get_database() as connection:
            if self.bot.channel_links[send_from]:
                await connection.execute(
                    "INSERT INTO channel_links (send_from, send_to) VALUES (?, ?) "
                    "ON CONFLICT (send_from) DO UPDATE SET send_to = EXCLUDED.send_to;",
                    (send_from.id,
                     {c.id
                      for c in self.bot.channel_links[send_from]}),
                )
            else:
                await connection.execute(
                    "DELETE FROM channel_links WHERE send_from = ?;",
                    (send_from.id, ))
                del self.bot.channel_links[send_from]

            await connection.commit()

        await ctx.confirm("Channels unlinked.")
Beispiel #2
0
    async def get_all_config() -> Optional[list]:
        async with db.get_database() as conn:
            cursor = await conn.execute("SELECT * FROM grubninja_settings;")
            res = await cursor.fetchall()

        if res:
            return list(res)
Beispiel #3
0
 async def on_guild_remove(self, guild: discord.Guild):
     self.bot.prefixes.pop(guild.id)
     async with db.get_database() as connection:
         await connection.execute(
             "DELETE FROM prefixes WHERE guild_id is ?;", (guild.id,)
         )
         await connection.commit()
Beispiel #4
0
    async def remove(self, ctx: SubContext, prefix: str):
        """
        Remove a prefix from this guild
        """
        if prefix not in self.bot.prefixes[ctx.guild.id]:
            return await ctx.send("Prefix not in this guild's prefixes.")

        async with db.get_database() as conn:
            async with conn.cursor() as cursor:
                # It's the only one in the guild so just reset to default prefix
                # rather than an empty set
                if len(self.bot.prefixes[ctx.guild.id]) == 1:
                    del self.bot.prefixes[ctx.guild.id]
                    await cursor.execute(
                        "DELETE FROM prefixes WHERE guild_id IS (?)",
                        (ctx.guild.id, ))

                else:
                    self.bot.prefixes[ctx.guild.id].remove(prefix)
                    await cursor.execute(
                        "UPDATE prefixes SET prefixes=? WHERE guild_id IS ?;",
                        (self.bot.prefixes[ctx.guild.id], ctx.guild.id),
                    )
            await conn.commit()

        await ctx.confirm()
Beispiel #5
0
    async def get_bot_ratings(bot_id):
        async with db.get_database() as connection:
            cursor = await connection.execute(
                "SELECT user_id, rating, review FROM ratings WHERE bot_id = (?)",
                (bot_id,),
            )

            return await cursor.fetchall()
Beispiel #6
0
    async def clear_bot_rating(bot_id, user_id):
        async with db.get_database() as connection:
            await connection.execute(
                "DELETE FROM ratings WHERE bot_id = (?) AND user_id = (?);",
                (bot_id, user_id),
            )

            await connection.commit()
Beispiel #7
0
    async def set_bot_rating(bot_id, user_id, rating, review):
        async with db.get_database() as connection:
            await connection.execute(
                "INSERT OR REPLACE INTO ratings (bot_id, user_id, rating, review) VALUES (?, ?, ?, ?);",
                (bot_id, user_id, rating, review),
            )

            await connection.commit()
Beispiel #8
0
    async def get_config(key: str) -> Optional[str]:
        async with db.get_database() as conn:
            cursor = await conn.execute(
                'SELECT "value" FROM grubninja_settings WHERE key = (?);',
                (key, ))
            res = await cursor.fetchone()

        if res:
            return res[0]
Beispiel #9
0
    async def set_config(key: str, value: str) -> None:
        async with db.get_database() as conn:
            await conn.execute(
                'INSERT INTO grubninja_settings (key, "value") VALUES (?, ?) '
                'ON CONFLICT (key) DO UPDATE SET "value" = EXCLUDED.value;',
                (key, value),
            )

            await conn.commit()
Beispiel #10
0
    async def get_bot_rating(bot_id, user_id):
        async with db.get_database() as connection:
            cursor = await connection.execute(
                "SELECT rating, review FROM ratings WHERE bot_id = (?) AND user_id = (?);",
                (bot_id, user_id),
            )

            res = await cursor.fetchone()

            if res:
                return res
Beispiel #11
0
    async def blacklist_remove(self, ctx: SubContext, user: Union[discord.User,
                                                                  int]):
        if isinstance(user, discord.User):
            user = user.id

        # No need to conflict check bc owner command
        del self.bot.blacklist[user]

        async with db.get_database() as connection:
            await connection.execute(
                "DELETE FROM blacklist WHERE user_id = (?)", (user, ))

        await ctx.confirm("Removed from blacklist.")
Beispiel #12
0
    async def blacklist_add(self,
                            ctx: SubContext,
                            user: Union[discord.User, int],
                            *,
                            reason: str = None):
        if isinstance(user, discord.User):
            user = user.id

        # No need to conflict check bc owner command
        self.bot.blacklist[user] = reason

        async with db.get_database() as connection:
            await connection.execute(
                "INSERT INTO blacklist (user_id, reason) VALUES (?, ?)",
                (user, reason))

        await ctx.confirm("Added to blacklist.")
Beispiel #13
0
    async def add(self, ctx: SubContext, prefix: str):
        """
        Adds a prefix to this guild
        """
        # it should never be over 20 but just to be sure
        if len(self.bot.prefixes[ctx.guild.id]) >= 20:
            return await ctx.send(
                "Guild at max prefixes of 20, remove one to add this one.")
        elif prefix in self.bot.prefixes[ctx.guild.id]:
            return await ctx.send("Prefix already added.")

        self.bot.prefixes[ctx.guild.id].add(prefix)
        async with db.get_database() as conn:
            async with conn.cursor() as cursor:
                await cursor.execute(
                    "INSERT INTO prefixes (guild_id, prefixes) VALUES (?, ?) "
                    "ON CONFLICT (guild_id) DO UPDATE SET prefixes = EXCLUDED.prefixes;",
                    (ctx.guild.id, self.bot.prefixes[ctx.guild.id]),
                )
            await conn.commit()

        await ctx.confirm()
Beispiel #14
0
    async def load_channel_links(self):
        """
        Loads channel links into memory as TextChannels
        """
        async with db.get_database() as connection:
            cursor = await connection.execute("SELECT * FROM channel_links;")
            for send_from, send_to in await cursor.fetchall():
                send_from_channel = self.bot.get_channel(send_from)
                if not send_from_channel:
                    logger.info(f"{send_from} is no longer accessable.")
                    continue

                channels = set()
                for channel_id in send_to:
                    channel = self.bot.get_channel(int(channel_id))
                    if channel:
                        channels.add(channel)
                    else:
                        logger.info(
                            f"{channel_id} is no longer accessable. (linked to {send_from_channel.name})"
                        )

                self.bot.channel_links[send_from_channel] = channels
Beispiel #15
0
    async def jsk_db(self, ctx: commands.Context, *, quarry: str):
        """
        Execute a db quarry
        """
        async with db.get_database() as connection:
            try:
                cursor = await connection.execute(quarry)
            except OperationalError as e:
                return await ctx.send(str(e))

            await connection.commit()
            quarry_result = await cursor.fetchall()

            if quarry_result:
                collums = [coll[0] for coll in cursor.description]
                final = [collums]

                for data in quarry_result:
                    final.append(list(data))

                table = AsciiTable(final)

                paginator = PartitionPaginator(prefix="```sql",
                                               max_size=1000,
                                               wrap_on=("|", "\n"))

                paginator.add_line(table.table)

                source = NormalPageSource(paginator.pages)

                menu = DCMenuPages(source)

                await menu.start(ctx)

            else:
                await ctx.send("[no result]")