Skip to content

Commit acb16c4

Browse files
committed
add music client
1 parent 83044c5 commit acb16c4

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies = [
2525
"pytest",
2626
"Pillow>=9.0.0",
2727
"requests>=2.20.0",
28-
"aiohttp>=3.8.0"
28+
"aiohttp>=3.8.0",
2929
]
3030

3131
[project.scripts]

src/zuffer/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import click
2-
from .commands import login #, login, config, create_rooms #, token
2+
from .commands import login
33
from .commands import create_channels
44
from .commands import embed
5-
from .commands import welcome, list
5+
from .commands import welcome, list, play_music
66

77
@click.group(invoke_without_command=True)
88
@click.version_option(package_name="zuffer")
@@ -22,3 +22,4 @@ def main(ctx: click.Context):
2222
main.add_command(create_channels.create_private)
2323
main.add_command(welcome.welcome_group)
2424
main.add_command(list.list_command)
25+
main.add_command(play_music.play_music)

src/zuffer/commands/play_music.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import click
2+
import questionary
3+
import discord
4+
from ..core import clients
5+
from ..core import auth
6+
7+
@click.command(name="play-music")
8+
@click.argument("playlist", required=True, type=click.Path(exists=True))
9+
def play_music(playlist):
10+
token = auth.get_token()
11+
if (token is not None):
12+
intents = discord.Intents.all()
13+
client = clients.MusicClient(playlist, intents=intents)
14+
client.run(token)
15+

src/zuffer/core/clients.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import requests
5+
import random
56
from io import BytesIO
67
from discord import Role, Member, Object
78
from typing import Mapping
@@ -243,3 +244,41 @@ async def on_member_join(self, member: discord.Member):
243244
else:
244245
print(f"Welcome channel with ID {welcome_channel_id} not found in guild {member.guild.name} or globally for bot.")
245246

247+
class MusicClient(discord.Client):
248+
def __init__(self, path_to_playlist, intents: discord.Intents):
249+
super().__init__(intents=intents)
250+
self.voice_recorders = {}
251+
self.playlist = path_to_playlist
252+
253+
async def on_ready(self):
254+
if (self.user is not None):
255+
print(f"Logged in as {self.user} (ID: {self.user.id})")
256+
print("------")
257+
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="Music"))
258+
async def on_message(self, message: discord.Message):
259+
if message.author == self.user:
260+
return
261+
if message.content.startswith("!join"):
262+
if isinstance(message.author, discord.Member) and message.author.voice and message.author.voice.channel:
263+
voice_channel = message.author.voice.channel
264+
try:
265+
voice_client = await voice_channel.connect()
266+
await message.channel.send(f"Joined {voice_channel.name}!")
267+
268+
if (os.path.isdir(self.playlist)):
269+
files = [os.path.join(self.playlist, f) for f in os.listdir(self.playlist) if f.endswith(".mp3")]
270+
def after_callback(error):
271+
if error:
272+
print(f'Player error: {error}')
273+
else:
274+
audio = discord.FFmpegPCMAudio(random.choice(files))
275+
voice_client.play(audio, after=after_callback)
276+
277+
audio_source = discord.FFmpegPCMAudio(random.choice(files))
278+
voice_client.play(audio_source, after=after_callback)
279+
280+
await message.channel.send("Now playing music on loop!")
281+
except Exception as e:
282+
await message.channel.send(f"Error joining voice channel: {str(e)}")
283+
else:
284+
await message.channel.send("You need to be in a voice channel first!")

0 commit comments

Comments
 (0)