channel disabling prerun

This commit is contained in:
deathbybandaid 2019-05-23 15:38:43 -04:00
parent 7b6dc3c187
commit 1298c811fb
13 changed files with 139 additions and 20 deletions

View File

@ -27,6 +27,14 @@ class BotCommands():
}, },
} }
def find_command_type(self, command):
for commandstype in self.dict['commands'].keys():
if commandstype not in ['rule']:
for com in self.dict['commands'][commandstype].keys():
if com.lower() == command.lower():
return commandstype
return None
def register(self, bot, command_dict): def register(self, bot, command_dict):
self.lock.acquire() self.lock.acquire()

View File

@ -4,27 +4,47 @@ from __future__ import unicode_literals, absolute_import, division, print_functi
This is the SpiceBot Prerun system. This is the SpiceBot Prerun system.
""" """
from sopel.tools import Identifier
import functools import functools
import copy import copy
import datetime
import spicemanip import spicemanip
from .Tools import command_permissions_check
from .Commands import commands
from .Database import db as spicedb
class BotPrerun(): class BotPrerun():
def __init__(self): def __init__(self):
self.dict = {} self.dict = {}
def prerun(self, trigger_command_type='module_command'): def prerun(self, trigger_command_type='module'):
def actual_decorator(function): def actual_decorator(function):
@functools.wraps(function) @functools.wraps(function)
def internal_prerun(bot, trigger, *args, **kwargs): def internal_prerun(bot, trigger, *args, **kwargs):
argsdict_default = {} # Bots can't run commands
argsdict_default["type"] = trigger_command_type if Identifier(trigger.nick) == bot.nick:
return
# Primary command used for trigger, and a list of all words # Primary command used for trigger, and a list of all words
trigger_args, trigger_command = self.trigger_args(trigger.args[1], trigger_command_type) trigger_args, trigger_command = self.trigger_args(trigger.args[1], trigger_command_type)
command_lookup = commands.find_command_type(trigger_command)
# Argsdict Defaults
argsdict_default = {}
argsdict_default["type"] = trigger_command_type
argsdict_default["com"] = trigger_command argsdict_default["com"] = trigger_command
if command_lookup and "aliasfor" in commands.dict["commands"][command_lookup].keys():
realcom = commands.dict["commands"][command_lookup]["aliasfor"]
else:
realcom = trigger_command
argsdict_default["realcom"] = realcom
# split into && groupings # split into && groupings
and_split = self.and_split(trigger_args) and_split = self.and_split(trigger_args)
@ -34,15 +54,19 @@ class BotPrerun():
# Run the function for all splits # Run the function for all splits
for argsdict in argsdict_list: for argsdict in argsdict_list:
trigger.sb = argsdict trigger.sb = argsdict
function(bot, trigger, *args, **kwargs)
if len(argsdict["hyphen_args"]):
self.hyphen_arg_handler(bot, trigger, argsdict)
if self.runstatus(bot, trigger, argsdict):
function(bot, trigger, *args, **kwargs)
return return
return internal_prerun return internal_prerun
return actual_decorator return actual_decorator
def trigger_args(self, triggerargs_one, trigger_command_type='module_command'): def trigger_args(self, triggerargs_one, trigger_command_type='module'):
trigger_args = spicemanip.main(triggerargs_one, 'create') trigger_args = spicemanip.main(triggerargs_one, 'create')
if trigger_command_type in ['nickname', 'nickname_command', 'nickname_commands']: if trigger_command_type in ['nickname']:
trigger_command = spicemanip.main(trigger_args, 2).lower() trigger_command = spicemanip.main(trigger_args, 2).lower()
trigger_args = spicemanip.main(trigger_args, '3+', 'list') trigger_args = spicemanip.main(trigger_args, '3+', 'list')
else: else:
@ -61,9 +85,96 @@ class BotPrerun():
for trigger_args_list in prerun_and_split: for trigger_args_list in prerun_and_split:
argsdict = copy.deepcopy(argsdict_default) argsdict = copy.deepcopy(argsdict_default)
trigger_args_part = spicemanip.main(trigger_args_list, 'create') trigger_args_part = spicemanip.main(trigger_args_list, 'create')
trigger_args_part, hyphen_args = self.hyphen_args(trigger_args_part)
argsdict["hyphen_args"] = hyphen_args
argsdict["args"] = trigger_args_part argsdict["args"] = trigger_args_part
prerun_split.append(argsdict) prerun_split.append(argsdict)
return prerun_split return prerun_split
def hyphen_args(self, trigger_args_part):
valid_hyphen_args = [
'enable', 'disable'
]
hyphen_args = []
trigger_args_unhyphend = []
for worditem in trigger_args_part:
if str(worditem).startswith("--") and worditem[2:] in valid_hyphen_args:
hyphen_args.append(worditem[2:])
else:
trigger_args_unhyphend.append(worditem)
return trigger_args_unhyphend, hyphen_args
def hyphen_arg_handler(self, bot, trigger, argsdict):
hyphenarg = argsdict["hyphen_args"][0]
# Commands that cannot run via privmsg
if hyphenarg in ['enable']:
if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']):
bot.say("I was unable to disable this command due to privilege issues.")
return
if trigger.is_privmsg:
bot.notice("This command must be run in a channel you which to enable it in.", trigger.nick)
return
disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {}
if argsdict["realcom"] not in disabled_list.keys():
bot.notice(argsdict["com"] + " is already enabled in " + str(trigger.sender), trigger.nick)
return
del disabled_list[argsdict["realcom"]]
spicedb.set_channel_value(bot, trigger.sender, 'disabled_commands', disabled_list, 'commands')
bot.say(argsdict["com"] + " is now enabled in " + str(trigger.sender))
return
elif hyphenarg in ['disable']:
if not command_permissions_check(bot, trigger, ['admins', 'owner', 'OP', 'ADMIN', 'OWNER']):
bot.say("I was unable to disable this command due to privilege issues.")
return
if trigger.is_privmsg:
bot.notice("This command must be run in a channel you which to disable it in.", trigger.nick)
return
disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {}
if argsdict["realcom"] in disabled_list.keys():
bot.notice(argsdict["com"] + " is already disabled in " + str(trigger.sender), trigger.nick)
return
trailingmessage = spicemanip.main(argsdict["args"], 0) or "No reason given."
timestamp = str(datetime.datetime.utcnow())
disabled_list[argsdict["realcom"]] = {"reason": trailingmessage, "timestamp": timestamp, "disabledby": trigger.nick}
spicedb.set_channel_value(bot, trigger.sender, 'disabled_commands', disabled_list, 'commands')
spicedb.adjust_channel_list(bot, trigger.sender, 'disabled_commands', argsdict["realcom"], 'add', 'commands')
bot.say(argsdict["com"] + " is now disabled in " + str(trigger.sender))
return
return
def runstatus(self, bot, trigger, argsdict):
# Hyphen args can only be used one per && split
if len(argsdict["hyphen_args"]):
return False
# don't run commands that are disabled in channels
if not trigger.is_privmsg:
disabled_list = spicedb.get_channel_value(bot, trigger.sender, 'disabled_commands', 'commands') or {}
if argsdict["realcom"] in disabled_list.keys():
reason = disabled_list["disabled_commands"][argsdict["realcom"]]["reason"]
timestamp = disabled_list["disabled_commands"][argsdict["realcom"]]["timestamp"]
bywhom = disabled_list["disabled_commands"][argsdict["realcom"]]["disabledby"]
bot.notice("The " + str(argsdict["com"]) + " command was disabled by " + bywhom + " in " + str(trigger.sender) + " at " + str(timestamp) + " for the following reason: " + str(reason), trigger.nick)
return False
return True
prerun = BotPrerun() prerun = BotPrerun()

