DOC: Renamed Spotify_cog to Spotify_module (makes more sense)

FIX: Fixed Issues of Selena not starting up
This commit is contained in:
Dan 2024-06-20 22:12:52 -04:00
parent 29d9ee5432
commit 726ec91a79
6 changed files with 91 additions and 66 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run Selena",
"type": "debugpy",
"request": "launch",
"program": "E:\\Development\\AI Development\\Selena\\main.py",
"console": "integratedTerminal"
}
]
}

View File

@ -1,60 +0,0 @@
import discord
from discord import app_commands
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import config
class SpotifyCog(discord.Cog):
def __init__(self, bot):
self.bot = bot
self.sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=config.SPOTIFY_CLIENT_ID,
client_secret=config.SPOTIFY_CLIENT_SECRET,
redirect_uri=config.SPOTIFY_REDIRECT_URI,
scope=(
"user-library-read user-read-playback-state "
"user-modify-playback-state user-read-currently-playing"
)
)
)
@app_commands.command(
name="current_track", description="Get the currently playing song"
)
async def current_track(self, interaction: discord.Interaction):
current = self.sp.currently_playing()
if current is None or current["item"] is None:
await interaction.response.send_message(
"No song is currently playing"
)
else:
track = current["item"]
artist = ", ".join([artist["name"] for artist in track["artists"]])
await interaction.response.send_message(
f"Currently playing: {track['name']} by {artist} "
f"({track['album']['name']})"
)
@app_commands.command(
name="play_track", description="Play a track by searching for it"
)
async def play_track(self, interaction: discord.Interaction, query: str):
results = self.sp.search(q=query, limit=1, type="track")
if not results["tracks"]["items"]:
await interaction.response.send_message("No results found")
return
track = results["tracks"]["items"][0]
uri = track["uri"]
self.sp.start_playback(uris=[uri])
await interaction.response.send_message(
f"Now playing: {track['name']} by "
f"{', '.join([artist['name'] for artist in track['artists']])} "
f"({track['album']['name']})"
)
async def setup(bot):
await bot.add_cog(SpotifyCog(bot))

View File

@ -4,6 +4,6 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID") SPOTIPY_CLIENT_ID = os.getenv("SPOTIPY_CLIENT_ID")
SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET") SPOTIPY_CLIENT_SECRET = os.getenv("SPOTIPY_CLIENT_SECRET")
SPOTIFY_REDIRECT_URI = os.getenv("SPOTIFY_REDIRECT_URI") SPOTIPY_REDIRECT_URI = os.getenv("SPOTIPY_REDIRECT_URI")

10
main.py
View File

@ -8,12 +8,16 @@ class Selena(discord.Client):
self.tree = discord.app_commands.CommandTree(self) self.tree = discord.app_commands.CommandTree(self)
async def setup_hook(self): async def setup_hook(self):
for extension in ["cogs.spotify_cog"]: await self.load_extension("modules.spotify_module")
await self.load_extension(extension)
await self.tree.sync() await self.tree.sync()
async def load_extension(self, name):
module = __import__(name, fromlist=["setup"])
await module.setup(self)
async def on_ready(self): async def on_ready(self):
print("Logged in as {0.user}".format(self)) print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
if __name__ == "__main__": if __name__ == "__main__":

66
modules/spotify_module.py Normal file
View File

@ -0,0 +1,66 @@
import discord
from discord import app_commands
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import config
class SpotifyCog:
def __init__(self, bot):
self.bot = bot
self.sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=config.SPOTIPY_CLIENT_ID,
client_secret=config.SPOTIPY_CLIENT_SECRET,
redirect_uri=config.SPOTIPY_REDIRECT_URI,
scope=(
"user-library-read user-read-playback-state "
"user-modify-playback-state user-read-currently-playing"
)
)
)
self.add_commands()
def add_commands(self):
@app_commands.command(
name="current_track", description="Get the currently playing song"
)
async def current_track(interaction: discord.Interaction):
current = self.sp.currently_playing()
if current is None or current["item"] is None:
await interaction.response.send_message(
"No song is currently playing"
)
else:
track = current["item"]
artist = ", ".join([artist["name"] for artist in track[
"artists"]])
await interaction.response.send_message(
f"Currently playing: {track['name']} by {artist} "
f"({track['album']['name']})"
)
@app_commands.command(
name="play_track", description="Play a track by searching for it"
)
async def play_track(interaction: discord.Interaction, query: str):
results = self.sp.search(q=query, limit=1, type="track")
if not results["tracks"]["items"]:
await interaction.response.send_message("No results found")
return
track = results["tracks"]["items"][0]
uri = track["uri"]
self.sp.start_playback(uris=[uri])
await interaction.response.send_message(
f"Now playing: {track['name']} by "
f"{', '.join([artist['name'] for artist in track['artists']])}"
f"({track['album']['name']})"
)
self.bot.tree.add_command(current_track)
self.bot.tree.add_command(play_track)
async def setup(bot):
SpotifyCog(bot)