From 01030eeb9fdea7bcba825554f33cf4e1b74f7e74 Mon Sep 17 00:00:00 2001 From: Lerk Hern Date: Fri, 9 Oct 2015 12:39:30 -0500 Subject: [PATCH] Persist aliases in database Aliases will be written and loaded from the database when the bot starts, also added commands to list the aliases, and restore them if they didn't load properly --- scripts/alias.rb | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/scripts/alias.rb b/scripts/alias.rb index 68cda79..7d3b0ec 100644 --- a/scripts/alias.rb +++ b/scripts/alias.rb @@ -25,6 +25,22 @@ register 'Manipulate command aliases.' +event :em_started do + load_aliases +end + +command 'restorealiases', 'Restore all aliases ever added' do + level! 10 + load_aliases +end + +command 'listaliases', 'List all of the aliases' do + level! 3 + get_all_data.each do |(aliascommand, aliastarget)| + reply "#{aliascommand} - #{aliastarget}" + end +end + command 'alias', 'Manipulate the command alias list. Syntax: ADD alias target|DELETE alias' do level! 5 and argc! 2 @@ -32,16 +48,36 @@ when 'add' argc! 3 - Bot::Commands.create_alias @params[1], @params[2] - + aliascommand = @params[1].downcase + aliastarget = @params[2].downcase + + Bot::Commands.create_alias aliascommand, aliastarget + store_data aliascommand, aliastarget + reply 'Alias created' when 'delete', 'del' - Bot::Commands.delete_alias @params[1] - + + aliaskey = @params[1].downcase + Bot::Commands.delete_alias aliaskey + delete_data aliaskey + reply 'Alias deleted.' end end +helpers do + + def load_aliases + get_all_data.each do |(aliascommand, aliastarget)| + begin + Bot::Commands.create_alias aliascommand.dup, aliastarget.dup + rescue Exception => e + reply e.message + end + end + end + +end # vim: set tabstop=2 expandtab: