Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM python:3.7-slim
FROM python:3.12-slim

LABEL maintainer="Joshua Arulsamy <joshua.gf.arul@gmail.com>"

# 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/*
Expand Down
27 changes: 23 additions & 4 deletions PlexBot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,16 +39,33 @@
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,
"lib_name": LIBRARY_NAME,
"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())
37 changes: 23 additions & 14 deletions PlexBot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 26 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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