mirror of
https://github.com/jarulsamy/Plex-Bot.git
synced 2024-08-19 15:01:55 +02:00
🔊 Add basic logging
This commit is contained in:
parent
4043c972c0
commit
ac25330f70
@ -1,8 +1,36 @@
|
|||||||
import yaml
|
import yaml
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
FORMAT = "%(asctime)s %(levelname)s: [%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
|
||||||
|
|
||||||
|
logging.basicConfig(format=FORMAT)
|
||||||
|
logger = logging.getLogger("PlexBot")
|
||||||
|
|
||||||
|
|
||||||
def load_config(filename: str) -> None:
|
def load_config(filename: str) -> Dict[str, str]:
|
||||||
|
|
||||||
|
# All config files should be in /config
|
||||||
|
# for docker deployment.
|
||||||
|
filename = Path("/config", filename)
|
||||||
|
try:
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
config = yaml.safe_load(f)
|
config = yaml.safe_load(f)
|
||||||
|
except FileNotFoundError:
|
||||||
|
logging.fatal("Configuration file not found.")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
# Convert str level type to logging constant
|
||||||
|
levels = {
|
||||||
|
"DEBUG": logging.DEBUG,
|
||||||
|
"INFO": logging.INFO,
|
||||||
|
"WARNING": logging.WARNING,
|
||||||
|
"ERROR": logging.ERROR,
|
||||||
|
"CRITICAL": logging.CRITICAL,
|
||||||
|
}
|
||||||
|
level = config["general"]["log_level"]
|
||||||
|
config["general"]["log_level"] = levels[level.upper()]
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
@ -3,7 +3,11 @@ from discord.ext.commands import Bot
|
|||||||
from .bot import General
|
from .bot import General
|
||||||
from .bot import Plex
|
from .bot import Plex
|
||||||
from PlexBot import load_config
|
from PlexBot import load_config
|
||||||
|
from . import FORMAT
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# Load config from file
|
||||||
config = load_config("config.yaml")
|
config = load_config("config.yaml")
|
||||||
|
|
||||||
BOT_PREFIX = config["discord"]["prefix"]
|
BOT_PREFIX = config["discord"]["prefix"]
|
||||||
@ -12,6 +16,12 @@ TOKEN = config["discord"]["token"]
|
|||||||
BASE_URL = config["plex"]["base_url"]
|
BASE_URL = config["plex"]["base_url"]
|
||||||
PLEX_TOKEN = config["plex"]["token"]
|
PLEX_TOKEN = config["plex"]["token"]
|
||||||
LIBRARY_NAME = config["plex"]["library_name"]
|
LIBRARY_NAME = config["plex"]["library_name"]
|
||||||
|
LOG_LEVEL = config["general"]["log_level"]
|
||||||
|
|
||||||
|
# Set appropiate log level
|
||||||
|
logger = logging.getLogger("PlexBot")
|
||||||
|
logging.basicConfig(format=FORMAT)
|
||||||
|
logger.setLevel(LOG_LEVEL)
|
||||||
|
|
||||||
bot = Bot(command_prefix=BOT_PREFIX)
|
bot = Bot(command_prefix=BOT_PREFIX)
|
||||||
bot.add_cog(General(bot))
|
bot.add_cog(General(bot))
|
||||||
|
@ -7,6 +7,10 @@ from discord.ext.commands import command
|
|||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
from plexapi.server import PlexServer
|
from plexapi.server import PlexServer
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger("PlexBot")
|
||||||
|
|
||||||
|
|
||||||
class General(commands.Cog):
|
class General(commands.Cog):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
@ -16,6 +20,7 @@ class General(commands.Cog):
|
|||||||
async def kill(self, ctx):
|
async def kill(self, ctx):
|
||||||
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
||||||
await self.bot.close()
|
await self.bot.close()
|
||||||
|
logger.info(f"Stopping upon the request of {ctx.author.mention}")
|
||||||
|
|
||||||
|
|
||||||
class Plex(commands.Cog):
|
class Plex(commands.Cog):
|
||||||
@ -29,7 +34,11 @@ class Plex(commands.Cog):
|
|||||||
self.music = self.pms.library.section(self.library_name)
|
self.music = self.pms.library.section(self.library_name)
|
||||||
|
|
||||||
self.vc = None
|
self.vc = None
|
||||||
|
self.current_track = None
|
||||||
self.play_queue = Queue()
|
self.play_queue = Queue()
|
||||||
|
|
||||||
|
logger.info("Started bot successfully")
|
||||||
|
|
||||||
# self.callback_ctx = None
|
# self.callback_ctx = None
|
||||||
|
|
||||||
def _search_tracks(self, title):
|
def _search_tracks(self, title):
|
||||||
@ -51,9 +60,13 @@ class Plex(commands.Cog):
|
|||||||
await ctx.send(f"Hello {member}")
|
await ctx.send(f"Hello {member}")
|
||||||
|
|
||||||
async def _after_callback(self, error=None):
|
async def _after_callback(self, error=None):
|
||||||
|
if self.play_queue.empty():
|
||||||
|
self.current_track = None
|
||||||
|
else:
|
||||||
track = self.play_queue.get()
|
track = self.play_queue.get()
|
||||||
audio_stream = FFmpegPCMAudio(track.getStreamURL())
|
audio_stream = FFmpegPCMAudio(track.getStreamURL())
|
||||||
self.vc.play(audio_stream)
|
self.vc.play(audio_stream)
|
||||||
|
self.current_track = track
|
||||||
await self.callback_ctx.send(f"Playing {track.title}")
|
await self.callback_ctx.send(f"Playing {track.title}")
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
@ -67,16 +80,21 @@ class Plex(commands.Cog):
|
|||||||
return
|
return
|
||||||
if not self.vc:
|
if not self.vc:
|
||||||
self.vc = await ctx.author.voice.channel.connect()
|
self.vc = await ctx.author.voice.channel.connect()
|
||||||
|
logger.debug("Connected to vc")
|
||||||
|
|
||||||
if self.vc.is_playing():
|
if self.vc.is_playing():
|
||||||
self.play_queue.put(track)
|
self.play_queue.put(track)
|
||||||
self.callback_ctx = ctx
|
self.callback_ctx = ctx
|
||||||
await ctx.send(f"Added {track.title} to queue.")
|
await ctx.send(f"Added {track.title} to queue.")
|
||||||
|
logger.debug(f"Added {track.title} to queue.")
|
||||||
else:
|
else:
|
||||||
audio_stream = FFmpegPCMAudio(track_url)
|
audio_stream = FFmpegPCMAudio(track_url)
|
||||||
self.vc.play(audio_stream, after=self._after_callback)
|
self.vc.play(audio_stream, after=self._after_callback)
|
||||||
|
self.current_track = track
|
||||||
|
logger.debug(f"Playing {track.title}")
|
||||||
await ctx.send(f"Playing {track.title}")
|
await ctx.send(f"Playing {track.title}")
|
||||||
else:
|
else:
|
||||||
|
logger.debug(f"{title} was not found.")
|
||||||
await ctx.send("Song not found!")
|
await ctx.send("Song not found!")
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
@ -85,7 +103,6 @@ class Plex(commands.Cog):
|
|||||||
self.vc.stop()
|
self.vc.stop()
|
||||||
await self.vc.disconnect()
|
await self.vc.disconnect()
|
||||||
self.vc = None
|
self.vc = None
|
||||||
|
|
||||||
await ctx.send("Stopped")
|
await ctx.send("Stopped")
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
@ -106,3 +123,7 @@ class Plex(commands.Cog):
|
|||||||
await self.vc.stop()
|
await self.vc.stop()
|
||||||
if not self.play_queue.empty():
|
if not self.play_queue.empty():
|
||||||
await self._after_callback()
|
await self._after_callback()
|
||||||
|
|
||||||
|
@command()
|
||||||
|
async def np(self, ctx):
|
||||||
|
await ctx.send(f"Currently playing: {self.current_track.title}")
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
general:
|
||||||
|
# Options: debug, info, warning, error, critical
|
||||||
|
log_level: "info"
|
||||||
|
|
||||||
discord:
|
discord:
|
||||||
prefix: "?"
|
prefix: "?"
|
||||||
token: "<BOT_TOKEN>"
|
token: "<BOT_TOKEN>"
|
||||||
|
Loading…
Reference in New Issue
Block a user