This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
forked from MiscDog/lushenda-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
249 lines (209 loc) · 6.94 KB
/
bot.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
use Discord\Discord;
use Discord\Parts\Channel\Message;
require __DIR__ . '/vendor/autoload.php';
include_once(__DIR__ . '/env.php');
const LOCAL_HEX_PATH = 'db';
const LOCAL_HEX_DICT = LOCAL_HEX_PATH . '/local_hex_dict.csv';
$discord = new Discord([
'token' => $discordToken
]);
$discord->on('message', static function (Message $message, Discord $discord) {
$acceptedChannelIds = [
"912506665079828491", // adhoc-uploads - DQX Tools (ENG)
"856955528944681021" // bot-test2 - MiscDog
];
if(in_array($message->channel_id, $acceptedChannelIds, true) && is_array($message->attachments) && count($message->attachments) > 0) {
try {
// Check if uploaded a zip - only check 1st attachment
if(validZipFile($message->attachments[0])) {
// Store file for simplicity
$file = $message->attachments[0];
// Build temp file path
$filePath = $file->url;
$fileName = $file->id;
// Extract Zip to temp path
$zipTempPath = extractZip($filePath, $fileName);
// Prepare data
$uploadedData = prepareUserUploadedData($zipTempPath);
$typeCount = [
'master' => [],
'local' => []
];
$newData = compareAgainstExisting($uploadedData, $typeCount);
// Add to local database and inform user
addToLocalDatabase($newData, $zipTempPath);
$msg = updateUser($typeCount);
$message->reply($msg);
// Clean up uploaded zip
unlink($zipTempPath . ".zip");
recursiveRmdir($zipTempPath);
}
} catch(\Error $e) {
$message->reply($e->getMessage());
}
// Delete the users message after an attempt
$message->delete();
}
});
/**
* Check if this zip file is valid
* @param $file
* @return bool
*/
function validZipFile($file): bool {
if($file->content_type === "application/zip") {
return true;
}
throw new \Error("uploaded file does not appear to be a zip.");
}
/**
* Extract the zip to a temp folder
* @param $filePath
* @param $fileName
* @return string
*/
function extractZip($filePath, $fileName): string {
$directory = 'tmp';
$zipTempPath = $directory . '/' . $fileName;
// Copy zip locally
if (!copy($filePath, $zipTempPath . '.zip')) {
throw new \Error('cannot copy zip for extraction.');
}
// Unzip
$zip = new ZipArchive;
if ($zip->open($zipTempPath . '.zip') === TRUE) {
$zip->extractTo($zipTempPath);
$zip->close();
} else {
throw new \Error('zip opening failed :crying_cat_face:');
}
return $zipTempPath;
}
/**
* Prepare the data for comparing
* @param $zipTempPath
* @return array
*/
function prepareUserUploadedData($zipTempPath): array {
// Empty Data
$uploadedData = [];
// Target directly file
$expectedSubFolder = 'new_adhoc_dumps';
$filename = 'new_hex_dict.csv';
$tempPathHexDict = $zipTempPath . '/' . $expectedSubFolder . '/' . $filename;
if(!is_file($tempPathHexDict)) {
throw new \Error("couldn't find `new_hex_dict.csv` in uploaded zip. Please zip the entire `new_adhoc_dumps` folder.");
}
// Open and Read individual CSV file
if (($handle = fopen($tempPathHexDict, 'r')) !== false) {
// Skip header
fgetcsv($handle, 1000);
while (($dataValue = fgetcsv($handle, 1000)) !== false) {
$uploadedData[] = $dataValue;
}
}
return $uploadedData;
}
/**
* Compare data against hex dict and local dict
* @param $uploadedData
* @param $typeCount
* @return array
*/
function compareAgainstExisting($uploadedData, &$typeCount): array {
// Build CSV for comparing
$hexDict = 'https://raw.githubusercontent.com/jmctune/dqxclarity/weblate/app/hex_dict.csv';
$existingHexValues = buildArrayFromHexDictCsv($hexDict);
$valuesNotFound = compareUploadedHexesAgainstExisting($uploadedData, $existingHexValues, $typeCount['master']);
echo "Found " . count($valuesNotFound) . " new hex" . (count($valuesNotFound) === 1 ? '' : 'es') . " in " . "the master hex database" . PHP_EOL;
// Creates a file if it doesn't exist
if(!file_exists(LOCAL_HEX_DICT)) {
$dictHeader = "file,hex_string\r\n";
file_put_contents(LOCAL_HEX_DICT, $dictHeader);
}
$localHexValues = buildArrayFromHexDictCsv(LOCAL_HEX_DICT);
$valuesNotFound = compareUploadedHexesAgainstExisting($valuesNotFound, $localHexValues, $typeCount['local']);
echo "Found " . count($valuesNotFound) . " new hex" . (count($valuesNotFound) === 1 ? '' : 'es') . " in " . "the Discord bot local database" . PHP_EOL;
return $valuesNotFound;
}
/**
* @param $typeCount
* @return string
*/
function updateUser($typeCount): string {
if($typeCount['master'] > 0 && $typeCount['local'] === $typeCount['master']) {
return "whoa :heart_eyes:! there's " . $typeCount['master'] . " entirely new hex" . ($typeCount['master'] === 1 ? '' : 'ex') . " in that zip you uploaded. Rawr~ :white_heart:";
} else if ($typeCount['master'] > 0) {
return "ouh, you seem to have " . $typeCount['master'] . " hex" . ($typeCount['master'] === 1 ? '' : 'ex') . " that " . ($typeCount['master'] === 1 ? "hasn't" : "haven't") . " been added to the master hex list yet. I'm sure Serany will get right on that :blush:!";
}
return "oh. We already have all these hexes. Thanks anyways babe :kissing_heart: ";
}
/**
* Checks if we have this value or not
* @param $uploadedData
* @param $existingHexValues
* @param $typeCount
* @return array
*/
function compareUploadedHexesAgainstExisting($uploadedData, $existingHexValues, &$typeCount): array {
// Loop through hex_dict to compare with zip
$newHexFound = [];
foreach($uploadedData as $row) {
if(!in_array($row[1], $existingHexValues, true)) {
$newHexFound[] = $row;
}
}
$typeCount = count($newHexFound);
return $newHexFound;
}
/**
* Maintain record of all CSVs + combine with the raw
* @param $remainingNotFound
* @param $zipTempPath
*/
function addToLocalDatabase($remainingNotFound, $zipTempPath) {
if(!empty($remainingNotFound)) {
// Add new line to CSV
$csvHandle = fopen(LOCAL_HEX_DICT, 'ab');
foreach($remainingNotFound as $newEntry) {
fputcsv($csvHandle, $newEntry);
// Copy to en and ja folders
copy($zipTempPath . "/new_adhoc_dumps/ja/" . $newEntry[0] . ".json", LOCAL_HEX_PATH . "/ja/" . $newEntry[0] . ".json");
copy($zipTempPath . "/new_adhoc_dumps/en/" . $newEntry[0] . ".json", LOCAL_HEX_PATH . "/en/" . $newEntry[0] . ".json");
}
}
}
/**
* Returns an array from a specific CSV format in which the 2nd column contains hex strings
* @param $dictPath
* @return array
*/
function buildArrayFromHexDictCsv($dictPath): array {
$csvFull = array_map('str_getcsv', file($dictPath));
$csvTrim = array_map(static function($v) {
return $v[1];
}, $csvFull);
array_shift($csvTrim);
return $csvTrim;
}
/**
* Recursive rmdir for cleaning zip uploads
* @param $dir
*/
function recursiveRmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object !== "." && $object !== "..") {
if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object)) {
recursiveRmdir($dir. DIRECTORY_SEPARATOR .$object);
} else {
unlink($dir . DIRECTORY_SEPARATOR . $object);
}
}
}
rmdir($dir);
}
}
$discord->run();