aboutsummaryrefslogtreecommitdiff
path: root/code/bot.py
blob: a96dca6ec2180f06e5916033d3027a1c0e06bec2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import discord
from discord.ext import commands, tasks
import os
import requests
import openai
import lyricsgenius

import utils.config as config
from utils.command_tree import Tree
from utils.media_api_key import get_media_api_key


class MyBot(commands.Bot):
    def __init__(self):
        super().__init__(
            command_prefix="***",
            activity=discord.Game(name="music!"),
            intents=discord.Intents.default(),
            tree_cls=Tree,
        )

    async def setup_hook(self):
        # Get Spotify, Apple Music, Genius, and OpenAI access tokens/clients
        get_access_token.start()
        refresh_media_api_key.start()
        login_genius.start()
        if config.OPENAI_API_KEY:
            bot.openai = openai.OpenAI(api_key=config.OPENAI_API_KEY)

        config.LOG.info("Loading cogs...")
        config.LOG.info(
            "YouTube support is enabled, make sure to set a poToken"
            if config.YOUTUBE_SUPPORT
            else "YouTube support is disabled"
        )
        for ext in os.listdir("./code/cogs"):
            if ext.endswith(".py"):
                # Load the OPTIONAL feedback cog
                if (
                    ext[:-3] == "feedback"
                    and config.FEEDBACK_CHANNEL_ID == None
                ):
                    config.LOG.info(
                        "Skipped loading feedback cog - channel ID not"
                        " provided"
                    )
                    continue
                # Load the OPTIONAL bug cog
                if ext[:-3] == "bug" and config.BUG_CHANNEL_ID == None:
                    config.LOG.info(
                        "Skipped loading bug cog - channel ID not provided"
                    )
                    continue
                # Load the OPTIONAL lyrics cog
                if ext[:-3] == "lyrics" and config.GENIUS_CLIENT_ID == None:
                    config.LOG.info(
                        "Skipped loading lyrics cog - Genius API credentials"
                        " not provided"
                    )
                    continue
                # Load the OPTIONAL autoplay cog
                if ext[:-3] == "autoplay" and config.OPENAI_API_KEY == None:
                    config.LOG.info(
                        "Skipped loading autoplay cog - OpenAI API credentials"
                        " not provided"
                    )
                    continue

                await self.load_extension(f"cogs.{ext[:-3]}")
        for ext in os.listdir("./code/cogs/owner"):
            if ext.endswith(".py"):
                await self.load_extension(f"cogs.owner.{ext[:-3]}")

    async def on_ready(self):
        config.LOG.info(f"{bot.user} has connected to Discord.")
        config.LOG.info(
            "Startup complete. Sync slash commands by DMing the bot"
            f" {bot.command_prefix}tree sync (guild id)"
        )


bot = MyBot()
bot.remove_command("help")
bot.temp_command_count = {}  # command_name: count
bot.autoplay = []  # guild_id, guild_id, etc.


@tasks.loop(minutes=45)
async def get_access_token():
    if config.SPOTIFY_CLIENT_ID and config.SPOTIFY_CLIENT_SECRET:
        auth_url = "https://accounts.spotify.com/api/token"
        data = {
            "grant_type": "client_credentials",
            "client_id": config.SPOTIFY_CLIENT_ID,
            "client_secret": config.SPOTIFY_CLIENT_SECRET,
        }
        response = requests.post(auth_url, data=data)
        if response.status_code == 200:
            access_token = response.json()["access_token"]
            bot.spotify_headers = {"Authorization": f"Bearer {access_token}"}
    else:
        bot.spotify_headers = None


@tasks.loop(hours=24)
async def refresh_media_api_key():
    media_api_key = get_media_api_key()
    if media_api_key is not None:
        bot.apple_headers = {
            "Authorization": f"Bearer {media_api_key}",
            "Origin": "https://apple.com",
        }
    else:
        bot.apple_headers = None


@tasks.loop(hours=1)
async def login_genius():
    if config.GENIUS_CLIENT_ID and config.GENIUS_CLIENT_SECRET:
        auth_url = "https://api.genius.com/oauth/token"
        data = {
            "client_id": config.GENIUS_CLIENT_ID,
            "client_secret": config.GENIUS_CLIENT_SECRET,
            "grant_type": "client_credentials",
        }
        response = requests.post(auth_url, data=data)
        if response.status_code == 200:
            access_token = response.json()["access_token"]
            bot.genius = lyricsgenius.Genius(access_token)
            bot.genius.verbose = False
    else:
        bot.genius = None


if __name__ == "__main__":
    config.load_config()
    bot.run(config.TOKEN)