2022-07-11 09:34:15 +00:00
|
|
|
import { overrideConsole } from 'nodejs-better-console';
|
|
|
|
import createFastify from 'fastify';
|
|
|
|
import Eris from 'eris';
|
|
|
|
|
|
|
|
import {
|
|
|
|
webPort,
|
2022-07-11 09:40:57 +00:00
|
|
|
webApiKey,
|
2022-07-11 09:34:15 +00:00
|
|
|
webRequestInterval,
|
|
|
|
botToken,
|
|
|
|
botWhitelist
|
|
|
|
} from './config.js';
|
|
|
|
|
|
|
|
overrideConsole();
|
|
|
|
|
|
|
|
const
|
|
|
|
fastify = createFastify(),
|
|
|
|
eris = new Eris(
|
|
|
|
botToken,
|
|
|
|
{ intents: ['directMessages'] }
|
|
|
|
);
|
|
|
|
|
2022-07-12 15:01:25 +00:00
|
|
|
fastify.addHook(
|
|
|
|
'onError',
|
|
|
|
error => console.error(error)
|
|
|
|
);
|
|
|
|
|
|
|
|
eris.on(
|
|
|
|
'error',
|
|
|
|
error => console.error(error)
|
|
|
|
);
|
|
|
|
|
2022-07-11 09:34:15 +00:00
|
|
|
let
|
2022-07-13 17:36:44 +00:00
|
|
|
getUrl,
|
2022-07-11 09:34:15 +00:00
|
|
|
nextRequestTimeout;
|
|
|
|
|
|
|
|
fastify.get(
|
|
|
|
'/',
|
|
|
|
async (
|
|
|
|
request,
|
|
|
|
reply
|
|
|
|
) => {
|
2022-07-11 09:40:57 +00:00
|
|
|
if(request.query['apiKey'] !== webApiKey)
|
|
|
|
return reply.code(401).send();
|
2022-07-13 17:36:44 +00:00
|
|
|
reply.send(getUrl ? await getUrl() : undefined);
|
2022-07-11 09:34:15 +00:00
|
|
|
clearTimeout(nextRequestTimeout);
|
|
|
|
nextRequestTimeout = setTimeout(
|
|
|
|
async () => {
|
|
|
|
await eris.editStatus('idle');
|
|
|
|
},
|
|
|
|
webRequestInterval
|
|
|
|
);
|
|
|
|
await eris.editStatus('online');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
eris.on(
|
|
|
|
'messageCreate',
|
2022-07-13 17:36:44 +00:00
|
|
|
async message => {
|
|
|
|
const {
|
|
|
|
guildID,
|
|
|
|
author: { id: authorID },
|
|
|
|
channel: { id: channelID },
|
|
|
|
id: messageID,
|
|
|
|
content
|
|
|
|
} = message;
|
2022-07-11 09:34:15 +00:00
|
|
|
if(
|
|
|
|
guildID
|
|
|
|
||
|
|
|
|
!botWhitelist.includes(authorID)
|
|
|
|
) return;
|
|
|
|
const reply = content => eris.createMessage(
|
|
|
|
channelID,
|
|
|
|
{
|
|
|
|
content,
|
|
|
|
messageReference: { messageID }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(content){
|
|
|
|
try {
|
|
|
|
new URL(content);
|
2022-07-13 17:36:44 +00:00
|
|
|
getUrl = () => new Promise(async resolve => {
|
|
|
|
resolve(content);
|
|
|
|
getUrl = undefined;
|
|
|
|
await message.addReaction('✅');
|
|
|
|
});
|
|
|
|
await message.addReaction('⏳');
|
2022-07-11 09:34:15 +00:00
|
|
|
}
|
|
|
|
catch {
|
2022-07-13 17:36:44 +00:00
|
|
|
await message.addReaction('❌');
|
2022-07-11 09:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
await fastify.listen({ port: webPort });
|
|
|
|
await Promise.all([
|
|
|
|
eris.connect(),
|
|
|
|
new Promise(resolve => eris.once('ready', resolve))
|
|
|
|
]);
|
|
|
|
await eris.editStatus('idle');
|
|
|
|
console.log('Ready');
|
|
|
|
})().catch(error => console.error(error));
|