Plex setup and roles setup now thru bot

This commit is contained in:
Faiz Ahmed
2021-07-26 12:19:37 -04:00
parent ceb8bc32a3
commit 49c9e68706
8 changed files with 231 additions and 113 deletions

View File

@@ -1,22 +1,48 @@
import logging
import discord
from discord.ext import commands
import asyncio
from plexapi.myplex import MyPlexAccount
from discord import Webhook, AsyncWebhookAdapter
from app.bot.helper.confighelper import roles, PLEXUSER, PLEXPASS, PLEX_SERVER_NAME, Plex_LIBS
logging.basicConfig(filename="app/config/invitarr.log", filemode='a', level=logging.ERROR)
import app.bot.helper.db as db
import app.bot.helper.plexhelper as plexhelper
import texttable
import os
from os import path
import configparser
CONFIG_PATH = 'app/config/config.ini'
BOT_SECTION = 'bot_envs'
# settings
roles = None
PLEXUSER = ""
PLEXPASS = ""
PLEX_SERVER_NAME = ""
Plex_LIBS = ["all"]
if(path.exists('app/config/config.ini')):
try:
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
except:
pass
if(path.exists('app/config/config.ini')):
try:
roles = config.get(BOT_SECTION, 'roles')
except:
pass
try:
account = MyPlexAccount(PLEXUSER, PLEXPASS)
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance
logging.info('Logged into plex!')
print('Logged into plex!')
except:
logging.error('Error with plex login. Please check username and password and Plex server name.')
print('Error with plex login. Please check username and password and Plex server name or setup plex in the bot.')
if roles is not None:
roles = list(roles.split(','))
class app(commands.Cog):
@@ -25,9 +51,11 @@ class app(commands.Cog):
@commands.Cog.listener()
async def on_ready(self):
logging.info('Made by Sleepingpirate https://github.com/Sleepingpirates/')
logging.info(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
logging.info('------')
print('Made by Sleepingpirate https://github.com/Sleepingpirates/')
print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
print('------')
if roles is None:
print('Configure roles to enable auto invite after a role is assigned.')
async def embederror(self, author, message):
embed1 = discord.Embed(title="ERROR",description=message, color=0xf50000)
@@ -58,10 +86,11 @@ class app(commands.Cog):
await self.embederror(after, message)
return None
async def addtoplex(self, email, channel):
if(plexhelper.verifyemail(email)):
if plexhelper.plexadd(plex,email):
await self.embedinfo(channel, 'There was an error adding this email address. Message Server Admin.')
await self.embedinfo(channel, 'This email address has been added to plex')
return True
else:
await self.embederror(channel, 'There was an error adding this email address. Check logs.')
@@ -73,7 +102,7 @@ class app(commands.Cog):
async def removefromplex(self, email, channel):
if(plexhelper.verifyemail(email)):
if plexhelper.plexadd(plex,email):
await self.embedinfo(channel, 'There was an error removing this email address. Message Server Admin.')
await self.embedinfo(channel, 'This email address has been removed from plex.')
return True
else:
await self.embederror(channel, 'There was an error removing this email address. Check logs.')
@@ -81,11 +110,11 @@ class app(commands.Cog):
else:
await self.embederror(channel, 'Invalid email.')
return False
#Auto add or remove user from plex if role is given or taken.
@commands.Cog.listener()
async def on_member_update(self, before, after):
if roles is None:
return
roles_in_guild = after.guild.roles
role = None
for role_for_app in roles:
@@ -112,25 +141,23 @@ class app(commands.Cog):
plexremove(email)
deleted = db.delete_user(user_id)
if deleted:
logging.info("Removed {} from db".format(email))
print("Removed {} from db".format(email))
#await secure.send(plexname + ' ' + after.mention + ' was removed from plex')
else:
logging.error("Cannot remove this user from db.")
print("Cannot remove this user from db.")
except:
logging.error("Cannot remove this user from plex.")
print("Cannot remove this user from plex.")
return
@commands.has_permissions(administrator=True)
@commands.command()
async def plexinvite(self, ctx):
email = str(ctx.content)
async def plexinvite(self, ctx, email):
await self.addtoplex(email, ctx.channel)
@commands.has_permissions(administrator=True)
@commands.command()
async def plexremove(self, ctx):
email = str(ctx.content)
async def plexremove(self, ctx, email):
await self.removefromplex(email, ctx.channel)
@commands.has_permissions(administrator=True)
@@ -143,7 +170,7 @@ class app(commands.Cog):
await self.embedinfo(ctx.channel,'email and user were added to the database.')
except Exception as e:
await self.embedinfo(ctx.channel, 'There was an error adding this email address to database.')
logging.error(e)
print(e)
else:
await self.embederror(ctx.channel, 'Invalid email.')
@@ -209,12 +236,12 @@ class app(commands.Cog):
email = db.get_useremail(id)
deleted = db.delete_user(id)
if deleted:
logging.info("Removed {} from db".format(email))
print("Removed {} from db".format(email))
await self.embedinfo(ctx.channel,"Removed {} from db".format(email))
else:
await self.embederror(ctx.channel,"Cannot remove this user from db.")
except Exception as e:
logging.error(e)
print(e)
def setup(bot):
bot.add_cog(app(bot))