discord-home-cinema/main.js

103 lines
2.3 KiB
JavaScript
Raw Normal View History

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'] }
);
fastify.addHook(
'onError',
error => console.error(error)
);
eris.on(
'error',
error => console.error(error)
);
2022-07-11 09:34:15 +00:00
let
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();
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',
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);
getUrl = () => new Promise(async resolve => {
resolve(content);
getUrl = undefined;
await message.addReaction('✅');
});
await message.addReaction('⏳');
2022-07-11 09:34:15 +00:00
}
catch {
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));