diff --git a/Dockerfile b/Dockerfile index d52c99d..b0e3583 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ -FROM python:3.7-slim +FROM python:3.12-slim LABEL maintainer="Joshua Arulsamy " # Install ffmpeg RUN apt-get -y update && \ - apt-get install -y --no-install-recommends ffmpeg=7:4.1.6-1~deb10u1 && \ + apt-get install -y --no-install-recommends ffmpeg=7:5.1.6-0+deb12u1 && \ apt-get autoremove -y && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* diff --git a/PlexBot/__main__.py b/PlexBot/__main__.py index 57d1982..3c9ff04 100644 --- a/PlexBot/__main__.py +++ b/PlexBot/__main__.py @@ -3,7 +3,9 @@ Sets up loggers and initiates bot. """ import logging +import asyncio +import discord from discord.ext.commands import Bot from . import load_config @@ -37,6 +39,13 @@ plex_log.setLevel(config["plex"]["log_level"]) bot_log.setLevel(config["discord"]["log_level"]) +intents = discord.Intents.default() +intents.guilds = True +intents.guild_messages = True +intents.message_content = True +intents.voice_states = True +intents.reactions = True + plex_args = { "base_url": BASE_URL, "plex_token": PLEX_TOKEN, @@ -44,9 +53,19 @@ "lyrics_token": LYRICS_TOKEN, } -bot = Bot(command_prefix=BOT_PREFIX) +bot = Bot(command_prefix=BOT_PREFIX, intents=intents) # Remove help command, we have our own custom one. bot.remove_command("help") -bot.add_cog(General(bot)) -bot.add_cog(Plex(bot, **plex_args)) -bot.run(TOKEN) +@bot.event +async def on_ready(): + print(f'Logged in as {bot.user.name} (ID: {bot.user.id})') + await bot.tree.sync() # Sync application commands here + +async def start_audio_player_task(): + await bot.cogs['Plex']._audio_player_task() + +async def main(): + await bot.add_cog(Plex(bot, **plex_args)) + await bot.start(TOKEN) + +asyncio.run(main()) diff --git a/PlexBot/bot.py b/PlexBot/bot.py index 8330f97..cb3435b 100644 --- a/PlexBot/bot.py +++ b/PlexBot/bot.py @@ -4,9 +4,14 @@ import logging from urllib.request import urlopen import requests +import sys import discord -from async_timeout import timeout +if sys.version_info.major == 3 and sys.version_info.minor >= 11: + from asyncio.exceptions import TimeoutError +else: + from asyncio import TimeoutError +import async_timeout from discord import FFmpegPCMAudio from discord.ext import commands from discord.ext.commands import command @@ -218,7 +223,8 @@ def __init__(self, bot, **kwargs): self.play_next_event = asyncio.Event() bot_log.info("Started bot successfully") - self.bot.loop.create_task(self._audio_player_task()) + def cog_load(self): + asyncio.create_task(self._audio_player_task()) def _search_tracks(self, title: str): """ @@ -301,21 +307,24 @@ async def _play(self): Raises: None """ - track_url = self.current_track.getStreamURL() - audio_stream = FFmpegPCMAudio(track_url) + if self.current_track is not None: + track_url = self.current_track.getStreamURL() + audio_stream = FFmpegPCMAudio(track_url) - while self.voice_channel and self.voice_channel.is_playing(): - bot_log.debug("waiting for track to finish") - await asyncio.sleep(2) - bot_log.debug("track finished") + while self.voice_channel and self.voice_channel.is_playing(): + bot_log.debug("waiting for track to finish") + await asyncio.sleep(2) + bot_log.debug("track finished") - if self.voice_channel: - self.voice_channel.play(audio_stream, after=self._toggle_next) + if self.voice_channel: + self.voice_channel.play(audio_stream, after=self._toggle_next) - plex_log.debug("%s - URL: %s", self.current_track, track_url) + plex_log.debug("%s - URL: %s", self.current_track, track_url) - embed, img = self._build_embed_track(self.current_track) - self.np_message_id = await self.ctx.send(embed=embed, file=img) + embed, img = self._build_embed_track(self.current_track) + self.np_message_id = await self.ctx.send(embed=embed, file=img) + else: + print("self.current_track is None") async def _play_next(self): try: @@ -362,7 +371,7 @@ async def _audio_player_task(self): if self.voice_channel: try: # Disconnect after 15 seconds idle - async with timeout(15): + async with async_timeout.timeout(15): await self._play_next() except asyncio.TimeoutError: bot_log("timeout - disconnecting") diff --git a/docker-compose.yml b/docker-compose.yml index 0e17d76..1a48063 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,11 +2,11 @@ version: "3" services: plex-bot: container_name: "PlexBot" - image: jarulsamy/plex-bot:latest + build: . environment: - PUID=1000 - PGID=1000 - - TZ=America/Denver + - TZ=Europe/Berlin # Required dir for configuration files volumes: - "./config:/config:ro" diff --git a/requirements.txt b/requirements.txt index 19193dd..05875cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,27 @@ -discord.py==1.4.1 -PlexAPI==4.0.0 -fuzzywuzzy==0.18.0 -pynacl==1.4.0 +aiohappyeyeballs==2.4.3 +aiohttp==3.10.10 +aiosignal==1.3.1 +async-timeout==4.0.3 +attrs==24.2.0 +beautifulsoup4==4.12.3 +certifi==2024.8.30 +cffi==1.17.1 +charset-normalizer==3.4.0 +discord.py==2.4.0 ffmpeg==1.4 -PyYAML==5.3.1 -lyricsgenius==2.0.0 +frozenlist==1.4.1 +fuzzywuzzy==0.18.0 +idna==3.10 +lyricsgenius==3.0.1 +multidict==6.1.0 +PlexAPI==4.15.16 +propcache==0.2.0 +pycparser==2.22 +PyNaCl==1.5.0 +PyYAML==6.0.2 +requests==2.32.3 +setuptools==75.1.0 +soupsieve==2.6 +urllib3==2.2.3 +wheel==0.44.0 +yarl==1.15.2