-
Notifications
You must be signed in to change notification settings - Fork 0
/
JussiNet.class.php
113 lines (91 loc) · 2.85 KB
/
JussiNet.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Test class for Jussinet
*/
class JussiNet
{
public $commands = [];
public $bot;
function __construct()
{
include_once("Bot.class.php");
$this->bot = new Bot();
}
function init()
{
# Include commands file here
include_once("bot_commands.php");
$this->do_ajax();
}
function do_ajax()
{
if(isset($_GET['ajax']) && $_GET['ajax'] == 1 )
{
# Ajax calls
$results = [];
if(isset($_POST['method']) && $_POST['method'] == 'sendMessage')
{
$results = (object)[
'success' => false,
'data' => []
];
if(isset($_POST['form']))
{
parse_str( $_POST['form'], $input );
$message = $input['message'] ? $input['message'] : '';
$results->data = $this->call_command($message);
$results->success = true;
}
}
echo json_encode($results);
exit();
}
}
function do_command(String $command, String $value, $callable)
{
$obj = (object) [
'command' => $command,
'value' => $value,
'callable' => $callable
];
$this->commands[] = $obj;
}
function call_command($message)
{
$fn_params = [];
foreach ( $this->commands as $cmd)
{
if( $cmd->command == 'call_command' )
{
$parts_msg = explode(" ", $message);
if(isset($parts_msg[0][0]) && $parts_msg[0][0] == '!')
{
$parts_value = explode(" ", $cmd->value);
foreach ($parts_value as $part_key => $part_value) {
if(isset($part_value[0]) == $parts_msg[0] )
{
$message = str_replace($parts_msg[0], "", $message);
$fn_params[] = $this->bot;
$fn_params[] = trim($message) ?? false;
return call_user_func_array($cmd->callable, $fn_params );
}
}
}
if( strtolower($cmd->value) == strtolower($message) )
{
if(is_callable($cmd->callable))
{
$this->bot->set_params( $message );
$fn_params[] = $this->bot;
return call_user_func_array($cmd->callable, $fn_params);
}
else {
throw new Exception("$cmd->callable is not callable");
}
}
}
}
return "I dont know what command.";
}
}
?>