From faa342933ea0b0f14b18f5a57182685624de5261 Mon Sep 17 00:00:00 2001 From: Etz Elia Date: Sun, 19 Apr 2020 22:26:51 +0200 Subject: [PATCH] Various fixes and some improvements (#49) Rip out bot refs Various fixes and some improvements Signed-off-by: Etzelia Co-authored-by: Etzelia Reviewed-by: ZeroHD --- api/api.py | 37 +++++------ bot/__init__.py | 39 ----------- bot/assets/{Discord-MCM.bot.py => example.py} | 8 +++ bot/discord.py | 4 +- external/views.py | 8 +-- templatetags/sidebar.py | 3 +- urls.py | 64 ++++++------------- views.py | 36 ----------- 8 files changed, 48 insertions(+), 151 deletions(-) rename bot/assets/{Discord-MCM.bot.py => example.py} (61%) diff --git a/api/api.py b/api/api.py index c194ca5..fc882af 100644 --- a/api/api.py +++ b/api/api.py @@ -40,35 +40,30 @@ def plugin(key, command): def discord_mcm(message='', embed=None, ping=False): discord_mcm_webhook = getattr(settings, 'DISCORD_MCM_WEBHOOK', None) if discord_mcm_webhook: - 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 - return requests.post(discord_mcm_webhook, json=data) - return None + return post_webhook(discord_mcm_webhook, message, embed, ping) + return None def discord_notification(message='', embed=None, ping=False): discord_notification_webhook = getattr(settings, 'DISCORD_NOTIFICATION_WEBHOOK', None) if discord_notification_webhook: - 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 - return requests.post(discord_notification_webhook, json=data) + return post_webhook(discord_notification_webhook, message, embed, ping) 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): return message.replace("§0", "").replace("§1", "").replace("§2", "").replace("§3", "").replace("§4", "") \ .replace("§5", "").replace("§6", "").replace("§7", "").replace("§8", "").replace("§9", "").replace("§a", "") \ diff --git a/bot/__init__.py b/bot/__init__.py index 0d0d377..e69de29 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -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" diff --git a/bot/assets/Discord-MCM.bot.py b/bot/assets/example.py similarity index 61% rename from bot/assets/Discord-MCM.bot.py rename to bot/assets/example.py index 092a99e..0b660fd 100644 --- a/bot/assets/Discord-MCM.bot.py +++ b/bot/assets/example.py @@ -2,11 +2,19 @@ import os import sys import django +# This block is assuming you will use this exact file sep = os.sep path = os.path.dirname(os.path.abspath(__file__)) path = path.split(sep)[:-3] project = path[-1] 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) print("Setting path for {0}: {1}".format(project, path)) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(project)) diff --git a/bot/discord.py b/bot/discord.py index 54a8087..2b57345 100644 --- a/bot/discord.py +++ b/bot/discord.py @@ -1,13 +1,11 @@ import asyncio import logging import traceback -import threading -from enum import Enum import discord from discord.ext import commands from django.conf import settings -from django.shortcuts import reverse + from minecraft_manager.models import Application, Ticket from minecraft_manager.utils import url_path diff --git a/external/views.py b/external/views.py index d6d41e1..ce3f693 100644 --- a/external/views.py +++ b/external/views.py @@ -45,10 +45,10 @@ def config(): def rules(): path = os.path.join(settings.MINECRAFT_BASE_DIR, "plugins/MinecraftManager/config.yml") with open(path) as config_file: - config = yaml.load(config_file) - data = config['rules']['rules'] - if config['rules']['application']['validate']: - data.append("The answer to the final question is \"{}\"".format(config['rules']['application']['answer'])) + cfg = yaml.safe_load(config_file) + data = cfg['rules']['rules'] + if cfg['rules']['application']['validate']: + data.append("The answer to the final question is \"{}\"".format(cfg['rules']['application']['answer'])) return data diff --git a/templatetags/sidebar.py b/templatetags/sidebar.py index 77945c0..b89939a 100644 --- a/templatetags/sidebar.py +++ b/templatetags/sidebar.py @@ -33,6 +33,5 @@ def get_sidebar(current_app, request): 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'): ret += '
  •   Chat
  • '.format('class="active"' if current_app == 'chat' else '', reverse('chat')) - if request.user.has_perm('minecraft_manager.bots'): - ret += '
  •   Bots
  • '.format('class="active"' if current_app == 'bots' else '', reverse('bots')) + return ret diff --git a/urls.py b/urls.py index d88ab71..7c4a359 100644 --- a/urls.py +++ b/urls.py @@ -5,67 +5,39 @@ import minecraft_manager.views as mcm urlpatterns = [ 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/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/(?P[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/reference/$', login_required(mcm.Reference.as_view()), name="reference"), url(r'^dashboard/application/(?P[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/(?P[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/(?P[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/(?P[0-9]{1,5})/$', login_required(mcm.NoteInfo.as_view())), url(r'^dashboard/note/add$', login_required(mcm.NoteAdd.as_view()), name="note_add"), - #IP + + # IP url(r'^dashboard/ip/(?P[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"), - #Chat + + # 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[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[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[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[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[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"), -# ] diff --git a/views.py b/views.py index 5dc5c61..68e9ea6 100644 --- a/views.py +++ b/views.py @@ -17,7 +17,6 @@ from minecraft_manager.forms import TicketNoteForm, NoteForm from minecraft_manager.overview import overview_data from minecraft_manager.utils import resolve_player import minecraft_manager.api.api as API -from minecraft_manager.bot import Bot class Overview(View): @@ -447,38 +446,3 @@ class Chat(View): else: data = {'success': False, 'message': 'No chat type or message set.'} 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()})