Skip to content

Commit

Permalink
Merge branch 'master' into playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
morgaesis committed Nov 20, 2021
2 parents a15158d + 4cb0dea commit e97bc0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
48 changes: 41 additions & 7 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jokeapi
import pafy
import youtubesearchpython
import pytube


class BotDispatcher(discord.Client):
Expand Down Expand Up @@ -111,6 +112,7 @@ def __init__(self, guild, loop, dispatcher_user):
r"http[s]?://"
r"(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
)
self.playlist_regex = re.compile(r"\b(?:play)?list\b\=(\w+)")

# Boolean to control whether the after callback is called
self.after_callback_blocked = False
Expand Down Expand Up @@ -260,10 +262,8 @@ def register_command( # pylint: disable=too-many-arguments
if guarded_by:

async def guarded_handler(*args):
logging.info("Aquiring lock")
async with guarded_by:
return await handler(*args)
logging.info("Releasing lock")

self.handlers[command_name] = guarded_handler
else:
Expand Down Expand Up @@ -358,7 +358,7 @@ def create_audio_source(self, audio_url):
"""Creates an audio sorce from an audio url"""
return discord.FFmpegPCMAudio(audio_url)

def next_in_queue(self):
def next_in_queue(self, notify=True):
"""
Switch to next song in queue
"""
Expand Down Expand Up @@ -386,11 +386,12 @@ def next_in_queue(self):
self.voice_client.play(audio_source, after=self.after_callback)
logging.info("Audio source started")

self.loop.create_task(
message.channel.send(
f":notes: Now Playing :notes:\n```\n{media.title}\n```"
if notify:
self.loop.create_task(
message.channel.send(
f":notes: Now Playing :notes:\n```\n{media.title}\n```"
)
)
)

async def create_or_get_voice_client(self, message):
"""Get a voice client to play audio.
Expand Down Expand Up @@ -470,6 +471,32 @@ async def notify_if_voice_client_is_missing(self, message):
return True
return False

async def playlist(self, message, command_content):
"""Play a playlist"""
logging.info("Fetching playlist for user %s", message.author)
playlist = pytube.Playlist(command_content)
added_videos = 0
failed_videos = 0
for video in playlist:
try:
media = pafy.new(video)
except KeyError as err:
logging.error(err)
failed_videos += 1
continue
self.media_queue.put((media, message))
added_videos += 1
if added_videos == 1 and not self.voice_client.is_playing():
self.next_in_queue(notify=False)
await message.channel.send(
f":notes: Now Playing :notes:\n```\n{media.title}\n```"
)
logging.info("%d items added to queue, %d failed", added_videos, failed_videos)
await message.channel.send(
f":clipboard: Added {added_videos} of "
f"{added_videos+failed_videos} songs to queue :notes:"
)

async def play(self, message, command_content):
"""
Play URL or first search term from command_content in the author's voice channel
Expand All @@ -479,6 +506,7 @@ async def play(self, message, command_content):
return

if not command_content:
# No search term/url
if self.voice_client.is_paused():
await self.resume(message, command_content)
elif not self.voice_client.is_playing() and not self.media_queue.empty():
Expand All @@ -495,12 +523,18 @@ async def play(self, message, command_content):
)
return

if re.findall(self.playlist_regex, command_content):
await self.playlist(message, command_content)
return

media = None
try:
if self.url_regex.match(command_content):
# url to video
logging.info("Fetching video metadata with pafy")
media = self.pafy_search(command_content)
else:
# search term
logging.info("Fetching search results with pafy")
search_result = self.youtube_search(command_content)
media = self.pafy_search(search_result["result"][0]["id"])
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pafy==0.5.5
pycparser==2.20
PyNaCl==1.4.0
python-dotenv==0.13.0
pytube==11.0.1
rfc3986==1.5.0
simplejson==3.17.0
six==1.16.0
Expand Down

0 comments on commit e97bc0e

Please sign in to comment.