aboutsummaryrefslogtreecommitdiff
path: root/code/bot.py
blob: 27b17f0a28a700ab7a033f2e8de745a4de7839f5 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import discord
from discord.ext import commands, tasks
import os
import zipfile
import tqdm
import requests

from validate_config import create_config
from database import init_database
from global_variables import LOG, BOT_TOKEN, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET


def unpack_reels():
    # Files ending in .zip within /code/utils, extract it to /code/utils
    for file in os.listdir("code/utils"):
        # If folders exist, don't unpack
        if (os.path.exists("code/utils/winning_reels") and
                os.path.exists("code/utils/losing_reels_1") and
                os.path.exists("code/utils/losing_reels_2") and
                os.path.exists("code/utils/losing_reels_3") and
                os.path.exists("code/utils/losing_reels_4") and
                os.path.exists("code/utils/losing_reels_5")):
            break
        if file.endswith(".zip"):
            with zipfile.ZipFile(f"code/utils/{file}", 'r') as zip_ref:
                for member in tqdm.tqdm(iterable=zip_ref.namelist(), total=len(zip_ref.namelist()), desc=f"Unpacking {file}..."):
                    zip_ref.extract(member, "code/utils")


class MyBot(commands.Bot):
    def __init__(self):
        super().__init__(
            command_prefix='***',
            activity = discord.Game(name="Ping Me For Help!"),
            intents = discord.Intents.default()
        )
    async def setup_hook(self):
        unpack_reels()
        create_config()
        get_access_token.start()
        await init_database()
        for ext in os.listdir('./code/cogs'):
            if ext.endswith('.py'):
                await self.load_extension(f'cogs.{ext[:-3]}')


bot = MyBot()
bot.count_hold = 0
bot.remove_command('help')


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


@tasks.loop(minutes=45)
async def get_access_token():
    auth_url = 'https://accounts.spotify.com/api/token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': SPOTIFY_CLIENT_ID,
        'client_secret': SPOTIFY_CLIENT_SECRET
    }
    response = requests.post(auth_url, data=data)
    access_token = response.json()['access_token']
    bot.access_token = access_token

class InsufficientFundsException(Exception):
    def __init__(self) -> None:
        super().__init__()


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