Skip to content

Commit a33a84f

Browse files
author
Pedro Maia
committed
Bot Discord
1 parent 2bec487 commit a33a84f

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Discord/app.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import discord
2+
import os
3+
from discord.ext import commands
4+
from spotify import get_musics
5+
6+
TOKEN = os.getenv('DISCORD_TOKEN')
7+
8+
# Configurando intents
9+
intents = discord.Intents.default()
10+
intents.message_content = True # Necessário para acessar o conteúdo das mensagens
11+
12+
bot = commands.Bot(command_prefix="$", intents=intents)
13+
14+
@bot.event
15+
async def on_ready():
16+
print(f"Bot conectado como {bot.user}")
17+
18+
@bot.command(name="ping")
19+
async def ping(ctx):
20+
await ctx.send("Pong!")
21+
22+
@bot.command(name="playlist")
23+
async def music_list(ctx):
24+
if ctx.author.voice:
25+
channel = ctx.author.voice.channel
26+
if ctx.voice_client is not None and ctx.voice_client.channel == channel:
27+
pass
28+
else:
29+
instance = await channel.connect()
30+
await ctx.send("Gerando Playlist")
31+
musics = get_musics(ctx.message.content[10:])
32+
for music in musics:
33+
await ctx.send(f"{music}")
34+
await instance.disconnect()
35+
@bot.event
36+
async def on_message(message):
37+
if message.author == bot.user:
38+
return
39+
40+
if "olá" in message.content.lower():
41+
await message.channel.send(f"Olá, {message.author.name}!")
42+
43+
await bot.process_commands(message)
44+
45+
46+
47+
bot.run(TOKEN)

Discord/spotify.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import spotipy
2+
import os
3+
from spotipy.oauth2 import SpotifyClientCredentials
4+
5+
# Suas credenciais do Spotify Developer
6+
CLIENT_ID = os.getenv('CLIENT_ID_SPOTIFY')
7+
CLIENT_SECRET = os.getenv('CLIENT_SECRET_SPOTIFY')
8+
9+
# Configurar a autenticação
10+
auth_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
11+
sp = spotipy.Spotify(auth_manager=auth_manager)
12+
13+
def get_musics(playlist_url):
14+
playlist_id = playlist_url.split("/")[-1].split("?")[0]
15+
16+
# Buscar informações da playlist
17+
results = sp.playlist_items(playlist_id)
18+
19+
# Extrair e imprimir os nomes das músicas
20+
music_list = []
21+
for item in results['items']:
22+
name_track = item['track']['name']
23+
first_artist_name = item['track']['artists'][0]['name']
24+
music_list.append(f"m!p {name_track} - {first_artist_name}")
25+
26+
return music_list

0 commit comments

Comments
 (0)