mirror of
https://github.com/jarulsamy/Plex-Bot.git
synced 2024-08-19 15:01:55 +02:00
🎉 Initial files
This commit is contained in:
parent
1f14571dc3
commit
a2b77ccc13
8
PlexBot/__init__.py
Normal file
8
PlexBot/__init__.py
Normal file
@ -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
|
19
PlexBot/__main__.py
Normal file
19
PlexBot/__main__.py
Normal file
@ -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)
|
54
PlexBot/bot.py
Normal file
54
PlexBot/bot.py
Normal file
@ -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!")
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
||||
discord.py==1.3.4
|
||||
PlexAPI==4.0.0
|
||||
fuzzywuzzy==0.18.0
|
||||
python-Levenshtein==0.12.0
|
Loading…
Reference in New Issue
Block a user