diff --git a/.env.example b/.env.example deleted file mode 100644 index dace595..0000000 --- a/.env.example +++ /dev/null @@ -1,15 +0,0 @@ -# This is an example environment variable file. -# If you want to use this bot for your own server, you need to change the values in here. - -# A token from the Discord API to let the bot log in and send messages. -BOT_TOKEN='token' - -# Characters the bot should look for when a user sends a message. The bot should only respond to -# messages if they start with this prefix. -COMMAND_PREFIX='!!' - -# A list of people, separated by spaces who have full control over this bot. -BOT_OWNERS='' - -# The name of the database that this bot should use when storing settings for a Discord server. -DATABASE_NAME='serversettings.db' \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 298a36a..92b01e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -185,6 +185,11 @@ "underscore": "~1.4.4" } }, + "bluebird": { + "version": "3.5.5", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz", + "integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==" + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -313,9 +318,9 @@ } }, "combined-stream": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.7.tgz", - "integrity": "sha512-brWl9y6vOB1xYPZcpZde3N9zDByXTosAeMDo4p1wzo6UMOX4vumB+TP1RZ76sfE6Md68Q0NJSrE/gbezd4Ul+w==", + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", "requires": { "delayed-stream": "~1.0.0" } @@ -1035,6 +1040,11 @@ "colornames": "^1.1.1" } }, + "later": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/later/-/later-1.2.0.tgz", + "integrity": "sha1-8s9sTdeVbdL1IK3wMpg26YdrrQ8=" + }, "levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", @@ -1084,16 +1094,16 @@ "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" }, "mime-db": { - "version": "1.37.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.37.0.tgz", - "integrity": "sha512-R3C4db6bgQhlIhPU48fUtdVmKnflq+hRdad7IyKhtFj06VPNVdk2RhiYL3UjQIlso8L+YxAtFkobT0VK+S/ybg==" + "version": "1.40.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", + "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" }, "mime-types": { - "version": "2.1.21", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.21.tgz", - "integrity": "sha512-3iL6DbwpyLzjR3xHSFNFeb9Nz/M8WDkX33t1GFQnFOllWk8pOrh/LSrB5OXlnlW5P9LH73X6loW/eogc+F5lJg==", + "version": "2.1.24", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", + "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", "requires": { - "mime-db": "~1.37.0" + "mime-db": "1.40.0" } }, "mimic-fn": { @@ -1374,9 +1384,9 @@ "dev": true }, "psl": { - "version": "1.1.29", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.29.tgz", - "integrity": "sha512-AeUmQ0oLN02flVHXWh9sSJF7mcdFq0ppid/JkErufc3hGIV/AMa8Fo9VgDo/cT2jFdOWoFvHp90qqBH54W+gjQ==" + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.3.1.tgz", + "integrity": "sha512-2KLd5fKOdAfShtY2d/8XDWVRnmp3zp40Qt6ge2zBPFARLXOGUf2fHD5eg+TV/5oxBtQKVhjUaKFsAaE4HnwfSA==" }, "punycode": { "version": "2.1.1", @@ -1453,6 +1463,25 @@ "uuid": "^3.3.2" } }, + "request-promise": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.4.tgz", + "integrity": "sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==", + "requires": { + "bluebird": "^3.5.0", + "request-promise-core": "1.1.2", + "stealthy-require": "^1.1.1", + "tough-cookie": "^2.3.3" + } + }, + "request-promise-core": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.2.tgz", + "integrity": "sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag==", + "requires": { + "lodash": "^4.17.11" + } + }, "require-all": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/require-all/-/require-all-2.2.0.tgz", @@ -1617,9 +1646,9 @@ } }, "sshpk": { - "version": "1.15.2", - "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.15.2.tgz", - "integrity": "sha512-Ra/OXQtuh0/enyl4ETZAfTaeksa6BXks5ZcjpSUNrjBr0DvrJKX+1fsKDPpT9TBXgHAFsa4510aNVgI8g/+SzA==", + "version": "1.16.1", + "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", + "integrity": "sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg==", "requires": { "asn1": "~0.2.3", "assert-plus": "^1.0.0", @@ -1644,6 +1673,11 @@ "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" }, + "stealthy-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", + "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" + }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", @@ -1837,9 +1871,9 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, "uuid": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", - "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz", + "integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==" }, "verror": { "version": "1.10.0", diff --git a/package.json b/package.json index d4a0abb..6fbcdf3 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,11 @@ "discord.js": "^11.4.2", "discord.js-commando": "^0.10.0", "dotenv": "^6.1.0", + "later": "^1.2.0", "moment": "^2.22.2", "nedb": "^1.8.0", + "request": "^2.88.0", + "request-promise": "^4.2.4", "sqlite": "^3.0.0", "sqlite3": "^4.0.4", "winston": "^3.1.0", diff --git a/src/commands/admin/deployreddit.js b/src/commands/admin/deployreddit.js new file mode 100644 index 0000000..5776234 --- /dev/null +++ b/src/commands/admin/deployreddit.js @@ -0,0 +1,366 @@ +const Commando = require('discord.js-commando'); +const request = require('request-promise'); +const later = require('later'); + +/** + * Sends an embedded message containing the most popular image on a given subreddit at + * a given time for a selected guild channel, daily. Offers C.R.U.D functionality for + * users to be able to manipulate their deployed instances. + */ +class DeployReddit extends Commando.Command { + constructor(client) { + super(client, { + name: 'dreddit', + description: 'Deploys the top img in any subreddit every 24hrs', + guildOnly: true, + group: 'admin', + memberName: 'deployreddit', + }); + } + + hasPermission(message) { + // Use officer role. + const isUserAnOfficer = message.member.roles.find((role) => role.name === 'Club Officers'); + return !!isUserAnOfficer || message.member.permissions.has('ADMINISTRATOR'); + } + + async run(message, args) { + const messageArguments = args.split(' '); + + switch (messageArguments[0]) { + case '--stop': + if (messageArguments.length == 2) { + const instanceKey = 'guild.reddit.instances'; + const channelID = messageArguments[1]; + deleteRedditInstance(message, instanceKey, channelID); + } else { + message.reply(`!!dreddit --stop \nStops the execution of the reddit deployment in the specified channel.`); + } + return; + case '--edit': + if (messageArguments.length == 3) { + const instanceKey = 'guild.reddit.instances'; + const channelID = messageArguments[1]; + const link = refactorLink(messageArguments[2]); + if (await validatedLink(message, link)) { + updateRedditFromKey(message, instanceKey, channelID, link); + } + } else { + message.reply(`!!dreddit --edit \nChanges the given URL of a channel.`); + } + return; + case '--status': + if (messageArguments.length == 2) { + const instanceKey = 'guild.reddit.instances'; + const channelID = refactorID(message, messageArguments[1]); + getValueOfReddit(message, instanceKey, channelID); + } else { + // debugging - remove following comments to enable debugging + // const instanceKey = 'guild.reddit.instances'; + // const instances = message.guild.settings.get(instanceKey, null); + // console.log(instances); + + message.reply(`!!dreddit --status \nGives information about the given channel with respect to reddit deployment.`); + } + return; + case '--removeall': + // debugging - remove following comments to enable debugging + // const instanceKey = 'guild.reddit.instances'; + // const instances = message.guild.settings.get(instanceKey, null); + // console.log(instances); + + deleteAllInstances(message); + return; + default: + if (messageArguments.length == 2) { + const instanceKey = 'guild.reddit.instances'; + let channelID = messageArguments[0]; + const link = refactorLink(messageArguments[1]); + + if (await validatedLink(message, link) != true) return; + + channelID = refactorID(message, channelID); + + if (doesChannelExist(message, instanceKey, channelID)) return; + + if (!isFirstInstance(message, instanceKey, channelID, link)) { + setRedditFromKey(message, instanceKey, channelID, link); + initializeInstance(message, link, channelID); + console.log(`${channelID} : ${link} has been added to Reddit active instances.`); + } + return; + } else { + message.reply(`!!dreddit \nAdds a reddit instance to a given channel.`); + } + return; + } + } +} + +/** + * Launches a Reddit instance upon first creation into + * specified channel + */ +function initializeInstance(message, redditURL, channelID) { + const DAILY_POST_TIME = 'at 08:00am'; + // const TESTING_POST_TIME = 'every 20 seconds'; + const schedule = later.parse.text(DAILY_POST_TIME); + later.date.localTime(); // relative time default is UTC + queryReddit(message, channelID); // one instant deployment + later.setInterval(function() { + queryReddit(message, channelID); + }, schedule); +} + +/** + * @return undefined if channelID not found or DB not instantiated + */ +function deleteRedditInstance(message, instanceKey, channelID) { + channelID = refactorID(message, channelID); + + let instances = message.guild.settings.get(instanceKey, null); + + if (instances == null) { + message.reply(`${instanceKey} was not found.\nTry creating a new instance using "!!dreddit "`); + return undefined; + } + + instances = JSON.parse(instances); + for (index in instances.instances) { + if (instances.instances[index][channelID] !== undefined) { + instances.instances.splice(index, 1); + const newInstances = JSON.stringify(instances); + if (typeof newInstances === 'string') { + message.guild.settings.set(instanceKey, newInstances); + message.reply(`${channelID} was removed from this guild.`); + return; + } + break; + } + } + + message.reply(`${channelID} was not in active instances.`); + return undefined; +} + +/** + * @return stringified JSON of instances if update successful + */ +function updateRedditFromKey(message, instanceKey, channelID, redditURL) { + channelID = refactorID(message, channelID); + let instances = message.guild.settings.get(instanceKey, null); + instances = JSON.parse(instances); + for (index in instances.instances) { + if (instances.instances[index][channelID] !== undefined) { + instances.instances[index][channelID] = redditURL; + instances = JSON.stringify(instances); + if (typeof instances === 'string') { + console.log('altering old val'); + message.guild.settings.set(instanceKey, instances); + message.reply(`${instanceKey} was assigned ${redditURL}`); + return instances; + } + break; + } + } + + message.reply(`${channelID} was not in active instances.`); + return; +} + +function setRedditFromKey(message, instanceKey, channelID, redditURL) { + let instances = message.guild.settings.get(instanceKey, null); + instances = JSON.parse(instances); + instances.instances.push({[channelID]: redditURL}); + instances = JSON.stringify(instances); + message.guild.settings.set(instanceKey, instances); + + message.reply(`${channelID} was assigned ${redditURL}`); + return; +} + +/** + * @return undefined if DB not found, null if ID not found, instances string + */ +function getValueOfReddit(message, instanceKey, channelID) { + let instances = message.guild.settings.get(instanceKey, null); + if (instances == null) { + message.reply(`${instanceKey} was not found`); + return undefined; + } + instances = JSON.parse(instances); + for (key in instances.instances) { + if (instances.instances[key][channelID] !== undefined) { + instances = instances.instances[key][channelID]; + break; + } + } + + if (typeof instances !== 'string') { + message.reply(`The given ID for ${instanceKey} was not found`); + return null; + } else { + message.reply(`Value for ${channelID} is ${instances}`); + return instances; + } +} + +function doesDefaultInstanceExist(message, instanceKey, channelID) { + const instances = message.guild.settings.get(instanceKey, null); + return (instances == null) ? false : true; +} + +function deleteAllInstances(message) { + const instanceKey = 'guild.reddit.instances'; + message.guild.settings.remove(instanceKey, null); + console.log('All instances deleted.'); +} + +/** + * Queries Reddit and sends embedded image + */ +async function queryReddit(message, channelID) { + try { + let redditURL; + const instanceKey = 'guild.reddit.instances'; + let instances = message.guild.settings.get(instanceKey, null); + instances = JSON.parse(instances); + for (index in instances.instances) { + if (instances.instances[index][channelID] !== undefined) { + redditURL = instances.instances[index][channelID]; + } + } + + await request(redditURL, async (error, response, html) => { + if (!error && response.statusCode == 200) { + const jsonData = JSON.parse(html); + for (let index = 0; index < jsonData.data.dist; index++) { + if (jsonData.data.children[index].data.post_hint == 'image' + || linksToImage(jsonData.data.children[index].data.url)) { + const imageToEmbed = { + 'image': { + 'url': jsonData.data.children[index].data.url, + }, + }; + channelID = parseID(channelID); + const channel = await message.guild.channels.get(channelID); + channel.send({embed: imageToEmbed}).then(async function(reply) { + reply.channel.fetchMessage(reply.id).then(async function(messageRetrieved) { + await messageRetrieved.react('👍'); + await messageRetrieved.react('👎'); + }); + }); + return; + } + } + } + }); + console.log('Reddit post deployed :', new Date()); + } catch (e) { + return; + } +} + +const parseID = (channelID) => { + return channelID.substring(2); +}; + +const linksToImage = (link) => { + const IMG_EXTENSIONS = ['jpg', 'png', 'gif']; + const linkExtension = link.substr(link.length - 3); + if (IMG_EXTENSIONS.includes(linkExtension)) { + return true; + } + return false; +}; + +const refactorID = (message, channelID) => { + if (channelID === undefined) { + channelID = 'id' + message.channel.id; + } else { + channelID = 'id' + channelID; + } + return channelID; +}; + +const isFirstInstance = (message, instanceKey, channelID, redditURL) => { + if (doesDefaultInstanceExist(message, instanceKey, channelID) === false) { + let defaultInstanceObject = { + 'instances': [ + ], + }; + defaultInstanceObject.instances.push({[channelID]: redditURL}); + defaultInstanceObject = JSON.stringify(defaultInstanceObject); + message.guild.settings.set(instanceKey, defaultInstanceObject); + initializeInstance(message, redditURL, channelID); + message.reply(`First Reddit instance has been deployed!`); + return true; + } + return false; +}; + +const doesChannelExist = (message, instanceKey, channelID) => { + let keyValueExits = message.guild.settings.get(instanceKey, null); + keyValueExits = JSON.parse(keyValueExits); + if (keyValueExits !== null) { + for (const z in keyValueExits.instances) { + if (keyValueExits.instances[z][channelID] !== undefined) { + message.reply(`${channelID} was already found in active instances\nTry using "--edit" or "--stop`); + return true; + } + } + } + return false; +}; + +const refactorLink = (link) => { + if (link.search('reddit.com/') == -1) { + if (link.search('/') == 0) { + link = 'https://www.reddit.com' + link; + } else { + link = 'https://www.reddit.com/' + link; + } + } + const lastCharOfURL = link.charAt(link.length - 1); + switch (lastCharOfURL) { + case '/': + link += '.json'; + break; + default: + link += '/.json'; + break; + } + return link; +}; + +const validatedLink = async (message, link) => { + await request(link, (error, response, html) => { + let errorGiven; + let jsonData; + try { + jsonData = JSON.parse(html); + } catch (e) { + message.reply('Sorry, this link did not work.\nTry using a link like "r/programmerhumor" or "r/memes"'); + return false; + } + + try { + errorGiven = (jsonData.data.after == null) ? true : false; + } catch (e) { + message.reply('Sorry, this link did not work.\nTry using a link like "r/programmerhumor" or "r/memes"'); + return false; + } + + if (error) { + message.reply('Sorry, this link did not work.\nTry using a link like "r/programmerhumor" or "r/memes"'); + return false; + } else if (errorGiven == true) { + message.reply('Sorry, this link did not work.\nTry using a link like "r/programmerhumor" or "r/memes"'); + return false; + } + return; + }); + return true; +}; + +module.exports = DeployReddit; diff --git a/src/commands/admin/managesettings.js b/src/commands/admin/managesettings.js index a45a2b4..d52dac4 100644 --- a/src/commands/admin/managesettings.js +++ b/src/commands/admin/managesettings.js @@ -1,6 +1,6 @@ // @ts-check const Commando = require('discord.js-commando'); -const {settingsKeys, settingNames} = require('../../settings/SettingsProvider'); +const {settingsKeys, settingNames, redditNames, redditKeys} = require('../../settings/SettingsProvider'); const Discord = require('discord.js'); const Logger = require('../../logging/Logger'); @@ -50,6 +50,8 @@ class SettingsCommand extends Commando.Command { return listServerSettings(message, messageArguments); case '--listkeys': return listSettingKeys(message, messageArguments); + case '--listreddit': + return listRedditKeys(message, messageArguments); case '--remove': return removeServerSetting(message, messageArguments); } @@ -127,6 +129,9 @@ function setSettingFromKey(message, messageArguments) { * @return {Promise} */ function getValueOfSetting(message, messageArguments) { + if (!settingNames.includes(messageArguments[0])) { + return message.reply(`The key ${messageArguments[0]} does not exist.`); + } const settingValue = message.guild.settings.get(messageArguments[0], null); if (settingValue) { return message.reply(`Value for ${messageArguments[0]} is ${settingValue}`); @@ -147,4 +152,8 @@ function listSettingKeys(message, messageArguments) { return message.reply(settingNames.join('\n'), {code: true}); } +function listRedditKeys(message, messageArguments) { + return message.reply(redditNames.join('\n'), {code: true}); +} + module.exports = SettingsCommand; diff --git a/src/commands/admin/startupdeployreddit.js b/src/commands/admin/startupdeployreddit.js new file mode 100644 index 0000000..d9987c8 --- /dev/null +++ b/src/commands/admin/startupdeployreddit.js @@ -0,0 +1,93 @@ +const Discord = require('discord.js'); +const request = require('request-promise'); +const later = require('later'); + +/** + * Launches Reddit instances into every channel listed under a guild's + * active instances in the settings DB upon bot start up. + */ +async function initializeInstance(discordClient) { + if (discordClient !== undefined) { + const DAILY_POST_TIME = 'at 08:00am'; + // const TESTING_POST_TIME = 'every 20 seconds'; + const schedule = later.parse.text(DAILY_POST_TIME); + + later.date.localTime(); // time default is UTC + + for (const guildIndex in discordClient.guilds.keyArray()) { + guild = discordClient.guilds.keyArray()[guildIndex]; + let instances = discordClient.guilds.get(guild).settings.get('guild.reddit.instances'); + instances = JSON.parse(instances); + for (let index = 0; index < instances.instances.length; index++) { + for (x in instances.instances[index]) { + const channelID = x; + // await queryReddit(discordClient, redditURL, x); // debugging - instant reply + await later.setInterval(function () { + queryReddit(discordClient, channelID); + }, schedule); + } + } + } + } +} + +/** + * Queries Reddit and sends embedded image + */ +async function queryReddit(discordClient, channelID) { + try { + let redditURL; + const guild = discordClient.guilds.keyArray()[0]; + let instances = await discordClient.guilds.get(guild).settings.get('guild.reddit.instances'); + instances = await JSON.parse(instances); + for (index in instances.instances) { + if (instances.instances[index][channelID] !== undefined) { + redditURL = instances.instances[index][channelID]; + } + } + + await request(redditURL, (error, response, html) => { + if (!error && response.statusCode == 200) { + const jsonData = JSON.parse(html); + for (let index = 0; index < jsonData.data.dist; index++) { + if (jsonData.data.children[index].data.post_hint == 'image' + || linksToImage(jsonData.data.children[index].data.url)) { + if (discordClient !== null) { + const messageToEmbed = new Discord.RichEmbed(); + messageToEmbed.setImage(jsonData.data.children[index].data.url); + channelID = parseID(channelID); + // debugging - line below results in all posts on one channel + // const channel = discordClient.channels.get('619024772898095115'); + const channel = discordClient.channels.get(channelID); + channel.send(messageToEmbed).then(async function (reply) { + reply.channel.fetchMessage(reply.id).then(async function (messageRetrieved) { + await messageRetrieved.react('👍'); + await messageRetrieved.react('👎'); + }); + }); + return; + } + } + } + } + }); + console.log(`Reddit post made from ${channelID} :`, new Date()); + } catch (e) { + return; + } +} + +const parseID = (channelID) => { + return channelID.substring(2); +}; + +const linksToImage = (link) => { + const IMG_EXTENSIONS = ['jpg', 'png', 'gif']; + const linkExtension = link.substr(link.length - 3); + if (IMG_EXTENSIONS.includes(linkExtension)) { + return true; + } + return false; +}; + +module.exports = { initializeInstance: initializeInstance }; diff --git a/src/main.js b/src/main.js index 9fef6fb..4a47422 100644 --- a/src/main.js +++ b/src/main.js @@ -12,6 +12,7 @@ const loadEvents = require('./util/eventLoader'); // Import methods responsible for creating roles in servers as necessary. const createServerRoles = require('./events/createServerRoles').run; +const initializeInstance = require('./commands/admin/startupdeployreddit').initializeInstance; const discordClient = new Commando.CommandoClient({ owner: Settings.botOwners, @@ -59,10 +60,16 @@ discordClient.on('ready', async function() { }); /** - * Create server roles if necessary. Any roles that aren't in the server that are in settings + * Creates server roles if necessary. Any roles that aren't in the server that are in settings * will be created. */ createServerRoles(discordClient); + + /** + * Creates instances of all of the channel and URL pairsin reddit instances + * within the settings db + */ + initializeInstance(discordClient); }); discordClient.registry.registerGroups([ diff --git a/src/settings/SettingsProvider.js b/src/settings/SettingsProvider.js index b2bd427..0a48881 100644 --- a/src/settings/SettingsProvider.js +++ b/src/settings/SettingsProvider.js @@ -28,12 +28,18 @@ const settingKeys = { ROLE_ASSIGNMENT_MESSAGES: 'guild.roles.roleAssignmentMessages', }; +const redditKeys = { + REDDIT_INSTANCES: 'guild.reddit.instances', +}; + /** @type {string[]} */ const settingNames = Object.getOwnPropertyNames(settingKeys).map((key) => settingKeys[key]); - +const redditNames = Object.getOwnPropertyNames(redditKeys).map((key) => redditKeys[key]); module.exports = { setupSettingsProvider, settingsKeys: settingKeys, settingNames: settingNames, + redditKeys: redditKeys, + redditNames: redditNames, };