From a2b77ccc137cee07edcc2e221072ef428a365a31 Mon Sep 17 00:00:00 2001 From: Joshua Arulsamy Date: Wed, 22 Jul 2020 22:21:41 -0600 Subject: [PATCH] :tada: Initial files --- PlexBot/__init__.py | 8 +++++++ PlexBot/__main__.py | 19 ++++++++++++++++ PlexBot/bot.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 4 files changed, 85 insertions(+) create mode 100644 PlexBot/__init__.py create mode 100644 PlexBot/__main__.py create mode 100644 PlexBot/bot.py create mode 100644 requirements.txt diff --git a/PlexBot/__init__.py b/PlexBot/__init__.py new file mode 100644 index 0000000..b809c0b --- /dev/null +++ b/PlexBot/__init__.py @@ -0,0 +1,8 @@ +import yaml + + +def load_config(filename: str) -> None: + with open(filename, "r") as f: + config = yaml.safe_load(f) + + return config diff --git a/PlexBot/__main__.py b/PlexBot/__main__.py new file mode 100644 index 0000000..5e365a4 --- /dev/null +++ b/PlexBot/__main__.py @@ -0,0 +1,19 @@ +from discord.ext.commands import Bot + +from .bot import General +from .bot import Plex +from PlexBot import load_config + +config = load_config("config.yaml") + +BOT_PREFIX = config["discord"]["prefix"] +TOKEN = config["discord"]["token"] + +BASE_URL = config["plex"]["base_url"] +PLEX_TOKEN = config["plex"]["token"] +LIBRARY_NAME = config["plex"]["library_name"] + +bot = Bot(command_prefix=BOT_PREFIX) +bot.add_cog(General(bot)) +bot.add_cog(Plex(bot, BASE_URL, PLEX_TOKEN, LIBRARY_NAME)) +bot.run(TOKEN) diff --git a/PlexBot/bot.py b/PlexBot/bot.py new file mode 100644 index 0000000..7b4a789 --- /dev/null +++ b/PlexBot/bot.py @@ -0,0 +1,54 @@ +import discord +from discord.ext import commands +from discord.ext.commands import command +from fuzzywuzzy import fuzz +from plexapi.server import PlexServer + + +class General(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @command() + async def stop(self, ctx): + await ctx.send(f"Stopping upon the request of {ctx.author.mention}") + await self.bot.close() + + +class Plex(commands.Cog): + def __init__(self, bot, base_url, plex_token, lib_name) -> None: + self.bot = bot + self.base_url = base_url + self.plex_token = plex_token + self.library_name = lib_name + + self.pms = PlexServer(self.base_url, self.plex_token) + self.music = self.pms.library.section(self.library_name) + + def _search_tracks(self, title): + tracks = self.music.searchTracks() + score = [[None], -1] + for i in tracks: + # scores[i].append(fuzz.ratio(title.lower(), i.lower)) + s = fuzz.ratio(title.lower(), i.title.lower()) + if s > score[1]: + score[0] = [i] + score[1] = s + elif s == score[1]: + score[0].append(i) + + return score + + @command() + async def hello(self, ctx, *, member: discord.member = None): + member = member or ctx.author + await ctx.send(f"Hello {member}") + + @command() + async def play(self, ctx, *args): + title = " ".join(args) + track = self._search_tracks(title) + if track[0][0]: + await ctx.send(track[0][0].title) + else: + await ctx.send("Song not found!") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ac20814 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +discord.py==1.3.4 +PlexAPI==4.0.0 +fuzzywuzzy==0.18.0 +python-Levenshtein==0.12.0