aboutsummaryrefslogtreecommitdiff
path: root/code/bot.py
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-08-03 02:55:32 +0000
committerGitHub <noreply@github.com>2024-08-03 02:55:32 +0000
commit608fad20d4f8cf977c39a47a9e30865bda57adfe (patch)
tree47b1ad56786f89c36d1c4eb0c67713c6b6a87d62 /code/bot.py
parent1bffa26776f6c80e2f2513d236f78ce9b7eea39f (diff)
parent3f23ef4edf5a33c3c184c8b92fc5143789cecbd1 (diff)
Merge pull request #2 from PacketParker/dev
Update main
Diffstat (limited to 'code/bot.py')
-rw-r--r--code/bot.py59
1 files changed, 48 insertions, 11 deletions
diff --git a/code/bot.py b/code/bot.py
index 2864e85..3569e68 100644
--- a/code/bot.py
+++ b/code/bot.py
@@ -3,6 +3,7 @@ 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
@@ -19,24 +20,38 @@ class MyBot(commands.Bot):
)
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...")
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]}")
- bot.openai = openai.OpenAI(api_key=config.OPENAI_API_KEY)
-
async def on_ready(self):
config.LOG.info(f"{bot.user} has connected to Discord.")
config.LOG.info(f"Startup complete. Sync slash commands by DMing the bot {bot.command_prefix}tree sync (guild id)")
@@ -50,15 +65,19 @@ bot.autoplay = [] # guild_id, guild_id, etc.
@tasks.loop(minutes=45)
async def get_access_token():
- 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)
- access_token = response.json()["access_token"]
- bot.spotify_headers = {"Authorization": f"Bearer {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)
@@ -73,6 +92,24 @@ async def refresh_media_api_key():
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)