View File

@ -10,7 +10,7 @@ import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED, SpiceBot.events.BOT_COMMANDSQUERY]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED, SpiceBot.events.BOT_COMMANDSQUERY])
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('(.*)') @sopel.module.nickname_commands('(.*)')
def bot_command_nick(bot, trigger): def bot_command_nick(bot, trigger):

View File

@ -9,13 +9,13 @@ import spicemanip
import sopel_modules.SpiceBot as SpiceBot import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('owners', 'owner') @sopel.module.nickname_commands('owners', 'owner')
def bot_command_owners(bot, trigger): def bot_command_owners(bot, trigger):
bot_command_process(bot, trigger) bot_command_process(bot, trigger)
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('admins') @sopel.module.nickname_commands('admins')
def bot_command_admins(bot, trigger): def bot_command_admins(bot, trigger):
bot_command_process(bot, trigger) bot_command_process(bot, trigger)

View File

@ -10,7 +10,7 @@ import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOADED])
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('channels', 'channel') @sopel.module.nickname_commands('channels', 'channel')
def nickname_comand_channels(bot, trigger): def nickname_comand_channels(bot, trigger):

View File

@ -11,7 +11,7 @@ import spicemanip
@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY])
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('(.*)') @sopel.module.nickname_commands('(.*)')
def query_detection_nick(bot, trigger): def query_detection_nick(bot, trigger):

