From 12d9c247120e566f6398c0c0d21ea7d18201e652 Mon Sep 17 00:00:00 2001 From: Malachi Date: Fri, 4 Jun 2021 22:21:59 -0600 Subject: [PATCH] lol --- commands/util/dpod.js | 34 ++++++------------- dpod.json | 1 + util/dpod.js | 78 +++++++++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 57 deletions(-) create mode 100644 dpod.json diff --git a/commands/util/dpod.js b/commands/util/dpod.js index 8d20b80..da65a37 100644 --- a/commands/util/dpod.js +++ b/commands/util/dpod.js @@ -2,8 +2,6 @@ const { Command } = require('discord.js-commando') const config = require('$root/config.json') const mission = require('$root/mission.json') const count = require('$util/count') -const dpod = require('$root/dpod.json') -const fs = require('fs') module.exports = class Invite extends Command { constructor(client) { @@ -35,9 +33,9 @@ module.exports = class Invite extends Command { } async run(message, { channel, action }) { - count.cmdRun(this.name, this.group, message) - let guildChannel = dpod.find(channel => channel.guild === message.guild.id) - let dpodInfo = dpod + count.cmdCount++ + + let guildChannel = this.client.provider.get(message.guild, 'dpod') function msg(description) { message.embed({ @@ -57,33 +55,21 @@ module.exports = class Invite extends Command { } if (!guildChannel) { - dpodInfo.push({ guild: message.guild.id, channel: channel.id }) - - let json = JSON.stringify(dpodInfo) - fs.writeFile('dpod.json', json, function (err) { - if (err) throw err - }) + this.client.provider.set(message.guild, 'dpod', channel.id) return msg(`Set Your DPOD Channel to <#${channel.id}>\nTo reset it use: ${message.anyUsage('dpod reset')}`) } msg( - `Your already have a DPOD\nThe Current DPOD Channel is <#${ - guildChannel.channel - }>\nTo reset it use:\n${message.anyUsage('dpod reset')}` + `Your already have a DPOD\nThe Current DPOD Channel is <#${guildChannel}>\nTo reset it use:\n${message.anyUsage( + 'dpod reset' + )}` ) } if (action == 'reset') { if (guildChannel) { - let dpodInfoFiltered = await dpodInfo.filter(array => { - array.guild != message.guild.id - }) - - let json = JSON.stringify(dpodInfoFiltered) - fs.writeFile('dpod.json', json, function (err) { - if (err) throw err - }) + this.client.provider.remove(message.guild, 'dpod') msg(`The DPOD Channel has been reset\nTo set a new one use: ${message.anyUsage('dpod set ')}`) return @@ -93,9 +79,9 @@ module.exports = class Invite extends Command { } if (action == 'info') { - if (dpod.find(channel => channel.guild === message.guild.id)) { + if (this.client.provider.get(message.guild, 'dpod', false)) { msg( - `The Current DPOD Channel is <#${guildChannel.channel}>\nTo reset it use:\n${message.anyUsage( + `The Current DPOD Channel is <#${guildChannel}>\nTo reset it use:\n${message.anyUsage( 'dpod reset' )}\nDPOD runs every day at [12PM UTC± 0](https://www.google.com/search?q=12pm+utc+time+zone+converter)` ) diff --git a/dpod.json b/dpod.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/dpod.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/util/dpod.js b/util/dpod.js index 17a7219..e6039b5 100644 --- a/util/dpod.js +++ b/util/dpod.js @@ -2,10 +2,49 @@ const axios = require('axios') const schedule = require('node-schedule') const chalk = require('chalk') const config = require('$root/config.json') -const dpod = require('$root/dpod.json') -let img = '' +const count = require('$util/count') + let delay = 1000 -let i + +const dpod = async function () { + let img = '' + await axios + .get(`https://api.nasa.gov/planetary/apod?api_key=${config.api_key}`) + .then(async res => { + if (res.data.hdurl) { + img = res.data.hdurl + } else { + img = res.data.url + } + + let embed = { + title: res.data.title, + url: img, + description: res.data.explanation, + color: config.embed_color, + image: { url: img }, + timestamp: res.data.date, + footer: { text: `Photo Credit: ${res.data.copyright}` } + } + + let channels = [] + this.guilds.cache.each(guild => { + if (this.provider.get(guild, 'dpod', false)) channels.push(this.provider.get(guild, 'dpod')) + }) + + for (let channel of channels) { + this.channels.cache + .get(channel) + .send({ embed }) + .catch(() => {}) + + await count.sleep(delay) + } + }) + .catch(function (error) { + console.log(error.stack) + }) +} module.exports = { async execute(client) { @@ -17,34 +56,7 @@ module.exports = { rule.tz = 'Etc/UTC' /*eslint-disable */ - const job = schedule.scheduleJob(rule, async function () { - /*eslint-enable */ - await axios - .get(`https://api.nasa.gov/planetary/apod?api_key=${config.api_key}`) - .then(async res => { - if (res.data.hdurl) { - img = res.data.hdurl - } else { - img = res.data.url - } - - let embed = { - title: res.data.title, - url: img, - description: res.data.explanation, - color: config.embed_color, - image: { url: img }, - timestamp: res.data.date, - footer: { text: `Photo Credit: ${res.data.copyright}` } - } - - for (i = 0; i < dpod.length; i++) { - client.channels.cache.get(dpod[i].channel).send({ embed }) - } - }) - .catch(function (error) { - console.log(error.stack) - }) - }) - } + const job = schedule.scheduleJob(rule, dpod.bind(client)) + }, + dpod }