This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathespybot.php
215 lines (183 loc) · 7.18 KB
/
espybot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
//Load F3 framework
$f3=require('./vendor/bcosca/fatfree-core/base.php');
$f3 = Base::instance();
//Load Discord
use Discord\Discord;
use Discord\WebSockets\Event;
use Discord\WebSockets\WebSocket;
use Discord\WebSockets\Intents;
use Logger\Botlog;
//Allow up to 1GB memory usage
ini_set("memory_limit", "1024M");
//Set default timezone
date_default_timezone_set('America/New_York');
//Define the current dir
define("BASEDIR", __DIR__);
define("PLUGINDIR", __DIR__ . "/plugins/");
//In case we started from a different directory
chdir(BASEDIR);
//Bot start time
$startTime = time();
//Load all the vendor files
require_once(BASEDIR . "/vendor/autoload.php");
//Load configuration
$f3->config('config/config.ini');
//Load logger
require_once(__DIR__ . "/classes/log.php");
$logger = new Botlog();
//Startup Discord lib
$discord = new Discord([
'token' => $f3->get('token'),
'loadAllMembers' => true,
'intents' => Intents::getDefaultIntents() | Intents::GUILD_MEMBERS | Intents::GUILD_MESSAGE_REACTIONS,
]);
//Load the library files
foreach (glob(__DIR__ . "/library/*.php") as $lib){
require_once($lib);
$logger->debug("Loaded library: ".str_replace(".php", "", basename($lib)));
}
//Start the cacher
$cache = new prefcache();
$discord->logger = $logger;
//Load the plugins
$plugins = array();
foreach (glob(__DIR__ . "/plugins/*.php") as $plugin) {
$fileName = str_replace(".php", "", basename($plugin));
if($f3->get('plugins.'.$fileName)){
require_once($plugin);
$p = new $fileName();
$p->init($f3, $discord, $logger);
$plugins[] = $p;
$logger->debug("Initialized plugin: {$fileName}");
}
}
//Build command list
$commands = array();
$plugin_cmds = array();
foreach ($plugins as $plugin) {
$infoarr = $plugin->information();
if(is_countable($infoarr) && count($infoarr) !== 0){
foreach($infoarr as $info){
if ($info["name"]){
$commands[] = $f3->get('trigger').$info["name"];
$plugin_cmds[$info["name"]] = $plugin;
}
}
}
}
//Show in use memory
$discord->loop->addPeriodicTimer(1800, function () use ($logger) {
$logger->info("Memory in use: " . round(memory_get_usage() / 1024 / 1024, 2) . "MB");
});
//Setup websocket hooks
$discord->on("ready", function() use ($discord, $logger, $plugins) {
$logger->notice("Connection Opened");
});
$discord->on('reconnecting', function () use ($logger) {
$logger->err('Discord WebSocket is reconnecting...');
});
$discord->on('reconnected', function () use ($logger) {
$logger->err('Discord WebSocket has reconnected.');
});
$discord->on('close', function ($op, $reason) use ($logger) {
$logger->err("Discord WebSocket closed with close code {$op} reason {$reason}");
});
$discord->on('error', function ($e) use ($logger) {
$logger->err('Discord WebSocket encountered an error.', [$e]);
});
//On a message, do all of the following
$discord->on(Event::MESSAGE_CREATE, function ($msgData, $botData) use ($logger, $discord, $plugins, $f3, $commands, $plugin_cmds){
//If we sent the message, just ignore it
if($msgData->user_id != $discord->id) {
//Check for command, so we don't do all of this expensive shit for a random message
if ($command = containsTrigger($msgData->content, $commands)) {
$channelData = $msgData->channel;
//Log command
$logger->debug("Command processed -> ".$msgData->content);
//Store message object
$msgobject = $msgData;
//If PM, set private flag
if(!$msgData->channel->guild_id){
$is_private = true;
}else{
$is_private = false;
}
//Create the data array for the plugins to use
$msgData = array(
"isBotOwner" => false,
"user" => $msgData->user,
"message" => array(
"timestamp" => $msgData->timestamp->setTimezone('America/New_York')->toDateTimeString(),
"id" => $msgData->user_id,
"message" => $msgData->content,
"channelID" => $msgData->channel_id,
"from" => $msgData->user->username,
"fromID" => $msgData->user_id,
"fromDiscriminator" => $msgData->user->discriminator,
"fromAvatar" => $msgData->user->avatar,
"attachments" => $msgData->attachments,
"isPrivate" => $is_private
),
"channel" => $channelData,
"guild" => $channelData->is_private ? array("name" => "PM") : array("id" => $channelData->guild_id, "name" => $channelData->guild->name),
"object" => $msgobject
);
//Fire main function for public command
try{
$plugin_cmds[$command]->onMessage($msgData);
}catch (\Exception $e){
$logger->err("Command: ".$e->getMessage());
}
//Check if user is an admin or bot owner
if(is_admin($msgData["message"]["fromID"]) || is_bot_owner($msgData["message"]["fromID"])){
//Fire main function for admin command
try{
$plugin_cmds[$command]->onMessageAdmin($msgData);
}catch (\Exception $e){
$logger->err("Admin Command: ".$e->getMessage());
}
}
}else{
//This is not a command, process for logging
//dump($msgData->channel);
$channelData = $msgData->channel;
//If PM, set private flag
if(!$msgData->channel->guild_id){
$is_private = true;
}else{
$is_private = false;
}
//Create the data array for the plugins to use
$msgData = array(
"isBotOwner" => false,
"user" => $msgData,
"message" => array(
"timestamp" => $msgData->timestamp->setTimezone('America/New_York')->toDateTimeString(),
"id" => $msgData->user_id,
"message" => $msgData->content,
"channelID" => $msgData->channel_id,
"from" => $msgData->user->username,
"fromID" => $msgData->user_id,
"fromDiscriminator" => $msgData->user->discriminator,
"fromAvatar" => $msgData->user->avatar,
"attachments" => $msgData->attachments,
"isPrivate" => $is_private
),
"channel" => $channelData,
"guild" => $channelData->is_private ? array("name" => "PM") : array("id" => $channelData->guild_id, "name" => $channelData->guild->name),
"botData" => $botData
);
//Run the plugins log functions
foreach($plugins as $plugin){
try{
$plugin->onLog($msgData);
}catch (\Exception $e){
$logger->err("Log Error: " . $e->getMessage());
}
}
}
}
});
//Start the bot
$discord->run();