aboutsummaryrefslogtreecommitdiff
path: root/code/bot.py
blob: 4b3d96dcc37e932c39b7ff9e8dc43318a247563e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import discord
from discord.ext import commands
from discord.ext import tasks
import datetime
import sqlite3
import os

from validate_config import create_config
from global_variables import LOG, BOT_TOKEN


class MyBot(commands.Bot):
    def __init__(self):
        super().__init__(
            command_prefix="#",
            intents=discord.Intents.default(),
        )

    async def setup_hook(self):
        create_config()
        delete_old_temp_accounts.start()
        for ext in os.listdir("./code/cogs"):
            if ext.endswith(".py"):
                await self.load_extension(f"cogs.{ext[:-3]}")


bot = MyBot()
bot.remove_command("help")


@bot.event
async def on_ready():
    LOG.info(f"{bot.user} has connected to Discord.")


@tasks.loop(seconds=60)
async def delete_old_temp_accounts():
    # Delete all of the temporary Jellyfin accounts that have passed
    # their expiration time
    db = sqlite3.connect("cordarr.db")
    cursor = db.cursor()
    cursor.execute(
        "DELETE FROM jellyfin_accounts WHERE deletion_time < ?",
        (datetime.datetime.now(),),
    )
    db.commit()
    db.close()


if __name__ == "__main__":
    bot.run(BOT_TOKEN)