added new env 'ownerid' -dbadd works anywhere

This commit is contained in:
Sleepingpirates
2020-06-12 04:25:00 +00:00
committed by repl.it user
parent 6e561e1f57
commit 7427a2445a
2 changed files with 161 additions and 152 deletions

2
.replit Normal file
View File

@@ -0,0 +1,2 @@
language = "python3"
run = ""

View File

@@ -1,152 +1,159 @@
#Copyright 2020 Sleepingpirate. #Copyright 2020 Sleepingpirate.
from os import environ from os import environ
import discord import discord
from discord.ext import commands from discord.ext import commands
import asyncio import asyncio
from plexapi.myplex import MyPlexAccount from plexapi.myplex import MyPlexAccount
from discord import Webhook, AsyncWebhookAdapter from discord import Webhook, AsyncWebhookAdapter
import aiohttp import aiohttp
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv(dotenv_path='bot.env') load_dotenv(dotenv_path='bot.env')
# settings # settings
Discord_bot_token = environ.get('discord_bot_token') Discord_bot_token = environ.get('discord_bot_token')
roleid = int(environ.get('roleid')) # Role Id, right click the role and copy id. roleid = int(environ.get('roleid')) # Role Id, right click the role and copy id.
PLEXUSER = environ.get('PLEXUSER') # Plex Username PLEXUSER = environ.get('PLEXUSER') # Plex Username
PLEXPASS = environ.get('PLEXPASS') # plex password PLEXPASS = environ.get('PLEXPASS') # plex password
PLEX_SERVER_NAME = environ.get('PLEX_SERVER_NAME') # Name of plex server PLEX_SERVER_NAME = environ.get('PLEX_SERVER_NAME') # Name of plex server
Plex_LIBS = environ.get('Plex_LIBS') #name of the libraries you want the user to have access to. Plex_LIBS = environ.get('Plex_LIBS') #name of the libraries you want the user to have access to.
chan = int(environ.get('channelid')) chan = int(environ.get('channelid'))
auto_remove_user = environ.get('autoremoveuser') if environ.get('autoremoveuser') else False # auto remove user from plex and db if removed from the role ownerid = int(environ.get('ownerid'))
auto_remove_user = environ.get('autoremoveuser') if environ.get('autoremoveuser') else False # auto remove user from plex and db if removed from the role
li = list(Plex_LIBS.split(','))
Plex_LIBS = li li = list(Plex_LIBS.split(','))
Plex_LIBS = li
if auto_remove_user:
print("auto remove user = True") if auto_remove_user:
import db as db print("auto remove user = True")
import db as db
account = MyPlexAccount(PLEXUSER, PLEXPASS)
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance account = MyPlexAccount(PLEXUSER, PLEXPASS)
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance
def plexadd(plexname):
try: def plexadd(plexname):
plex.myPlexAccount().inviteFriend(user=plexname, server=plex, sections=Plex_LIBS, allowSync=False, try:
allowCameraUpload=False, allowChannels=False, filterMovies=None, plex.myPlexAccount().inviteFriend(user=plexname, server=plex, sections=Plex_LIBS, allowSync=False,
filterTelevision=None, filterMusic=None) allowCameraUpload=False, allowChannels=False, filterMovies=None,
filterTelevision=None, filterMusic=None)
except Exception as e:
print(e) except Exception as e:
return False print(e)
else: return False
print(plexname +' has been added to plex (☞ຈل͜ຈ)☞') else:
return True print(plexname +' has been added to plex (☞ຈل͜ຈ)☞')
return True
def plexremove(plexname):
try: def plexremove(plexname):
plex.myPlexAccount().removeFriend(user=plexname) try:
except Exception as e: plex.myPlexAccount().removeFriend(user=plexname)
print(e) except Exception as e:
return False print(e)
else: return False
print(plexname +' has been removed from plex (☞ຈل͜ຈ)☞') else:
return True print(plexname +' has been removed from plex (☞ຈل͜ຈ)☞')
return True
class MyClient(discord.Client):
async def on_ready(self): class MyClient(discord.Client):
print('Made by Sleepingpirate https://github.com/Sleepingpirates/') async def on_ready(self):
print('Logged in as') print('Made by Sleepingpirate https://github.com/Sleepingpirates/')
print(self.user.name) print('Logged in as')
print(self.user.id) print(self.user.name)
print('------') print(self.user.id)
print('------')
async def on_member_update(self, before, after):
role = after.guild.get_role(roleid) async def on_member_update(self, before, after):
if (role in after.roles and role not in before.roles): role = after.guild.get_role(roleid)
await after.send('Welcome To '+ PLEX_SERVER_NAME +'. Just reply with your email so we can add you to Plex!') if (role in after.roles and role not in before.roles):
await after.send('I will wait 10 minutes for your message, if you do not send it by then I will cancel the command.') await after.send('Welcome To '+ PLEX_SERVER_NAME +'. Just reply with your email so we can add you to Plex!')
def check(m): await after.send('I will wait 10 minutes for your message, if you do not send it by then I will cancel the command.')
return m.author == after and not m.guild def check(m):
try: return m.author == after and not m.guild
email = await client.wait_for('message', timeout=600, check=check) try:
except asyncio.TimeoutError: email = await client.wait_for('message', timeout=600, check=check)
await after.send('Timed Out. Message Server Admin So They Can Add You Manually.') except asyncio.TimeoutError:
else: await after.send('Timed Out. Message Server Admin So They Can Add You Manually.')
await asyncio.sleep(5) else:
await after.send('Got it we will be processing your email shortly') await asyncio.sleep(5)
print(email.content) #make it go to a log channel await after.send('Got it we will be processing your email shortly')
plexname = str(email.content) print(email.content) #make it go to a log channel
if plexadd(plexname): plexname = str(email.content)
if auto_remove_user: if plexadd(plexname):
db.save_user(str(after.id), email.content) if auto_remove_user:
await asyncio.sleep(20) db.save_user(str(after.id), email.content)
await after.send('You have Been Added To Plex!') await asyncio.sleep(20)
secure = client.get_channel(chan) await after.send('You have Been Added To Plex!')
await secure.send(plexname + ' ' + after.mention + ' was added to plex') secure = client.get_channel(chan)
else: await secure.send(plexname + ' ' + after.mention + ' was added to plex')
await after.send('There was an error adding this email address. Message Server Admin.') else:
elif(role not in after.roles and role in before.roles): await after.send('There was an error adding this email address. Message Server Admin.')
if auto_remove_user: elif(role not in after.roles and role in before.roles):
try: if auto_remove_user:
user_id = after.id try:
email = db.get_useremail(user_id) user_id = after.id
plexremove(email) email = db.get_useremail(user_id)
deleted = db.delete_user(user_id) plexremove(email)
if deleted: deleted = db.delete_user(user_id)
print("Removed {} from db".format(email)) if deleted:
else: print("Removed {} from db".format(email))
print("Cannot remove this user from db.") else:
except: print("Cannot remove this user from db.")
print("Cannot remove this user from plex.") except:
print("Cannot remove this user from plex.")
async def on_message(self, message):
secure = client.get_channel(chan) async def on_message(self, message):
if message.author.id == self.user.id: secure = client.get_channel(chan)
return if message.author.id == self.user.id:
return
if str(message.channel) == str(secure):
if message.content.startswith('-plexadd'): if message.author.id == ownerid:
mgs = message.content.replace('-plexadd ','')
if plexadd(mgs): if message.content.startswith('-dbadd'):
await message.channel.send('The email has been added! {0.author.mention}'.format(message)) mgs = message.content.replace('-dbadd ','')
else: try:
message.channel.send('Error Check Logs! {0.author.mention}'.format(message)) mgs = mgs.split(' ')
if message.content.startswith('-plexrm'): email = mgs[0]
mgs = message.content.replace('-plexrm ','') bad_chars = ['<','>','@','!']
if plexremove(mgs): user_id = mgs[1]
await message.channel.send('The email has been removed! {0.author.mention}'.format(message)) for i in bad_chars:
else: user_id = user_id.replace(i, '')
message.channel.send('Error Check Logs! {0.author.mention}'.format(message)) db.save_user(user_id, email)
await message.channel.send('The user has been added to db!')
if message.content.startswith('-dbadd'): except:
mgs = message.content.replace('-dbadd ','') await message.channel.send('Cannot add this user to db.')
try: print("Cannot add this user to db.")
mgs = mgs.split(' ') await message.delete()
email = mgs[0]
bad_chars = ['<','>','@','!']
user_id = mgs[1] if str(message.channel) == str(secure):
for i in bad_chars: if message.content.startswith('-plexadd'):
user_id = user_id.replace(i, '') mgs = message.content.replace('-plexadd ','')
db.save_user(user_id, email) if plexadd(mgs):
await message.channel.send('The user {} has been added to db!'.format(mgs[0])) await message.channel.send('The email has been added! {0.author.mention}'.format(message))
except: else:
print("Cannot add this user to db.") message.channel.send('Error Check Logs! {0.author.mention}'.format(message))
if message.content.startswith('-plexrm'):
async def on_member_remove(self, member): mgs = message.content.replace('-plexrm ','')
if auto_remove_user: if plexremove(mgs):
try: await message.channel.send('The email has been removed! {0.author.mention}'.format(message))
user_id = member.id ## not there else:
email = db.get_useremail(user_id) message.channel.send('Error Check Logs! {0.author.mention}'.format(message))
plexremove(email)
deleted = db.delete_user(user_id)
if deleted: async def on_member_remove(self, member):
print("Removed {} from db".format(email)) if auto_remove_user:
else: try:
print("Cannot remove this user from db.") user_id = member.id ## not there
except: email = db.get_useremail(user_id)
print("Cannot remove this user from plex.") plexremove(email)
deleted = db.delete_user(user_id)
client = MyClient() if deleted:
client.run(Discord_bot_token) print("Removed {} from db".format(email))
else:
print("Cannot remove this user from db.")
except:
print("Cannot remove this user from plex.")
client = MyClient()
client.run(Discord_bot_token)