From f493a5ae2bd03634a24d866f1520b90faca39487 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Sun, 5 May 2019 20:42:56 -0400 Subject: [PATCH] Add Channels to new event system --- sopel_modules/SpiceBot_Channels/Channels.py | 168 +++--------------- .../SpiceBot_Channels/Channels_Command.py | 120 +++++++++++++ .../SpiceBot_Channels/Channels_Parsing.py | 32 ++++ sopel_modules/SpiceBot_Channels/__init__.py | 2 + sopel_modules/SpiceBot_Events/Events.py | 1 - sopel_modules/SpiceBot_Events/System.py | 2 +- 6 files changed, 177 insertions(+), 148 deletions(-) create mode 100644 sopel_modules/SpiceBot_Channels/Channels_Command.py create mode 100644 sopel_modules/SpiceBot_Channels/Channels_Parsing.py diff --git a/sopel_modules/SpiceBot_Channels/Channels.py b/sopel_modules/SpiceBot_Channels/Channels.py index 42d94e9..103df07 100644 --- a/sopel_modules/SpiceBot_Channels/Channels.py +++ b/sopel_modules/SpiceBot_Channels/Channels.py @@ -6,18 +6,18 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi import sopel.module from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute -from sopel_modules.SpiceBot_LoadOrder.LoadOrder import set_bot_event, startup_bot_event - -import time - -import spicemanip +from sopel_modules.SpiceBot_LoadOrder.LoadOrder import startup_bot_event, set_bot_event +from sopel_modules.SpiceBot_Events.System import bot_events_startup_register, bot_events_recieved, bot_events_trigger from sopel_modules.SpiceBot_SBTools import ( - sopel_triggerargs, inlist, topic_compile, channel_privs, join_all_channels, chanadmin_all_channels, channel_list_current, bot_logging ) +import spicemanip + +import time + class SpiceBot_Channels_MainSection(StaticSection): announcenew = ValidatedAttribute('announcenew', default=False) @@ -35,20 +35,14 @@ def configure(config): def setup(bot): - bot_logging(bot, 'SpiceBot_Channels', "Initial Setup processing") + bot_logging(bot, 'SpiceBot_Channels', "Starting setup procedure") + bot_events_startup_register(bot, ['2001']) startup_bot_event(bot, "SpiceBot_Channels") bot.config.define_section("SpiceBot_Channels", SpiceBot_Channels_MainSection, validate=False) - if 'SpiceBot_Channels' not in bot.memory: - bot.memory['SpiceBot_Channels'] = dict() - if 'channels' not in bot.memory['SpiceBot_Channels'].keys(): - bot.memory['SpiceBot_Channels']['channels'] = dict() - if 'InitialProcess' not in bot.memory['SpiceBot_Channels'].keys(): - bot.memory['SpiceBot_Channels']['InitialProcess'] = False - if 'ProcessLock' not in bot.memory['SpiceBot_Channels'].keys(): - bot.memory['SpiceBot_Channels']['ProcessLock'] = False + bot_channels_setup_check(bot) def shutdown(bot): @@ -56,9 +50,11 @@ def shutdown(bot): del bot.memory["SpiceBot_Channels"] -@sopel.module.event('001') +@sopel.module.event('1003') @sopel.module.rule('.*') def trigger_channel_list_initial(bot, trigger): + bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) + bot_events_recieved(bot, trigger.event) # Unkickable bot.write(('SAMODE', bot.nick, '+q')) @@ -81,6 +77,7 @@ def trigger_channel_list_initial(bot, trigger): foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) bot_logging(bot, 'SpiceBot_Channels', "Channel listing finished! " + str(foundchannelcount) + " channel(s) found.") set_bot_event(bot, "SpiceBot_Channels") + bot_events_trigger(bot, 2001, "SpiceBot_Channels") join_all_channels(bot) chanadmin_all_channels(bot) @@ -111,134 +108,13 @@ def trigger_channel_list_initial(bot, trigger): del bot.memory['SpiceBot_Channels']['channels']["*"] -@sopel.module.event('321') +def bot_channels_setup_check(bot): + if "SpiceBot_Channels" not in bot.memory: + bot.memory["SpiceBot_Channels"] = {"channels": {}, "InitialProcess": False, "ProcessLock": False} + + +@sopel.module.event('2001') @sopel.module.rule('.*') -def watch_chanlist_start(bot, trigger): - bot.memory['SpiceBot_Channels']['ProcessLock'] = True - - -@sopel.module.event('322') -@sopel.module.rule('.*') -def watch_chanlist_populate(bot, trigger): - channel, _, topic = trigger.args[1:] - if channel.lower() not in bot.memory['SpiceBot_Channels']['channels']: - bot.memory['SpiceBot_Channels']['channels'][channel.lower()] = dict() - bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['name'] = channel - bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['topic'] = topic_compile(topic) - bot.memory['SpiceBot_Channels']['ProcessLock'] = True - - -@sopel.module.event('323') -@sopel.module.rule('.*') -def watch_chanlist_complete(bot, trigger): - bot.memory['SpiceBot_Channels']['ProcessLock'] = False - if not bot.memory['SpiceBot_Channels']['InitialProcess']: - bot.memory['SpiceBot_Channels']['InitialProcess'] = True - - -@sopel.module.nickname_commands('channels', 'channel') -def nickname_comand_channels(bot, trigger): - - triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') - - if not len(triggerargs): - commandused = 'list' - else: - commandused = spicemanip.main(triggerargs, 1).lower() - - triggerargs = spicemanip.main(triggerargs, '2+', 'list') - - channel_list_current(bot) - - if commandused == 'list': - chanlist = spicemanip.main(bot.channels.keys(), 'andlist') - bot.osd("You can find me in " + chanlist) - return - - elif commandused == 'total': - botcount = len(bot.channels.keys()) - servercount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) - bot.osd("I am in " + str(botcount) + " of " + str(servercount) + " channel(s) available on this server.") - return - - elif commandused == 'random': - channel = spicemanip.main(bot.memory['SpiceBot_Channels']['channels'], 'random') - topic = bot.memory['SpiceBot_Channels']['channels'][channel]['topic'] - msg = ["Random channel for you: {}.".format(bot.memory['SpiceBot_Channels']['channels'][channel]['name'])] - if topic and not topic.isspace(): - msg.append("The topic is: {}".format(topic)) - else: - msg.append("Its topic is empty.") - bot.osd(msg) - return - - elif commandused == 'update': - if not trigger.admin: - bot.osd("You do not have permission to update the channel listing.") - return - bot.write(['LIST']) - bot.osd(["[SpiceBot_Channels]", "I am now updating the channel listing for this server."]) - bot.memory['SpiceBot_Channels']['ProcessLock'] = True - while bot.memory['SpiceBot_Channels']['ProcessLock']: - pass - join_all_channels(bot) - foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) - bot.osd("[SpiceBot_Channels]", "Channel listing finished!", str(foundchannelcount) + " channel(s) found.") - return - - elif commandused == 'topic': - if not len(triggerargs): - bot.osd("Channel name input missing.") - return - channel = spicemanip.main(triggerargs, 1) - if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): - bot.osd("Channel name {} not valid.".format(channel)) - return - topic = bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['topic'] - channel = bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['name'] - bot.osd("Topic for {}: {}".format(channel, topic)) - return - - if commandused.upper() in ['OP', 'HOP', 'VOICE', 'OWNER', 'ADMIN']: - if not len(triggerargs): - if trigger.is_privmsg: - bot.osd("Channel name required.") - return - else: - channel = trigger.sender - else: - channel = spicemanip.main(triggerargs, 1) - if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): - bot.osd("Channel name {} not valid.".format(channel)) - return - if not inlist(bot, channel.lower(), bot.channels.keys()): - bot.osd("I need to be in {} to see nick privileges.".format(channel)) - return - - privlist = channel_privs(bot, channel, commandused.upper()) - dispmsg = [] - if not len(privlist): - dispmsg.append("There are no Channel " + commandused.upper() + "s for " + str(channel)) - else: - oplist = spicemanip.main(privlist, 'andlist') - dispmsg.append("Channel " + commandused.upper() + "s for " + str(channel) + " are: " + oplist) - bot.osd(dispmsg, trigger.nick, 'notice') - return - - # Users List - if commandused == 'users': - channel = spicemanip.main(triggerargs, 1) - if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): - bot.osd("Channel name {} not valid.".format(channel)) - return - if not inlist(bot, channel.lower(), bot.channels.keys()): - bot.osd("I need to be in {} to see user list.".format(channel)) - return - dispmsg = [] - if not len(bot.channels[channel].privileges.keys()): - dispmsg.append("There are no Channel users for " + str(channel)) - else: - userslist = spicemanip.main(bot.channels[channel].privileges.keys(), 'andlist') - dispmsg.append("Channel users for " + str(channel) + " are: " + userslist) - bot.osd(dispmsg, trigger.nick, 'notice') - return +def bot_events_setup(bot, trigger): + bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) + bot_events_recieved(bot, trigger.event) diff --git a/sopel_modules/SpiceBot_Channels/Channels_Command.py b/sopel_modules/SpiceBot_Channels/Channels_Command.py new file mode 100644 index 0000000..f678866 --- /dev/null +++ b/sopel_modules/SpiceBot_Channels/Channels_Command.py @@ -0,0 +1,120 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel.module + +import spicemanip + +from sopel_modules.SpiceBot_SBTools import ( + sopel_triggerargs, inlist, channel_privs, + join_all_channels, channel_list_current, + ) + + +@sopel.module.nickname_commands('channels', 'channel') +def nickname_comand_channels(bot, trigger): + + triggerargs, triggercommand = sopel_triggerargs(bot, trigger, 'nickname_command') + + if not len(triggerargs): + commandused = 'list' + else: + commandused = spicemanip.main(triggerargs, 1).lower() + + triggerargs = spicemanip.main(triggerargs, '2+', 'list') + + channel_list_current(bot) + + if commandused == 'list': + chanlist = spicemanip.main(bot.channels.keys(), 'andlist') + bot.osd("You can find me in " + chanlist) + return + + elif commandused == 'total': + botcount = len(bot.channels.keys()) + servercount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) + bot.osd("I am in " + str(botcount) + " of " + str(servercount) + " channel(s) available on this server.") + return + + elif commandused == 'random': + channel = spicemanip.main(bot.memory['SpiceBot_Channels']['channels'], 'random') + topic = bot.memory['SpiceBot_Channels']['channels'][channel]['topic'] + msg = ["Random channel for you: {}.".format(bot.memory['SpiceBot_Channels']['channels'][channel]['name'])] + if topic and not topic.isspace(): + msg.append("The topic is: {}".format(topic)) + else: + msg.append("Its topic is empty.") + bot.osd(msg) + return + + elif commandused == 'update': + if not trigger.admin: + bot.osd("You do not have permission to update the channel listing.") + return + bot.write(['LIST']) + bot.osd(["[SpiceBot_Channels]", "I am now updating the channel listing for this server."]) + bot.memory['SpiceBot_Channels']['ProcessLock'] = True + while bot.memory['SpiceBot_Channels']['ProcessLock']: + pass + join_all_channels(bot) + foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) + bot.osd("[SpiceBot_Channels]", "Channel listing finished!", str(foundchannelcount) + " channel(s) found.") + return + + elif commandused == 'topic': + if not len(triggerargs): + bot.osd("Channel name input missing.") + return + channel = spicemanip.main(triggerargs, 1) + if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): + bot.osd("Channel name {} not valid.".format(channel)) + return + topic = bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['topic'] + channel = bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['name'] + bot.osd("Topic for {}: {}".format(channel, topic)) + return + + if commandused.upper() in ['OP', 'HOP', 'VOICE', 'OWNER', 'ADMIN']: + if not len(triggerargs): + if trigger.is_privmsg: + bot.osd("Channel name required.") + return + else: + channel = trigger.sender + else: + channel = spicemanip.main(triggerargs, 1) + if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): + bot.osd("Channel name {} not valid.".format(channel)) + return + if not inlist(bot, channel.lower(), bot.channels.keys()): + bot.osd("I need to be in {} to see nick privileges.".format(channel)) + return + + privlist = channel_privs(bot, channel, commandused.upper()) + dispmsg = [] + if not len(privlist): + dispmsg.append("There are no Channel " + commandused.upper() + "s for " + str(channel)) + else: + oplist = spicemanip.main(privlist, 'andlist') + dispmsg.append("Channel " + commandused.upper() + "s for " + str(channel) + " are: " + oplist) + bot.osd(dispmsg, trigger.nick, 'notice') + return + + # Users List + if commandused == 'users': + channel = spicemanip.main(triggerargs, 1) + if not inlist(bot, channel.lower(), bot.memory['SpiceBot_Channels']['channels'].keys()): + bot.osd("Channel name {} not valid.".format(channel)) + return + if not inlist(bot, channel.lower(), bot.channels.keys()): + bot.osd("I need to be in {} to see user list.".format(channel)) + return + dispmsg = [] + if not len(bot.channels[channel].privileges.keys()): + dispmsg.append("There are no Channel users for " + str(channel)) + else: + userslist = spicemanip.main(bot.channels[channel].privileges.keys(), 'andlist') + dispmsg.append("Channel users for " + str(channel) + " are: " + userslist) + bot.osd(dispmsg, trigger.nick, 'notice') + return diff --git a/sopel_modules/SpiceBot_Channels/Channels_Parsing.py b/sopel_modules/SpiceBot_Channels/Channels_Parsing.py new file mode 100644 index 0000000..6998524 --- /dev/null +++ b/sopel_modules/SpiceBot_Channels/Channels_Parsing.py @@ -0,0 +1,32 @@ +# coding=utf-8 + +from __future__ import unicode_literals, absolute_import, division, print_function + +import sopel.module + +from sopel_modules.SpiceBot_SBTools import topic_compile + + +@sopel.module.event('321') +@sopel.module.rule('.*') +def watch_chanlist_start(bot, trigger): + bot.memory['SpiceBot_Channels']['ProcessLock'] = True + + +@sopel.module.event('322') +@sopel.module.rule('.*') +def watch_chanlist_populate(bot, trigger): + channel, _, topic = trigger.args[1:] + if channel.lower() not in bot.memory['SpiceBot_Channels']['channels']: + bot.memory['SpiceBot_Channels']['channels'][channel.lower()] = dict() + bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['name'] = channel + bot.memory['SpiceBot_Channels']['channels'][channel.lower()]['topic'] = topic_compile(topic) + bot.memory['SpiceBot_Channels']['ProcessLock'] = True + + +@sopel.module.event('323') +@sopel.module.rule('.*') +def watch_chanlist_complete(bot, trigger): + bot.memory['SpiceBot_Channels']['ProcessLock'] = False + if not bot.memory['SpiceBot_Channels']['InitialProcess']: + bot.memory['SpiceBot_Channels']['InitialProcess'] = True diff --git a/sopel_modules/SpiceBot_Channels/__init__.py b/sopel_modules/SpiceBot_Channels/__init__.py index fe6cf28..6d0a4e4 100644 --- a/sopel_modules/SpiceBot_Channels/__init__.py +++ b/sopel_modules/SpiceBot_Channels/__init__.py @@ -4,3 +4,5 @@ from __future__ import unicode_literals, absolute_import, print_function, division from .Channels import * +from .Channels_Command import * +from .Channels_Parsing import * diff --git a/sopel_modules/SpiceBot_Events/Events.py b/sopel_modules/SpiceBot_Events/Events.py index a01caf9..6316bd1 100644 --- a/sopel_modules/SpiceBot_Events/Events.py +++ b/sopel_modules/SpiceBot_Events/Events.py @@ -56,4 +56,3 @@ def bot_events_connected(bot, trigger): def bot_events_monologue(bot, trigger): bot_logging(bot, 'SpiceBot_Events', trigger.args[1]) bot_events_recieved(bot, trigger.event) - bot.osd(trigger.args[1], bot.channels.keys()) diff --git a/sopel_modules/SpiceBot_Events/System.py b/sopel_modules/SpiceBot_Events/System.py index da5723d..d96005c 100644 --- a/sopel_modules/SpiceBot_Events/System.py +++ b/sopel_modules/SpiceBot_Events/System.py @@ -13,7 +13,7 @@ def configure(config): def setup(bot): - bot_logging(bot, 'SpiceBot_Events', "Starting setup procedure.") + bot_logging(bot, 'SpiceBot_Events', "Starting setup procedure") bot_events_setup_check(bot) bot_events_startup_register(bot, ['1001', '1002', '1003'])