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
|
|
|
|
url,
|
|
|
|
nextRequestTimestamp,
|
|
|
|
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-11 09:34:15 +00:00
|
|
|
reply.send(url);
|
|
|
|
url = '';
|
|
|
|
nextRequestTimestamp = Date.now() + webRequestInterval;
|
|
|
|
clearTimeout(nextRequestTimeout);
|
|
|
|
nextRequestTimeout = setTimeout(
|
|
|
|
async () => {
|
|
|
|
await eris.editStatus('idle');
|
|
|
|
},
|
|
|
|
webRequestInterval
|
|
|
|
);
|
|
|
|
await eris.editStatus('online');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
eris.on(
|
|
|
|
'messageCreate',
|
|
|
|
async ({
|
|
|
|
guildID,
|
|
|
|
author: { id: authorID },
|
|
|
|
channel: { id: channelID },
|
|
|
|
id: messageID,
|
|
|
|
content
|
|
|
|
}) => {
|
|
|
|
if(
|
|
|
|
guildID
|
|
|
|
||
|
|
|
|
!botWhitelist.includes(authorID)
|
|
|
|
) return;
|
|
|
|
const reply = content => eris.createMessage(
|
|
|
|
channelID,
|
|
|
|
{
|
|
|
|
content,
|
|
|
|
messageReference: { messageID }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(content){
|
|
|
|
try {
|
|
|
|
new URL(content);
|
|
|
|
url = content;
|
|
|
|
await reply(
|
|
|
|
nextRequestTimestamp && nextRequestTimestamp > Date.now()
|
|
|
|
? `ETA: ${Math.ceil((nextRequestTimestamp - Date.now()) / 1000)} seconds.`
|
|
|
|
: 'ETA: as soon as online.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
await reply('Invalid URL');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
(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));
|