Various fixes and some improvements (#49)
Rip out bot refs Various fixes and some improvements Signed-off-by: Etzelia <etzelia@hotmail.com> Co-authored-by: Etzelia <etzelia@hotmail.com> Reviewed-by: ZeroHD <joey@ahines.net>captcha
parent
3bb1e910c9
commit
faa342933e
37
api/api.py
37
api/api.py
|
@ -40,35 +40,30 @@ def plugin(key, command):
|
||||||
def discord_mcm(message='', embed=None, ping=False):
|
def discord_mcm(message='', embed=None, ping=False):
|
||||||
discord_mcm_webhook = getattr(settings, 'DISCORD_MCM_WEBHOOK', None)
|
discord_mcm_webhook = getattr(settings, 'DISCORD_MCM_WEBHOOK', None)
|
||||||
if discord_mcm_webhook:
|
if discord_mcm_webhook:
|
||||||
ping_list = getattr(settings, 'DISCORD_PING_LIST', [])
|
return post_webhook(discord_mcm_webhook, message, embed, ping)
|
||||||
if ping and ping_list:
|
return None
|
||||||
ping_list = ["<@&{0}>".format(ping) for ping in ping_list]
|
|
||||||
message = "{0}\n{1}".format(" ".join(ping_list), message)
|
|
||||||
data = {}
|
|
||||||
if message:
|
|
||||||
data['content'] = message
|
|
||||||
if embed:
|
|
||||||
data['embeds'] = embed
|
|
||||||
return requests.post(discord_mcm_webhook, json=data)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def discord_notification(message='', embed=None, ping=False):
|
def discord_notification(message='', embed=None, ping=False):
|
||||||
discord_notification_webhook = getattr(settings, 'DISCORD_NOTIFICATION_WEBHOOK', None)
|
discord_notification_webhook = getattr(settings, 'DISCORD_NOTIFICATION_WEBHOOK', None)
|
||||||
if discord_notification_webhook:
|
if discord_notification_webhook:
|
||||||
ping_list = getattr(settings, 'DISCORD_PING_LIST', [])
|
return post_webhook(discord_notification_webhook, message, embed, ping)
|
||||||
if ping and ping_list:
|
|
||||||
ping_list = ["<@&{0}>".format(ping) for ping in ping_list]
|
|
||||||
message = "{0}\n{1}".format(" ".join(ping_list), message)
|
|
||||||
data = {}
|
|
||||||
if message:
|
|
||||||
data['content'] = message
|
|
||||||
if embed:
|
|
||||||
data['embeds'] = embed
|
|
||||||
return requests.post(discord_notification_webhook, json=data)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def post_webhook(webhook_url, message, embed, ping):
|
||||||
|
ping_list = getattr(settings, 'DISCORD_PING_LIST', [])
|
||||||
|
if ping and ping_list:
|
||||||
|
ping_list = ["<@&{0}>".format(ping) for ping in ping_list]
|
||||||
|
message = "{0}\n{1}".format(" ".join(ping_list), message)
|
||||||
|
data = {}
|
||||||
|
if message:
|
||||||
|
data['content'] = message
|
||||||
|
if embed:
|
||||||
|
data['embeds'] = [embed.to_dict()]
|
||||||
|
return requests.post(webhook_url, json=data)
|
||||||
|
|
||||||
|
|
||||||
def strip_format(message):
|
def strip_format(message):
|
||||||
return message.replace("§0", "").replace("§1", "").replace("§2", "").replace("§3", "").replace("§4", "") \
|
return message.replace("§0", "").replace("§1", "").replace("§2", "").replace("§3", "").replace("§4", "") \
|
||||||
.replace("§5", "").replace("§6", "").replace("§7", "").replace("§8", "").replace("§9", "").replace("§a", "") \
|
.replace("§5", "").replace("§6", "").replace("§7", "").replace("§8", "").replace("§9", "").replace("§a", "") \
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
from django.conf import settings
|
|
||||||
import subprocess, os
|
|
||||||
|
|
||||||
|
|
||||||
class Bot:
|
|
||||||
plugin_port = getattr(settings, 'PLUGIN_PORT', None)
|
|
||||||
bot_dir = getattr(settings, 'BOT_DIR', "")
|
|
||||||
if not bot_dir.endswith("/"):
|
|
||||||
bot_dir += "/"
|
|
||||||
|
|
||||||
def __init__(self, name, asset, executable=None, start=None, stop=None, restart=None, status=None, display=None):
|
|
||||||
self.name = name
|
|
||||||
self.asset = asset
|
|
||||||
self.executable = executable
|
|
||||||
self.start = start if start else self._start
|
|
||||||
self.stop = stop if stop else self._stop
|
|
||||||
self.restart = restart if restart else self._restart
|
|
||||||
self.status = status if status else self._status
|
|
||||||
self.display = display if display else self._display
|
|
||||||
self.dir = "{}/assets/".format(os.path.dirname(os.path.abspath(__file__))) if self.asset else self.bot_dir
|
|
||||||
|
|
||||||
def _start(self):
|
|
||||||
screen = 'screen -S {0}_{1} -d -m {2} {3}{1}.bot.py'
|
|
||||||
subprocess.run(screen.format(self.plugin_port, self.name, self.executable, self.dir),
|
|
||||||
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
||||||
|
|
||||||
def _stop(self):
|
|
||||||
subprocess.run('screen -X -S "{0}_{1}" quit'.format(self.plugin_port, self.name), shell=True)
|
|
||||||
|
|
||||||
def _restart(self):
|
|
||||||
self.stop()
|
|
||||||
self.start()
|
|
||||||
|
|
||||||
def _status(self):
|
|
||||||
screens = subprocess.getoutput("screen -ls")
|
|
||||||
return True if "{0}_{1}".format(self.plugin_port, self.name) in screens else False
|
|
||||||
|
|
||||||
def _display(self):
|
|
||||||
return "Started" if self.status() else "Stopped"
|
|
|
@ -2,11 +2,19 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import django
|
import django
|
||||||
|
|
||||||
|
# This block is assuming you will use this exact file
|
||||||
sep = os.sep
|
sep = os.sep
|
||||||
path = os.path.dirname(os.path.abspath(__file__))
|
path = os.path.dirname(os.path.abspath(__file__))
|
||||||
path = path.split(sep)[:-3]
|
path = path.split(sep)[:-3]
|
||||||
project = path[-1]
|
project = path[-1]
|
||||||
path = sep.join(path)
|
path = sep.join(path)
|
||||||
|
|
||||||
|
# What you need here is
|
||||||
|
# project = name of your main django project
|
||||||
|
# path = path to the root of your django project
|
||||||
|
# e.g. If your project is at /home/mcm/django1 and settings.py is at /home/mcm/django1/django2/settings.py
|
||||||
|
# project = django2
|
||||||
|
# path = /home/mcm/django1
|
||||||
sys.path.append(path)
|
sys.path.append(path)
|
||||||
print("Setting path for {0}: {1}".format(project, path))
|
print("Setting path for {0}: {1}".format(project, path))
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(project))
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(project))
|
|
@ -1,13 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import threading
|
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.shortcuts import reverse
|
|
||||||
from minecraft_manager.models import Application, Ticket
|
from minecraft_manager.models import Application, Ticket
|
||||||
from minecraft_manager.utils import url_path
|
from minecraft_manager.utils import url_path
|
||||||
|
|
||||||
|
|
|
@ -45,10 +45,10 @@ def config():
|
||||||
def rules():
|
def rules():
|
||||||
path = os.path.join(settings.MINECRAFT_BASE_DIR, "plugins/MinecraftManager/config.yml")
|
path = os.path.join(settings.MINECRAFT_BASE_DIR, "plugins/MinecraftManager/config.yml")
|
||||||
with open(path) as config_file:
|
with open(path) as config_file:
|
||||||
config = yaml.load(config_file)
|
cfg = yaml.safe_load(config_file)
|
||||||
data = config['rules']['rules']
|
data = cfg['rules']['rules']
|
||||||
if config['rules']['application']['validate']:
|
if cfg['rules']['application']['validate']:
|
||||||
data.append("The answer to the final question is \"{}\"".format(config['rules']['application']['answer']))
|
data.append("The answer to the final question is \"{}\"".format(cfg['rules']['application']['answer']))
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,5 @@ def get_sidebar(current_app, request):
|
||||||
show_chat = True if getattr(settings, 'GLOBAL_LOG', None) is not None else False
|
show_chat = True if getattr(settings, 'GLOBAL_LOG', None) is not None else False
|
||||||
if show_chat and request.user.has_perm('minecraft_manager.chat'):
|
if show_chat and request.user.has_perm('minecraft_manager.chat'):
|
||||||
ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-comment"></span> Chat</a></li>'.format('class="active"' if current_app == 'chat' else '', reverse('chat'))
|
ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-comment"></span> Chat</a></li>'.format('class="active"' if current_app == 'chat' else '', reverse('chat'))
|
||||||
if request.user.has_perm('minecraft_manager.bots'):
|
|
||||||
ret += '<li {}><a href="{}"><span class="glyphicon glyphicon-flash"></span> Bots</a></li>'.format('class="active"' if current_app == 'bots' else '', reverse('bots'))
|
|
||||||
return ret
|
return ret
|
||||||
|
|
64
urls.py
64
urls.py
|
@ -5,67 +5,39 @@ import minecraft_manager.views as mcm
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', RedirectView.as_view(pattern_name='overview')),
|
url(r'^$', RedirectView.as_view(pattern_name='overview')),
|
||||||
#Dashboard
|
|
||||||
|
# Dashboard
|
||||||
url(r'^dashboard/overview/$', login_required(mcm.Overview.as_view()), name="overview"),
|
url(r'^dashboard/overview/$', login_required(mcm.Overview.as_view()), name="overview"),
|
||||||
url(r'^dashboard/ban/$', login_required(mcm.Ban.as_view()), name="ban"),
|
url(r'^dashboard/ban/$', login_required(mcm.Ban.as_view()), name="ban"),
|
||||||
#Alerts
|
|
||||||
|
# Alerts
|
||||||
url(r'^dashboard/alert/$', login_required(mcm.Alert.as_view()), name="alert"),
|
url(r'^dashboard/alert/$', login_required(mcm.Alert.as_view()), name="alert"),
|
||||||
url(r'^dashboard/alert/(?P<alert_id>[0-9]{1,5})/$', login_required(mcm.AlertInfo.as_view())),
|
url(r'^dashboard/alert/(?P<alert_id>[0-9]{1,5})/$', login_required(mcm.AlertInfo.as_view())),
|
||||||
#Applications
|
|
||||||
|
# Applications
|
||||||
url(r'^dashboard/application/$', login_required(mcm.Application.as_view()), name="application"),
|
url(r'^dashboard/application/$', login_required(mcm.Application.as_view()), name="application"),
|
||||||
url(r'^dashboard/reference/$', login_required(mcm.Reference.as_view()), name="reference"),
|
url(r'^dashboard/reference/$', login_required(mcm.Reference.as_view()), name="reference"),
|
||||||
url(r'^dashboard/application/(?P<application_id>[0-9]{1,5})/$', login_required(mcm.ApplicationInfo.as_view())),
|
url(r'^dashboard/application/(?P<application_id>[0-9]{1,5})/$', login_required(mcm.ApplicationInfo.as_view())),
|
||||||
#Players
|
|
||||||
|
# Players
|
||||||
url(r'^dashboard/player/$', login_required(mcm.Player.as_view()), name="player"),
|
url(r'^dashboard/player/$', login_required(mcm.Player.as_view()), name="player"),
|
||||||
url(r'^dashboard/player/(?P<player_id>[0-9]{1,5})/$', login_required(mcm.PlayerInfo.as_view())),
|
url(r'^dashboard/player/(?P<player_id>[0-9]{1,5})/$', login_required(mcm.PlayerInfo.as_view())),
|
||||||
#Tickets
|
|
||||||
|
# Tickets
|
||||||
url(r'^dashboard/ticket/$', login_required(mcm.Ticket.as_view()), name="ticket"),
|
url(r'^dashboard/ticket/$', login_required(mcm.Ticket.as_view()), name="ticket"),
|
||||||
url(r'^dashboard/ticket/(?P<ticket_id>[0-9]{1,5})/$', login_required(mcm.TicketInfo.as_view())),
|
url(r'^dashboard/ticket/(?P<ticket_id>[0-9]{1,5})/$', login_required(mcm.TicketInfo.as_view())),
|
||||||
#Warnings
|
|
||||||
|
# Warnings
|
||||||
url(r'^dashboard/note/$', login_required(mcm.Note.as_view()), name="note"),
|
url(r'^dashboard/note/$', login_required(mcm.Note.as_view()), name="note"),
|
||||||
url(r'^dashboard/note/(?P<note_id>[0-9]{1,5})/$', login_required(mcm.NoteInfo.as_view())),
|
url(r'^dashboard/note/(?P<note_id>[0-9]{1,5})/$', login_required(mcm.NoteInfo.as_view())),
|
||||||
url(r'^dashboard/note/add$', login_required(mcm.NoteAdd.as_view()), name="note_add"),
|
url(r'^dashboard/note/add$', login_required(mcm.NoteAdd.as_view()), name="note_add"),
|
||||||
#IP
|
|
||||||
|
# IP
|
||||||
url(r'^dashboard/ip/(?P<ip_id>[0-9]{1,5})/$', login_required(mcm.IP.as_view()), name="ip"),
|
url(r'^dashboard/ip/(?P<ip_id>[0-9]{1,5})/$', login_required(mcm.IP.as_view()), name="ip"),
|
||||||
#Report
|
|
||||||
|
# Report
|
||||||
url(r'^report/$', login_required(mcm.Report.as_view()), name="report"),
|
url(r'^report/$', login_required(mcm.Report.as_view()), name="report"),
|
||||||
#Chat
|
|
||||||
|
# Chat
|
||||||
url(r'^dashboard/chat/$', permission_required('minecraft_manager.chat')(mcm.Chat.as_view()), name="chat"),
|
url(r'^dashboard/chat/$', permission_required('minecraft_manager.chat')(mcm.Chat.as_view()), name="chat"),
|
||||||
#Bots
|
|
||||||
url(r'^dashboard/bots/$', permission_required('minecraft_manager.bots')(mcm.Bots.as_view()), name="bots"),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Possible future feature
|
|
||||||
# from django.conf import settings
|
|
||||||
#
|
|
||||||
# LOGIN_REQUIRED = settings.LOGIN_REQUIRED if hasattr(settings, 'LOGIN_REQUIRED') else False
|
|
||||||
#
|
|
||||||
# urlpatterns = [
|
|
||||||
# #Dashboard
|
|
||||||
# url(r'^dashboard/overview/$', login_required(mcm.Overview.as_view()) if LOGIN_REQUIRED else mcm.Overview.as_view(), name="overview"),
|
|
||||||
# url(r'^dashboard/coreprotect/$', login_required(mcm.CoreProtect.as_view()) if LOGIN_REQUIRED else mcm.CoreProtect.as_view(), name="coreprotect"),
|
|
||||||
# url(r'^dashboard/activity/$', login_required(mcm.Activity.as_view()) if LOGIN_REQUIRED else mcm.Activity.as_view(), name="activity"),
|
|
||||||
# url(r'^dashboard/ban/$', login_required(mcm.Ban.as_view()) if LOGIN_REQUIRED else mcm.Ban.as_view(), name="ban"),
|
|
||||||
# #Alerts
|
|
||||||
# url(r'^dashboard/alert/$', login_required(mcm.Alert.as_view()) if LOGIN_REQUIRED else mcm.Alert.as_view(), name="alert"),
|
|
||||||
# url(r'^dashboard/alert/(?P<alert_id>[0-9]{1,5})/$', login_required(mcm.AlertInfo.as_view()) if LOGIN_REQUIRED else mcm.AlertInfo.as_view()),
|
|
||||||
# #Applications
|
|
||||||
# url(r'^dashboard/application/$', login_required(mcm.Application.as_view()) if LOGIN_REQUIRED else mcm.Application.as_view(), name="application"),
|
|
||||||
# url(r'^dashboard/application/(?P<application_id>[0-9]{1,5})/$', login_required(mcm.ApplicationInfo.as_view()) if LOGIN_REQUIRED else mcm.ApplicationInfo.as_view()),
|
|
||||||
# #Players
|
|
||||||
# url(r'^dashboard/player/$', login_required(mcm.Player.as_view()) if LOGIN_REQUIRED else mcm.Player.as_view(), name="player"),
|
|
||||||
# url(r'^dashboard/player/(?P<player_id>[0-9]{1,5})/$', login_required(mcm.PlayerInfo.as_view()) if LOGIN_REQUIRED else mcm.PlayerInfo.as_view()),
|
|
||||||
# #Tickets
|
|
||||||
# url(r'^dashboard/ticket/$', login_required(mcm.Ticket.as_view()) if LOGIN_REQUIRED else mcm.Ticket.as_view(), name="ticket"),
|
|
||||||
# url(r'^dashboard/ticket/(?P<ticket_id>[0-9]{1,5})/$', login_required(mcm.TicketInfo.as_view()) if LOGIN_REQUIRED else mcm.TicketInfo.as_view()),
|
|
||||||
# #Warnings
|
|
||||||
# url(r'^dashboard/warning/$', login_required(mcm.Warning.as_view()) if LOGIN_REQUIRED else mcm.Warning.as_view(), name="warning"),
|
|
||||||
# url(r'^dashboard/warning/(?P<warning_id>[0-9]{1,5})/$', login_required(mcm.WarningInfo.as_view()) if LOGIN_REQUIRED else mcm.WarningInfo.as_view()),
|
|
||||||
# url(r'^dashboard/warning/add$', login_required(mcm.WarningAdd.as_view()) if LOGIN_REQUIRED else mcm.WarningAdd.as_view(), name="warning_add"),
|
|
||||||
# #Chat
|
|
||||||
# url(r'^dashboard/chat/$', login_required(mcm.Chat.as_view()) if LOGIN_REQUIRED else mcm.Chat.as_view(), name="chat"),
|
|
||||||
# #Bots
|
|
||||||
# url(r'^dashboard/bots/$', login_required(mcm.Bots.as_view()) if LOGIN_REQUIRED else mcm.Bots.as_view(), name="bots"),
|
|
||||||
# ]
|
|
||||||
|
|
36
views.py
36
views.py
|
@ -17,7 +17,6 @@ from minecraft_manager.forms import TicketNoteForm, NoteForm
|
||||||
from minecraft_manager.overview import overview_data
|
from minecraft_manager.overview import overview_data
|
||||||
from minecraft_manager.utils import resolve_player
|
from minecraft_manager.utils import resolve_player
|
||||||
import minecraft_manager.api.api as API
|
import minecraft_manager.api.api as API
|
||||||
from minecraft_manager.bot import Bot
|
|
||||||
|
|
||||||
|
|
||||||
class Overview(View):
|
class Overview(View):
|
||||||
|
@ -447,38 +446,3 @@ class Chat(View):
|
||||||
else:
|
else:
|
||||||
data = {'success': False, 'message': 'No chat type or message set.'}
|
data = {'success': False, 'message': 'No chat type or message set.'}
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
|
||||||
class Bots(View):
|
|
||||||
|
|
||||||
def get_bots(self):
|
|
||||||
bot_dir = getattr(settings, 'BOT_DIR', None)
|
|
||||||
bots = []
|
|
||||||
if bot_dir:
|
|
||||||
for file in os.listdir(bot_dir):
|
|
||||||
if file.endswith('.bot.py'):
|
|
||||||
ve = file.replace('.bot.py', '')
|
|
||||||
py = os.path.join(bot_dir, ve, 'bin/python')
|
|
||||||
if os.path.isfile(py):
|
|
||||||
bots.append(Bot(file.replace('.bot.py', ''), False, py))
|
|
||||||
else:
|
|
||||||
bots.append(Bot(file.replace('.bot.py', ''), False, sys.executable))
|
|
||||||
# Also get packaged MCM bots
|
|
||||||
if getattr(settings, 'DISCORD_BOT_TOKEN', None):
|
|
||||||
bots.append(Bot("Discord-MCM", True, sys.executable))
|
|
||||||
return bots
|
|
||||||
|
|
||||||
def get(self, request):
|
|
||||||
return render(request, 'minecraft_manager/bots.html', {'current_app': 'bots', 'bots': self.get_bots()})
|
|
||||||
|
|
||||||
def post(self, request):
|
|
||||||
post = request.POST
|
|
||||||
for bot in self.get_bots():
|
|
||||||
if bot.name in post:
|
|
||||||
if post[bot.name] == "stop":
|
|
||||||
bot.stop()
|
|
||||||
elif post[bot.name] == "start":
|
|
||||||
bot.start()
|
|
||||||
elif post[bot.name] == "restart":
|
|
||||||
bot.restart()
|
|
||||||
return render(request, 'minecraft_manager/bots.html', {'current_app': 'bots', 'bots': self.get_bots()})
|
|
||||||
|
|
Loading…
Reference in New Issue