-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkHandler.ts
205 lines (193 loc) · 8.4 KB
/
linkHandler.ts
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
import https from 'https';
import tesseract from 'tesseract.js';
import * as Discord from 'discord.js';
import fs from 'fs';
import { filespath, path } from './path.js';
import * as checks from './src/checks.js';
import * as func from './src/func.js';
import * as osucmds from './commands/cmdosu.js';
import * as extypes from './src/types/extratypes.js';
let imgParseCooldown = false;
export async function onMessage(
input: extypes.input,
message: Discord.Message
) {
let canReply = true;
if (!checks.botHasPerms(message, input.client, ['ReadMessageHistory'])) {
canReply = false;
}
const currentDate = new Date();
const button = null;
const args = [];
const obj = message;
let parse = null;
const commandType: extypes.commandType = 'link';
if (!(message.content.startsWith('http') || message.content.includes('osu.') || message.attachments.size > 0)) {
return;
}
const overrides = {
user: null,
page: null,
mode: null,
sort: null,
reverse: null,
ex: null,
commandAs: commandType
};
let absoluteID = func.generateId();
let settings: extypes.guildSettings;
try {
const curGuildSettings = await input.guildSettings.findOne({ where: { guildid: message.guildId } });
settings = curGuildSettings.dataValues;
} catch (error) {
try {
await input.guildSettings.create({
guildid: message.guildId,
guildname: message?.guild?.name ?? 'Unknown',
prefix: 'sbr-',
osuParseLinks: true,
osuParseScreenshots: true,
osuParseReplays: true,
});
} catch (error) {
}
settings = {
guildid: message.guildId,
guildname: message?.guild?.name ?? 'Unknown',
prefix: 'sbr-',
osuParseLinks: true,
osuParseScreenshots: false,
osuParseReplays: true,
};
}
//disabled for now
if (/* false && */ input.config.useScreenshotParse == true && settings.osuParseScreenshots == true) {
//warning: uses a lot of memory
//if message attachments size > 0
if (imgParseCooldown == false) {
if (message.attachments.size > 0) {
if (message.attachments.first().url.includes('.png') || message.attachments.first().url.includes('.jpg')) {
// const worker = tesseract.createWorker({
// logger: m => {
// fs.appendFileSync(`${path}/logs/gen/imagerender${obj.guildId}.log`,
// `
// ================================
// ${currentDate.toISOString()}
// ID: ${absoluteID}
// workerID: ${m.workerId}
// jobID: ${m.jobId}
// userjobID: ${m.userJobId}
// status: ${m.status ? m.status : 'none/completed'}
// progress: ${m.progress ? m.progress : 'none'}
// ================================
// `
// );
// }
// });
imgParseCooldown = true;
await (async () => {
await tesseract.recognize(message.attachments.first().url, 'eng', {
logger: m => {
fs.appendFileSync(`${path}/logs/gen/imagerender${obj.guildId}.log`,
`
================================
${currentDate.toISOString()}
ID: ${absoluteID}
workerID: ${m.workerId}
jobID: ${m.jobId}
userjobID: ${m.userJobId}
status: ${m.status ? m.status : 'none/completed'}
progress: ${m.progress ? m.progress : 'none'}
================================
`
);
}
});
const { data: { text } } = await tesseract.recognize(message.attachments.first().url, 'eng', {
logger: m => {
fs.appendFileSync(`${path}/logs/gen/imagerender${obj.guildId}.log`,
`
================================
${currentDate.toISOString()}
ID: ${absoluteID}
workerID: ${m.workerId}
jobID: ${m.jobId}
userjobID: ${m.userJobId}
status: ${m.status ? m.status : 'none/completed'}
progress: ${m.progress ? m.progress : 'none'}
================================
`
);
}
});
if (text.includes('Beatmap by')) {
const txttitle = text.split('\n')[0];
const txtcreator = text.split('Beatmap by ')[1].split('\n')[0];
parse = `${txttitle} ${txtcreator}`;
}
if (text.includes('Mapped by')) {
const txttitle = text.split('\n')[0];
const txtcreator = text.split('Mapped by ')[1].split('\n')[0];
parse = `${txttitle} ${txtcreator}`;
}
})();
}
}
}
if (imgParseCooldown == true) {
setTimeout(() => {
imgParseCooldown = false;
}, 5000);
}
}
if (parse) {
args.push('query', parse);
}
const messagenohttp = message.content.replace('https://', '').replace('http://', '').replace('www.', '');
if (messagenohttp.startsWith('osu.ppy.sh/b/') || messagenohttp.startsWith('osu.ppy.sh/beatmaps/') || messagenohttp.startsWith('osu.ppy.sh/beatmapsets/') || messagenohttp.startsWith('osu.ppy.sh/s/') || parse != null) {
overrides.ex = 'link';
if (absoluteID == null) {
absoluteID = func.generateId();
}
osucmds.map({ commandType, obj, args, canReply, button, config: input.config, client: input.client, absoluteID, currentDate, overrides, userdata: input.userdata });
}
if (messagenohttp.startsWith('osu.ppy.sh/u/') || messagenohttp.startsWith('osu.ppy.sh/users/')) {
if (absoluteID == null) {
absoluteID = func.generateId();
}
osucmds.osu({ commandType, obj, args, canReply, button, config: input.config, client: input.client, absoluteID, currentDate, overrides, userdata: input.userdata, statsCache: input.statsCache });
}
if (message.attachments.size > 0 && message.attachments.every(attachment => func.removeURLparams(attachment.url).endsWith('.osr'))) {
if (settings.osuParseReplays == false) {
return;
}
if (absoluteID == null) {
absoluteID = func.generateId();
}
const attachosr = message.attachments.first().url;
const osrdlfile = fs.createWriteStream(`${filespath}/replays/${absoluteID}.osr`);
https.get(`${attachosr}`, function (response) {
response.pipe(osrdlfile);
});
setTimeout(() => {
osucmds.replayparse({ commandType, obj, args, canReply, button, config: input.config, client: input.client, absoluteID, currentDate, overrides, userdata: input.userdata, });
}, 1500);
}
if (messagenohttp.startsWith('osu.ppy.sh/scores/')) {
osucmds.scoreparse({ commandType, obj, args, canReply, button, config: input.config, client: input.client, absoluteID, currentDate, overrides, userdata: input.userdata, statsCache: input.statsCache });
}
if (message.attachments.size > 0 && message.attachments.every(attachment => func.removeURLparams(attachment.url).endsWith('.osu'))) {
// return;
if (absoluteID == null) {
absoluteID = func.generateId();
}
const attachosu = message.attachments.first().url;
const osudlfile = fs.createWriteStream(`${filespath}/localmaps/${absoluteID}.osu`);
https.get(`${attachosu}`, function (response) {
response.pipe(osudlfile);
});
setTimeout(() => {
osucmds.maplocal({ commandType, obj, args, canReply, button, config: input.config, client: input.client, absoluteID, currentDate, overrides, userdata: input.userdata });
}, 1500);
}
};