jillo-bot/src/index.ts

142 lines
3.6 KiB
TypeScript

import * as Discord from 'discord.js';
import * as fs from 'fs';
const { token, disableDaytimeAnnouncements } = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
import * as path from 'path';
const morningHour = 8;
const eveningHour = 21;
function getNextMorning() {
const now = new Date();
const next = new Date();
if (now.getUTCHours() >= morningHour) {
next.setTime(next.getTime() + 1000 * 60 * 60 * 24);
}
next.setUTCHours(morningHour);
next.setUTCMinutes(Math.floor(Math.random() * 60));
next.setUTCSeconds(Math.floor(Math.random() * 60));
console.log('next morning set to ' + next);
return next.getTime();
}
function getNextNight() {
const now = new Date();
const next = new Date();
if (now.getUTCHours() >= eveningHour) {
next.setTime(next.getTime() + 1000 * 60 * 60 * 24);
}
next.setUTCHours(eveningHour);
next.setUTCMinutes(Math.floor(Math.random() * 60));
next.setUTCSeconds(Math.floor(Math.random() * 60));
console.log('next night set to ' + next);
return next.getTime();
}
interface DaytimeSchedule {
morning: number;
night: number;
}
let next: DaytimeSchedule;
function save() {
fs.writeFileSync('./next.json', JSON.stringify(next));
}
if (fs.existsSync('./next.json')) {
next = JSON.parse(fs.readFileSync('./next.json', 'utf8'));
} else {
next = {
morning: getNextMorning(),
night: getNextNight()
};
save();
}
const bot = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_VOICE_STATES,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
});
const channels = [
'587108210683412493'
];
const morning = [
'goob morning',
'gm besties',
'morning !!!',
'it is Morning',
'gm',
'goom morbning'
];
const night = [
'good night!!!!!!',
'nini all',
'goob night',
'goo night .....',
'sleep well everyone!!!!!',
'good night !! dont let the bugs bite'
];
bot.on('ready', async () => {
setInterval(() => {
const current = new Date().getTime();
// console.log(current, next.morning, next.night);
if (current > next.morning && !disableDaytimeAnnouncements) {
next.morning = getNextMorning();
channels.forEach(c => {
bot.channels.fetch(c)
.then(channel => (channel as Discord.TextBasedChannel).send(morning[Math.floor(Math.random() * morning.length)]))
.catch(err => console.log('couldnt find channel ' + c + ': ' + err));
});
save();
}
if (current > next.night && !disableDaytimeAnnouncements) {
next.night = getNextNight();
channels.forEach(c => {
bot.channels.fetch(c)
.then(channel => (channel as Discord.TextBasedChannel).send(night[Math.floor(Math.random() * night.length)]))
.catch(err => console.log('couldnt find channel ' + c + ': ' + err));
});
save();
}
}, 1000);
bot.commands = new Discord.Collection();
const cmdFiles = fs.readdirSync(path.join(__dirname, './commands')).filter((file) => file.endsWith('.js'));
for (const file of cmdFiles) {
const cmd = (await import(`./commands/${file}`));
bot.commands.set(cmd.data.name, cmd);
if (cmd.onClientReady) cmd.onClientReady(bot);
}
console.log('foggy online');
});
bot.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, interaction.member);
} catch (error) {
interaction.reply({ content: '`ERROR`', ephemeral: true });
console.error(error);
}
});
bot.login(token);