clean if statements, ban users, closes #51

pull/74/head
Mallory Hancock 2019-10-02 09:25:38 -07:00
parent 9af3abd4a5
commit 23355bb699
1 changed files with 25 additions and 10 deletions

View File

@ -27,6 +27,7 @@ class Bot(irc.bot.SingleServerIRCBot):
self.cmd_prefix = kwargs.get('cmd_prefix', '!') self.cmd_prefix = kwargs.get('cmd_prefix', '!')
self.use_prefix_for_plugins = kwargs.get('use_prefix_for_plugins', False) self.use_prefix_for_plugins = kwargs.get('use_prefix_for_plugins', False)
self.disable_help = kwargs.get('disable_help', False) self.disable_help = kwargs.get('disable_help', False)
self.banned_users = kwargs.get('banned_users', [])
if self.ssl_required: if self.ssl_required:
factory = irc.connection.Factory(wrapper=ssl.wrap_socket) factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port, self.server_pass)], nickname, nickname, connect_factory=factory) irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port, self.server_pass)], nickname, nickname, connect_factory=factory)
@ -45,7 +46,10 @@ class Bot(irc.bot.SingleServerIRCBot):
'disable': 'disable a plugin', 'disable': 'disable a plugin',
'op': 'add a user as bot operator', 'op': 'add a user as bot operator',
'deop': 'remove a user as bot operator', 'deop': 'remove a user as bot operator',
'ops': 'list all ops' 'ops': 'list all ops',
'ban': 'ban a user from using the bot',
'unban': 'remove bot ban for user',
'banlist': 'currently banned nicks'
} }
self.internal_commands = {self.cmd_prefix + k: v for k,v in self.internal_commands.items()} self.internal_commands = {self.cmd_prefix + k: v for k,v in self.internal_commands.items()}
plugin.load_plugins(self.plugin_dir, use_prefix=self.use_prefix_for_plugins, cmd_prefix=self.cmd_prefix) plugin.load_plugins(self.plugin_dir, use_prefix=self.use_prefix_for_plugins, cmd_prefix=self.cmd_prefix)
@ -128,26 +132,27 @@ class Bot(irc.bot.SingleServerIRCBot):
op = True op = True
else: else:
op = False op = False
if cmd == self.cmd_prefix + 'join' and op: cmd = cmd.lstrip(self.cmd_prefix)
if cmd == 'join' and op:
try: try:
c.join(*arg.split()) c.join(*arg.split())
self.logger.info('joining {} per request of {}'.format(arg, nick)) self.logger.info('joining {} per request of {}'.format(arg, nick))
output = self.output_message('{}: joined {}'.format(nick, arg.split()[0])) output = self.output_message('{}: joined {}'.format(nick, arg.split()[0]))
except: except:
self.logger.exception('issue with join command: {}join #channel <channel key>'.format(self.cmd_prefix)) self.logger.exception('issue with join command: {}join #channel <channel key>'.format(self.cmd_prefix))
elif cmd == self.cmd_prefix + 'quit' and op: elif cmd == 'quit' and op:
self.logger.info('quitting per request of {}'.format(nick)) self.logger.info('quitting per request of {}'.format(nick))
if not arg: if not arg:
arg = "See y'all later!" arg = "See y'all later!"
c.quit(arg) c.quit(arg)
quit() quit()
elif cmd == self.cmd_prefix + 'help' and not self.disable_help: elif cmd == 'help' and not self.disable_help:
self.call_help(nick, op) self.call_help(nick, op)
elif cmd == self.cmd_prefix + 'reload' and op: elif cmd == 'reload' and op:
self.logger.info('reloading plugins per request of {}'.format(nick)) self.logger.info('reloading plugins per request of {}'.format(nick))
plugin.load_plugins(self.plugin_dir, use_prefix=self.use_prefix_for_plugins, cmd_prefix=self.cmd_prefix) plugin.load_plugins(self.plugin_dir, use_prefix=self.use_prefix_for_plugins, cmd_prefix=self.cmd_prefix)
output = self.output_message('Plugins reloaded') output = self.output_message('Plugins reloaded')
elif cmd == self.cmd_prefix + 'enable' and op: elif cmd == 'enable' and op:
if arg in plugin.plugins: if arg in plugin.plugins:
if plugin.plugins[arg].enabled: if plugin.plugins[arg].enabled:
output = self.output_message("{}: '{}' already enabled".format(nick, arg)) output = self.output_message("{}: '{}' already enabled".format(nick, arg))
@ -156,23 +161,33 @@ class Bot(irc.bot.SingleServerIRCBot):
output = self.output_message("{}: '{}' enabled!".format(nick, arg)) output = self.output_message("{}: '{}' enabled!".format(nick, arg))
else: else:
output = self.output_message("{}: '{}' not found".format(nick, arg)) output = self.output_message("{}: '{}' not found".format(nick, arg))
elif cmd == self.cmd_prefix + 'disable' and op: elif cmd == 'disable' and op:
if arg in plugin.plugins: if arg in plugin.plugins:
if not plugin.plugins[arg].enabled: if not plugin.plugins[arg].enabled:
output = self.output_message("{}: '{}' already disabled".format(nick, arg)) output = self.output_message("{}: '{}' already disabled".format(nick, arg))
else: else:
plugin.plugins[arg].disable() plugin.plugins[arg].disable()
output = self.output_message("{}: '{}' disabled!".format(nick, arg)) output = self.output_message("{}: '{}' disabled!".format(nick, arg))
elif cmd == self.cmd_prefix + 'op' and op: elif cmd == 'op' and op:
for o in arg.split(' '): for o in arg.split(' '):
self.ops.append(o) self.ops.append(o)
output = self.output_message('{}: {} added as op'.format(nick, arg)) output = self.output_message('{}: {} added as op'.format(nick, arg))
elif cmd == self.cmd_prefix + 'deop' and op: elif cmd == 'deop' and op:
for o in arg.split(' '): for o in arg.split(' '):
self.ops = [i for i in self.ops if i != o] self.ops = [i for i in self.ops if i != o]
output = self.output_message('{}: {} removed as op'.format(nick, arg)) output = self.output_message('{}: {} removed as op'.format(nick, arg))
elif cmd == self.cmd_prefix + 'ops' and op: elif cmd == 'ops' and op:
output = self.output_message('current ops: {}'.format(', '.join(self.ops))) output = self.output_message('current ops: {}'.format(', '.join(self.ops)))
elif cmd == 'ban' and op:
for o in arg.split(' '):
self.banned_users.append(o)
output = self.output_message('{}: banned {}'.format(nick, arg))
elif cmd == 'unban' and op:
for o in arg.split(' '):
self.banned_users = [i for i in self.banned_users if i != o]
output = self.output_message('{}: removed ban for {}'.format(nick, arg))
elif cmd == 'banlist':
output = self.output_message('currently banned: {}'.format(', '.join(self.banned_users)))
return output return output
def call_plugins(self, privmsg, action, notice, chan, cmd, text, nick_list, nick, arg, msg_type): def call_plugins(self, privmsg, action, notice, chan, cmd, text, nick_list, nick, arg, msg_type):