View File

@ -11,7 +11,7 @@ import spicemanip
@SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_COMMANDSQUERY])
@SpiceBot.prerun.prerun('query_command') @SpiceBot.prerun.prerun('query')
@sopel.module.rule('^\?(.*)') @sopel.module.rule('^\?(.*)')
def query_detection(bot, trigger): def query_detection(bot, trigger):

View File

@ -12,7 +12,7 @@ import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
@SpiceBot.prerun.prerun('prefix_command') @SpiceBot.prerun.prerun('prefix')
@sopel.module.commands('(.*)') @sopel.module.commands('(.*)')
def gifapi_triggers(bot, trigger): def gifapi_triggers(bot, trigger):

View File

@ -12,7 +12,7 @@ import spicemanip
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
@SpiceBot.prerun.prerun('prefix_command') @SpiceBot.prerun.prerun('prefix')
@module.commands('gif') @module.commands('gif')
def gif_trigger(bot, trigger): def gif_trigger(bot, trigger):

View File

@ -12,7 +12,7 @@ import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_GIFSEARCH])
@SpiceBot.prerun.prerun('prefix_command') @SpiceBot.prerun.prerun('prefix')
@sopel.module.commands('(.*)') @sopel.module.commands('(.*)')
def gifapi_triggers(bot, trigger): def gifapi_triggers(bot, trigger):

View File

@ -9,7 +9,7 @@ import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.events.check_ready([SpiceBot.events.BOT_LOGS]) @SpiceBot.events.check_ready([SpiceBot.events.BOT_LOGS])
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('logs', 'debug') @sopel.module.nickname_commands('logs', 'debug')
def bot_command_logs(bot, trigger): def bot_command_logs(bot, trigger):

View File

@ -9,19 +9,19 @@ import spicemanip
import sopel_modules.SpiceBot as SpiceBot import sopel_modules.SpiceBot as SpiceBot
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('action') @sopel.module.nickname_commands('action')
def bot_command_action(bot, trigger): def bot_command_action(bot, trigger):
bot_command_process(bot, trigger) bot_command_process(bot, trigger)
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('notice') @sopel.module.nickname_commands('notice')
def bot_command_notice(bot, trigger): def bot_command_notice(bot, trigger):
bot_command_process(bot, trigger) bot_command_process(bot, trigger)
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('privmsg', 'say', 'msg') @sopel.module.nickname_commands('privmsg', 'say', 'msg')
def bot_command_privmsg(bot, trigger): def bot_command_privmsg(bot, trigger):
bot_command_process(bot, trigger) bot_command_process(bot, trigger)

View File

@ -27,7 +27,7 @@ def setup(bot):
bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False)
@SpiceBot.prerun.prerun('nickname_command') @SpiceBot.prerun.prerun('nickname')
@sopel.module.nickname_commands('update') @sopel.module.nickname_commands('update')
def nickname_comand_update(bot, trigger): def nickname_comand_update(bot, trigger):