From 54757e110fffdd831f5bec07b9d4de6011b2331d Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Wed, 22 Mar 2023 13:59:06 +0300 Subject: [PATCH] move subscriptions into their own file --- src/commands/subscribe.ts | 3 +- src/index.ts | 178 ++----------------------------------- src/lib/subscriptions.ts | 179 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 173 deletions(-) create mode 100644 src/lib/subscriptions.ts diff --git a/src/commands/subscribe.ts b/src/commands/subscribe.ts index f585036..9f5154b 100644 --- a/src/commands/subscribe.ts +++ b/src/commands/subscribe.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder } from '@discordjs/builders'; import { CommandInteraction } from 'discord.js'; -import { subscriptions, timeAnnouncements } from '../index'; +import { subscriptions, timeAnnouncements } from '../lib/subscriptions'; module.exports = { data: new SlashCommandBuilder() @@ -30,5 +30,6 @@ module.exports = { content: `<#${interaction.channelId}> has been subscribed to \`${announcementType}\`` }); } + } }; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 26987e9..00a0218 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,152 +3,10 @@ import * as fs from 'fs'; const { token, disableDaytimeAnnouncements } = JSON.parse(fs.readFileSync('./config.json', 'utf8')); import * as path from 'path'; import { initializeCounter } from './lib/counter'; +import { getNextAnnouncementTime, initializeAnnouncements, loadNext, loadSubscriptions, next, saveNext, subscriptions, timeAnnouncements } from './lib/subscriptions'; -interface AnnouncementType { - hour: number; - randomizeMinute: boolean; - messages: string[], - messagesPrefix?: string -} - -export const timeAnnouncements: Record = { - morning: { - hour: 7, - randomizeMinute: true, - messages: [ - 'goob morning', - 'gm besties', - 'morning !!!', - 'it is Morning', - 'gm', - 'goom morbning', - 'goo morning', - 'hiiiii besties ........', - 'good morning!!!!', - 'awake, but at what cost ... ehehe hi all', - 'morning all!!!', - 'rise and shine!! new day!!', - 'mmm... morning', - 'morning to all of my slime fellows!!!! oh and the rest of you, uhm.. good morning i guess πŸ™„', - 'gm gamers... rise and grind', - 'good morning!!!', - 'morning!! don\'t forget to brush your teeth!! or else!!', - ':) hi all', - 'it is currently: Morning!!!!! f', - `🌞 ☁️ ☁️ - ☁️ - ☁️ ☁️ - 🟩🟩🟩🟩🟩🟩🟩 - 🟫 🟫🟫🟫🟫🟫🟫Morning.......`, - 'https://tenor.com/view/good-morning-tweety-heart-gif-gif-26261249', - 'g', - 'hmmm....l... i don\'t think it\'s a very good morning today..... JUST KIDDING GREAT mornign ALL!!!!!', - ' GoodMorning!! ', - 'Good Morning!!!!fell out of bed and broke a bone!!', - 'Bed. Water Mel on. Swamp water. morning All !', - 'Error: good morning\n' + - ' at REPL7:1:7\n' + - ' at Script.runInThisContext (node:vm:129:12)\n' + - ' at REPLServer.defaultEval (node:repl:566:29)\n' + - ' at bound (node:domain:421:15)\n' + - ' at REPLServer.runBound [as eval] (node:domain:432:12)\n' + - ' at REPLServer.onLine (node:repl:893:10)\n' + - ' at REPLServer.emit (node:events:539:35)\n' + - ' at REPLServer.emit (node:domain:475:12)\n' + - ' at REPLServer.Interface._onLine (node:readline:487:10)\n' + - ' at REPLServer.Interface._line (node:readline:864:8)', - '```\ngood morning...\n```', - 'hello everyone! good morning :)', - ] - }, - evening: { - hour: 20, - randomizeMinute: true, - messages: [ - 'good night!!!!!!', - 'nini all', - 'goob night', - 'goo night .....', - 'sleep well everyone!!!!!', - 'good night !! dont let the bugs bite', - 'mmm....... goo night all', - 'night night!!!!', - 'have a good rest all!!! i will see you all tommorow :)', - 'a new day awaits tommorow!!!! head to bed!!!!!', - 'it\'s Getting Late... head To Bed....!', - 'good night!! be sure to brush your teeth before u go to sleep :)!', - 'mmmm... Sleepy.... bed bed time...!!', - 'good night everyone!! besure to prepare the [', - 'a good day awaits you all tommorow... so Go Hit The Bed and Get Some Rest!!!!', - 'sweet dreams everyone!!!! i\'m going to be dreaming of cheese..', - 'good night all!!', - 'bed.. comfy cosy!!!! sleepy time', - 'good night everyone....... hope u all stay warm and safe this night!!', - 'it\'s Bed Time!!! time for sleepy time', - 'https://tenor.com/view/night-goodnight-my-love-sweet-gif-26175663', - 'going bedbed now... good night all', - 'sweet dreams everyone!!!! i\'m going to be dreaming of ( )..', - 'Night Time....... go sleeby!!! don\'t forget your alarm!!!', - 'may ur dreams be as pleasant as uhm.... cheesecake ..... good night', - 'too sleepy for witty messages... Bed time ...', - ' Good Night!! ', - ] - }, - sex: { - hour: 17, - randomizeMinute: false, - messages: [ - 'https://cdn.discordapp.com/attachments/838764449899872356/1088024173051387974/sex_time.png', - 'sex. NO sleeping' - ], - messagesPrefix: '<@&1088023310882836500>' - } -}; - -function getNextAnnouncementTime(type: AnnouncementType) { - return getNextTime(type.hour, type.randomizeMinute); -} - -function getNextTime(hour: number, randomMinute = true) { - const now = new Date(); - const next = new Date(); - if (now.getUTCHours() >= hour) { - next.setTime(next.getTime() + 1000 * 60 * 60 * 24); - } - next.setUTCHours(hour); - if (randomMinute) { - next.setUTCMinutes(Math.floor(Math.random() * 60)); - next.setUTCSeconds(Math.floor(Math.random() * 60)); - } else { - next.setUTCMinutes(0); - next.setUTCSeconds(0); - } - - return next.getTime(); -} - -let next: Record = {}; -export let subscriptions: Record = {}; - -function saveNext() { - fs.writeFileSync('./next.json', JSON.stringify(next)); -} -function saveSubscriptions() { - fs.writeFileSync('./subscriptions.json', JSON.stringify(subscriptions)); -} - -if (fs.existsSync('./next.json')) { - next = JSON.parse(fs.readFileSync('./next.json', 'utf8')); -} -for (const k of Object.keys(timeAnnouncements)) { - if (!next[k]) next[k] = getNextAnnouncementTime(timeAnnouncements[k]); -} -saveNext(); - -if (fs.existsSync('./subscriptions.json')) { - subscriptions = JSON.parse(fs.readFileSync('./subscriptions.json', 'utf8')); -} -saveSubscriptions(); +loadNext(); +loadSubscriptions(); const bot = new Discord.Client({ intents: [ @@ -163,33 +21,9 @@ const bot = new Discord.Client({ bot.on('ready', async () => { initializeCounter(); - setInterval(() => { - if (disableDaytimeAnnouncements) return; - - const current = new Date().getTime(); - - // console.log(current, next.morning, next.night); - - for (const k of Object.keys(timeAnnouncements)) { - if (next[k] && current > next[k]) { - const announcement = timeAnnouncements[k]; - next[k] = getNextAnnouncementTime(announcement); - saveNext(); - - if (subscriptions[k]) { - for (const channelID of subscriptions[k]) { - bot.channels.fetch(channelID, {allowUnknownGuild: true}) - .then(c => - (c as Discord.TextChannel).send( - `${announcement.messagesPrefix ? announcement.messagesPrefix : ''} ${announcement.messages[Math.floor(Math.random() * announcement.messages.length)]}` - ) - ) - .catch(err => `failed to send ${k} announcement to ${channelID}: ${err}`); - } - } - } - } - }, 1000); + if (!disableDaytimeAnnouncements) { + initializeAnnouncements(bot); + } bot.commands = new Discord.Collection(); const cmdFiles = fs.readdirSync(path.join(__dirname, './commands')).filter((file) => file.endsWith('.js')); diff --git a/src/lib/subscriptions.ts b/src/lib/subscriptions.ts new file mode 100644 index 0000000..e6e9a84 --- /dev/null +++ b/src/lib/subscriptions.ts @@ -0,0 +1,179 @@ +import { Client, TextChannel } from 'discord.js'; +import * as fs from 'fs'; + +interface AnnouncementType { + hour: number; + randomizeMinute: boolean; + messages: string[], + messagesPrefix?: string +} + +export const timeAnnouncements: Record = { + morning: { + hour: 7, + randomizeMinute: true, + messages: [ + 'goob morning', + 'gm besties', + 'morning !!!', + 'it is Morning', + 'gm', + 'goom morbning', + 'goo morning', + 'hiiiii besties ........', + 'good morning!!!!', + 'awake, but at what cost ... ehehe hi all', + 'morning all!!!', + 'rise and shine!! new day!!', + 'mmm... morning', + 'morning to all of my slime fellows!!!! oh and the rest of you, uhm.. good morning i guess πŸ™„', + 'gm gamers... rise and grind', + 'good morning!!!', + 'morning!! don\'t forget to brush your teeth!! or else!!', + ':) hi all', + 'it is currently: Morning!!!!! f', + `🌞 ☁️ ☁️ + ☁️ + ☁️ ☁️ + 🟩🟩🟩🟩🟩🟩🟩 + 🟫 🟫🟫🟫🟫🟫🟫Morning.......`, + 'https://tenor.com/view/good-morning-tweety-heart-gif-gif-26261249', + 'g', + 'hmmm....l... i don\'t think it\'s a very good morning today..... JUST KIDDING GREAT mornign ALL!!!!!', + ' GoodMorning!! ', + 'Good Morning!!!!fell out of bed and broke a bone!!', + 'Bed. Water Mel on. Swamp water. morning All !', + 'Error: good morning\n' + + ' at REPL7:1:7\n' + + ' at Script.runInThisContext (node:vm:129:12)\n' + + ' at REPLServer.defaultEval (node:repl:566:29)\n' + + ' at bound (node:domain:421:15)\n' + + ' at REPLServer.runBound [as eval] (node:domain:432:12)\n' + + ' at REPLServer.onLine (node:repl:893:10)\n' + + ' at REPLServer.emit (node:events:539:35)\n' + + ' at REPLServer.emit (node:domain:475:12)\n' + + ' at REPLServer.Interface._onLine (node:readline:487:10)\n' + + ' at REPLServer.Interface._line (node:readline:864:8)', + '```\ngood morning...\n```', + 'hello everyone! good morning :)', + ] + }, + evening: { + hour: 20, + randomizeMinute: true, + messages: [ + 'good night!!!!!!', + 'nini all', + 'goob night', + 'goo night .....', + 'sleep well everyone!!!!!', + 'good night !! dont let the bugs bite', + 'mmm....... goo night all', + 'night night!!!!', + 'have a good rest all!!! i will see you all tommorow :)', + 'a new day awaits tommorow!!!! head to bed!!!!!', + 'it\'s Getting Late... head To Bed....!', + 'good night!! be sure to brush your teeth before u go to sleep :)!', + 'mmmm... Sleepy.... bed bed time...!!', + 'good night everyone!! besure to prepare the [', + 'a good day awaits you all tommorow... so Go Hit The Bed and Get Some Rest!!!!', + 'sweet dreams everyone!!!! i\'m going to be dreaming of cheese..', + 'good night all!!', + 'bed.. comfy cosy!!!! sleepy time', + 'good night everyone....... hope u all stay warm and safe this night!!', + 'it\'s Bed Time!!! time for sleepy time', + 'https://tenor.com/view/night-goodnight-my-love-sweet-gif-26175663', + 'going bedbed now... good night all', + 'sweet dreams everyone!!!! i\'m going to be dreaming of ( )..', + 'Night Time....... go sleeby!!! don\'t forget your alarm!!!', + 'may ur dreams be as pleasant as uhm.... cheesecake ..... good night', + 'too sleepy for witty messages... Bed time ...', + ' Good Night!! ', + ] + }, + sex: { + hour: 17, + randomizeMinute: false, + messages: [ + 'https://cdn.discordapp.com/attachments/838764449899872356/1088024173051387974/sex_time.png', + 'sex. NO sleeping' + ], + messagesPrefix: '<@&1088023310882836500>' + } +}; + +export function getNextAnnouncementTime(type: AnnouncementType) { + return getNextTime(type.hour, type.randomizeMinute); +} + +function getNextTime(hour: number, randomMinute = true) { + const now = new Date(); + const next = new Date(); + if (now.getUTCHours() >= hour) { + next.setTime(next.getTime() + 1000 * 60 * 60 * 24); + } + next.setUTCHours(hour); + if (randomMinute) { + next.setUTCMinutes(Math.floor(Math.random() * 60)); + next.setUTCSeconds(Math.floor(Math.random() * 60)); + } else { + next.setUTCMinutes(0); + next.setUTCSeconds(0); + } + + return next.getTime(); +} + +export let next: Record = {}; +export let subscriptions: Record = {}; + +export function saveNext() { + fs.writeFileSync('./next.json', JSON.stringify(next)); +} +export function saveSubscriptions() { + fs.writeFileSync('./subscriptions.json', JSON.stringify(subscriptions)); +} + +export function loadNext() { + if (fs.existsSync('./next.json')) { + next = JSON.parse(fs.readFileSync('./next.json', 'utf8')); + } + for (const k of Object.keys(timeAnnouncements)) { + if (!next[k]) next[k] = getNextAnnouncementTime(timeAnnouncements[k]); + } + saveNext(); +} +export function loadSubscriptions() { + if (fs.existsSync('./subscriptions.json')) { + subscriptions = JSON.parse(fs.readFileSync('./subscriptions.json', 'utf8')); + } + saveSubscriptions(); +} + +export function initializeAnnouncements(bot: Client) { + setInterval(() => { + const current = new Date().getTime(); + + // console.log(current, next.morning, next.night); + + for (const k of Object.keys(timeAnnouncements)) { + if (next[k] && current > next[k]) { + const announcement = timeAnnouncements[k]; + next[k] = getNextAnnouncementTime(announcement); + saveNext(); + + if (subscriptions[k]) { + for (const channelID of subscriptions[k]) { + bot.channels.fetch(channelID, {allowUnknownGuild: true}) + .then(c => + (c as TextChannel).send( + `${announcement.messagesPrefix ? announcement.messagesPrefix : ''} ${announcement.messages[Math.floor(Math.random() * announcement.messages.length)]}` + ) + ) + .catch(err => `failed to send ${k} announcement to ${channelID}: ${err}`); + } + } + } + } + }, 1000); +} \ No newline at end of file