$announceGratz = new autogratz(&$bot);
/*
The Class itself...
*/
class autogratz Extends BaseActiveModule
{
var $bot;
/*
Constructor:
Hands over a referance to the "Bot" class.
*/
function __construct (&$bot)
{
parent::__construct(&$bot, get_class($this));
$this -> register_event('buddy');
$this -> register_command('all', 'autogratz', 'ADMIN', array('add' => 'ADMIN', 'addlevel' => 'ADMIN', 'remove' => 'ADMIN', 'list' => 'ADMIN'));
$this -> bot -> core("settings") -> create("autogratz", "Enabled", "Off", "Turns the functionality of this module on and off.", "On;Off");
$this -> bot -> core("settings") -> create("autogratz", "SendTo", "gc", "Send the message via tell or guild chat.", "gc;tell");
$this -> bot -> db -> query("CREATE TABLE IF NOT EXISTS " . $this -> bot -> db -> define_tablename("gratz_messages", "true") . " (id int(10) unsigned NOT NULL AUTO_INCREMENT, level int(10) unsigned NOT NULL DEFAULT '0', message varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (id)) DEFAULT CHARSET=utf8;");
$this -> bot -> db -> update_table('gratz_messages', 'id', 'add', "ALTER TABLE #___gratz_messages ADD COLUMN id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, DROP PRIMARY KEY, ADD PRIMARY KEY (id);");
$this -> help['description'] = "Congratulates a guild member on gaining a level.";
$this -> help['command']['autogratz add <message>']="Adds a congratulations message (use <name> for the characters name and <level> for the characters level).";
$this -> help['command']['autogratz addlevel <level> <message>']="Adds a congratulations message (use <name> for the characters name and <level> for the characters level).";
$this -> help['command']['autogratz remove <id>']="Removes a levelling message.";
$this -> help['command']['autogratz list']="Displays a list of levelling messages.";
}
function command_handler($name, $msg, $origin)
{
$this->error->reset(); //Reset the error message so we don't trigger the handler by old error messages.
$com = $this->parse_com($msg, array('com', 'sub', 'level', 'message'));
switch($com['sub'])
{
case 'add':
return $this -> add_gratz(0, $com['level'].' '.$com['message']);
case 'addlevel':
return $this -> add_gratz($com['level'], $com['message']);
case 'remove':
return $this -> remove_gratz($com['level']);
case 'list':
return $this -> list_gratz();
}
return 'Please use autograts add or autogratz addlevel to add messages.';
}
function add_gratz($level, $message)
{
$level = trim($level);
if('' == $level)
$level = 0;
if($this -> bot -> db -> query("INSERT INTO #___gratz_messages (level, message) VALUES (".$level.", '".mysql_real_escape_string(html_entity_decode($message))."');"))
return 'The message has been added.';
else
return 'Problem occured while adding a message.';
}
function remove_gratz($id)
{
if($this -> bot -> db -> query("DELETE FROM #___gratz_messages WHERE id = '".mysql_real_escape_string($id)."';"))
return 'The message has been removed.';
else
return 'A problem occured while removing the message.';
}
function list_gratz()
{
$results = $this -> bot -> db -> select("SELECT id, level, message FROM #___gratz_messages ORDER BY level, message");
if (!empty($results))
{
$old_level = -1;
$inside = "Gratz Messages\n\n";
foreach ($results as $val)
{
if($old_level != $val[1])
{
if(0 == $val[1])
$inside .= "For All Levels :: \n\n";
else
$inside .= "For Level ".$val[1]." :: \n\n";
}
$inside .= htmlentities($val[2])." [".$this -> bot -> core("tools") -> chatcmd("autogratz remove " . $val[0], "Delete")."]";
$inside .= "\n\n";
$old_level = $val[1];
}
}
return "Gratz Messages :: " . $this -> bot -> core("tools") -> make_blob("click to view", $inside);
}
function buddy($name, $status, $level)
{
if(7 == $status)
{
// If the module is active
if('on' == strtolower($this -> bot -> core("settings") -> get("Autogratz", "Enabled")))
{
if(!is_numeric($level) && !is_int((real) $level))
{
$this -> bot -> log("BUDDY", "LOG", "Invalid level data");
return;
}
$user = $this -> bot -> core("chat") -> get_uname($name);
$mem = $this -> bot -> core("notify") -> check($user);
// Check if $name is a guild member
if ($mem)
{
$class_name = $this -> bot -> core("Whois") -> class_name[$classId];
$who = $this -> bot -> core("Whois") -> lookup($name);
$messages = $this -> bot -> db -> select("SELECT message FROM #___gratz_messages WHERE level = ".$level." ORDER BY RAND() LIMIT 1;");
if (empty($messages))
{
$messages = $this -> bot -> db -> select("SELECT message FROM #___gratz_messages WHERE level = 0 ORDER BY RAND() LIMIT 1;");
if (empty($messages))
{
$message = "Congratulations on level , ";
}
}
if('' == $message)
{
$message = array_shift($messages);
$message = $message[0];
}
$message = str_replace('', $name, $message);
$message = str_replace('', $level, $message);
$message = htmlentities($message);
// Send Gratz message to guild chat and log the event
if('gc' == strtolower($this -> bot -> core("settings") -> get("Autogratz", "SendTo")))
$this -> bot -> send_gc($message);
else
$this -> bot -> send_tell($name, $message);
$this -> bot -> log("AUTOGRATZ", "LOG", $name . " achieved level " . $level);
}
}
}
}
}
?>