Skip to content

Commit

Permalink
Merge pull request #76 from Krutyi-4el/develop
Browse files Browse the repository at this point in the history
Refresh expired URLs
  • Loading branch information
solaluset authored Nov 18, 2023
2 parents 9453443 + b8a74e0 commit e9c6fcc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
17 changes: 8 additions & 9 deletions musicbot/audiocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from musicbot.bot import MusicBot


VC_TIMEOUT = 10
_not_provided = object()


Expand Down Expand Up @@ -100,7 +101,7 @@ async def register_voice_channel(self, channel: discord.VoiceChannel):
# to avoid ClientException: Not connected to voice
await asyncio.sleep(1)
else:
await channel.connect(reconnect=True)
await channel.connect(reconnect=True, timeout=VC_TIMEOUT)

def make_view(self):
if not self.is_active():
Expand Down Expand Up @@ -325,6 +326,7 @@ async def play_song(self, song: Song):
song.base_url,
before_options="-reconnect 1 -reconnect_streamed 1"
" -reconnect_delay_max 5",
options="-loglevel error",
),
after=self.next_song,
)
Expand Down Expand Up @@ -419,16 +421,13 @@ async def timeout_handler(self):
if not self.guild.voice_client:
return

if all(m.bot for m in self.guild.voice_client.channel.members):
await self.udisconnect()
return

sett = self.bot.settings[self.guild]

if not sett.vc_timeout or self.guild.voice_client.is_playing():
return

await self.udisconnect()
if sett.vc_timeout and (
not self.guild.voice_client.is_playing()
or all(m.bot for m in self.guild.voice_client.channel.members)
):
await self.udisconnect()

async def uconnect(self, ctx, move=False):
author_vc = ctx.author.voice
Expand Down
12 changes: 9 additions & 3 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import asyncio
from traceback import print_exception
from typing import Dict, Union, List

import discord
Expand All @@ -10,12 +11,13 @@
from sqlalchemy.orm import sessionmaker

from config import config
from musicbot.audiocontroller import AudioController
from musicbot.audiocontroller import VC_TIMEOUT, AudioController
from musicbot.settings import (
GuildSettings,
run_migrations,
extract_legacy_settings,
)
from musicbot.utils import CheckError


class MusicBot(bridge.Bot):
Expand Down Expand Up @@ -74,6 +76,8 @@ async def on_guild_join(self, guild):

async def on_command_error(self, ctx, error):
await ctx.send(error)
if not isinstance(error, CheckError):
print_exception(error)

async def on_application_command_error(self, ctx, error):
await self.on_command_error(ctx, error)
Expand All @@ -82,15 +86,17 @@ async def on_voice_state_update(self, member, before, after):
guild = member.guild
if member == self.user:
audiocontroller = self.audio_controllers[guild]
if after.channel:
if not guild.voice_client:
await asyncio.sleep(VC_TIMEOUT)
if guild.voice_client:
is_playing = guild.voice_client.is_playing()
await audiocontroller.timer.start(is_playing)
if is_playing:
# bot was moved, restore playback
await asyncio.sleep(1)
guild.voice_client.resume()
else:
# disconnected, clear state
# did not reconnect, clear state
await audiocontroller.udisconnect()
elif (
guild.voice_client
Expand Down
24 changes: 23 additions & 1 deletion musicbot/loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
import asyncio
import threading
from urllib.request import urlparse
from datetime import datetime, timezone
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import get_context as mp_context
from typing import List, Tuple, Optional, Union
Expand Down Expand Up @@ -129,6 +131,7 @@ def _load_song(track: str) -> Union[Optional[Song], List[Song]]:
return None

data = search_youtube(track)
host = linkutils.Sites.YouTube

elif host == linkutils.Sites.Spotify:
title = _loop.run_until_complete(linkutils.convert_spotify(track))
Expand Down Expand Up @@ -223,8 +226,27 @@ def _preload(song: Song) -> Optional[Song]:


async def preload(song: Song) -> bool:
if song.info.title is not None or song.info.webpage_url is None:
if song.base_url is not None:
if song.host not in (linkutils.Sites.YouTube, linkutils.Sites.Spotify):
return True

expire = (
("&" + urlparse(song.base_url).query)
.partition("&expire=")[2]
.partition("&")[0]
)
try:
expire = int(expire)
except ValueError:
return True
if datetime.now(timezone.utc) < datetime.fromtimestamp(
expire, timezone.utc
):
return True

if song.info.webpage_url is None:
return True

future = _preloading.get(song)
if future:
return await future
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
py-cord[voice] @ https://github.com/Krutyi-4el/pycord/archive/8393a5379d2e1fe119f0fb0d8c297cda0d8c1f90.zip
yt-dlp==2023.7.6
aiohttp==3.8.5
yt-dlp==2023.10.13
aiohttp==3.8.6
beautifulsoup4==4.12.2
spotipy==2.23.0
emoji==2.8.0
SQLAlchemy[asyncio]==2.0.21
alembic==1.12.0
SQLAlchemy[asyncio]==2.0.23
alembic==1.12.1
aioconsole==0.6.2
./config

0 comments on commit e9c6fcc

Please sign in to comment.