I changed my mind, we're going back to promises

Require command invoke methods to return a promise
This commit is contained in:
benji7425 2017-09-24 22:46:58 +01:00
parent 60efeeebe8
commit 5b2916bf77
8 changed files with 45 additions and 49 deletions

View File

@ -1,20 +0,0 @@
{
"addFeed": {
"command": "add-feed",
"description": "Add an RSS feed to be posted in a channel, with an optional role to tag",
"syntax": "add-feed <url> <#channel> [@role]",
"admin": true
},
"removeFeed": {
"command": "remove-feed",
"description": "Remove an RSS feed by it's ID",
"syntax": "remove-feed <id>",
"admin": true
},
"viewFeeds": {
"command": "view-feeds",
"description": "View a list of configured feeds and their associated details",
"syntax": "view-feed",
"admin": true
}
}

View File

@ -29,20 +29,22 @@ function invoke({ message, params, guildData, client }) {
maxCacheSize: Config.maxCacheSize maxCacheSize: Config.maxCacheSize
}); });
//ask the user if they're happy with the details they set up, save if yes, don't if no return new Promise((resolve, reject) => {
Core.util.ask(client, message.channel, message.member, "Are you happy with this (yes/no)?\n" + feedData.toString()) //ask the user if they're happy with the details they set up, save if yes, don't if no
.then(responseMessage => { Core.util.ask(client, message.channel, message.member, "Are you happy with this (yes/no)?\n" + feedData.toString())
.then(responseMessage => {
//if they responded yes, save the feed and let them know, else tell them to start again //if they responded yes, save the feed and let them know, else tell them to start again
if (responseMessage.content.toLowerCase() === "yes") { if (responseMessage.content.toLowerCase() === "yes") {
if (!guildData) if (!guildData)
guildData = new GuildData({ id: message.guild.id, feeds: [] }); guildData = new GuildData({ id: message.guild.id, feeds: [] });
guildData.feeds.push(feedData); guildData.feeds.push(feedData);
guildData.cachePastPostedLinks(message.guild) guildData.cachePastPostedLinks(message.guild)
.then(() => message.reply("Your new feed has been saved!")); .then(() => resolve("Your new feed has been saved!"));
} }
else else
message.reply("Your feed has not been saved, please add it again with the correct details"); reject("Your feed has not been saved, please add it again with the correct details");
}); });
});
} }

View File

@ -11,8 +11,8 @@ module.exports = new Core.Command({
function invoke({ message, params, guildData, client }) { function invoke({ message, params, guildData, client }) {
const idx = guildData.feeds.findIndex(feed => feed.id === params[2]); const idx = guildData.feeds.findIndex(feed => feed.id === params[2]);
if (!Number.isInteger(idx)) if (!Number.isInteger(idx))
message.reply("Can't find feed with id " + params[2]); return Promise.reject("Can't find feed with id " + params[2]);
guildData.feeds.splice(idx, 1); guildData.feeds.splice(idx, 1);
message.reply("Feed removed!"); return Promise.resolve("Feed removed!");
} }

View File

@ -10,7 +10,7 @@ module.exports = new Core.Command({
function invoke({ message, params, guildData, client }) { function invoke({ message, params, guildData, client }) {
if (!guildData) if (!guildData)
message.reply("Guild not setup"); return Promise.reject("Guild not setup");
message.reply(guildData.feeds.map(f => f.toString()).join("\n")); return Promise.resolve(guildData.feeds.map(f => f.toString()).join("\n"));
} }

View File

@ -10,12 +10,12 @@ const token = require("../" + process.argv[2]).token,
const client = new Core.Client(token, dataFile, __dirname + "/commands", GuildData); const client = new Core.Client(token, dataFile, __dirname + "/commands", GuildData);
client.on("beforeLogin", () => { client.on("beforeLogin", () => {
setInterval(() => checkFeedsInGuilds(client.guilds, client.guildsData), Config.feedCheckIntervalSec * 1000); setInterval(() => checkFeedsInGuilds(client.guildsData), Config.feedCheckIntervalSec * 1000);
}); });
client.on("ready", () => { client.on("ready", () => {
parseLinksInGuilds(client.guilds, client.guildsData) parseLinksInGuilds(client.guilds, client.guildsData)
.then(() => checkFeedsInGuilds(client.guilds, client.guildsData)); .then(() => checkFeedsInGuilds(client.guildsData));
}); });
client.on("message", message => { client.on("message", message => {
@ -30,8 +30,8 @@ client.on("message", message => {
client.bootstrap(); client.bootstrap();
//INTERNAL FUNCTIONS// //INTERNAL FUNCTIONS//
function checkFeedsInGuilds(guilds, guildsData) { function checkFeedsInGuilds(guildsData) {
Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds)); Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(client.guilds));
} }
function parseLinksInGuilds(guilds, guildsData) { function parseLinksInGuilds(guilds, guildsData) {

View File

@ -1,9 +1,10 @@
const DiscordUtil = require("../../discord-bot-core").util; const DiscordUtil = require("../../discord-bot-core").util;
const Core = require("../../discord-bot-core");
const FeedData = require("./feed-data.js"); const FeedData = require("./feed-data.js");
module.exports = class GuildData { module.exports = class GuildData extends Core.BaseGuildData{
constructor({ id, feeds }) { constructor({ id, feeds }) {
this.id = id; super(id);
this.feeds = (feeds || []).map(feed => new FeedData(feed)); this.feeds = (feeds || []).map(feed => new FeedData(feed));
} }

View File

@ -47,8 +47,11 @@ module.exports = class Client extends Discord.Client {
} }
onMessage(message) { onMessage(message) {
if (message.channel.type === "text" && message.member) if (message.channel.type === "text" && message.member) {
HandleMessage(this, message, this.commands, this.guildsData[message.guild.id] || new this.guildDataModel(message.guild.id)); if(!this.guildsData[message.guild.id])
this.guildsData[message.guild.id] = new this.guildDataModel(message.guild.id);
HandleMessage(this, message, this.commands, this.guildsData[message.guild.id]);
}
} }
onDebug(info) { onDebug(info) {

View File

@ -1,5 +1,6 @@
// @ts-ignore // @ts-ignore
const ParentPackageJSON = require("../package.json"); const ParentPackageJSON = require("../package.json");
const CoreUtil = require("./Util.js");
/**@param param*/ /**@param param*/
function handleMessage(client, message, commands, guildData) { function handleMessage(client, message, commands, guildData) {
@ -16,15 +17,24 @@ function handleMessage(client, message, commands, guildData) {
handleInternalCommand(message, split); handleInternalCommand(message, split);
else if (params.length < command.expectedParamCount) else if (params.length < command.expectedParamCount)
message.reply(`Incorrect syntax!\n**Expected:** *${botName} ${command.syntax}*\n**Need help?** *${botName} help*`); message.reply(`Incorrect syntax!\n**Expected:** *${botName} ${command.syntax}*\n**Need help?** *${botName} help*`);
else if(isMemberAdmin || !command.admin) else if (isMemberAdmin || !command.admin)
command.invoke({ message, params, guildData, client }); command.invoke({ message, params, guildData, client })
.then(response => {
client.writeFile();
if (response)
message.reply(response);
})
.catch(err => {
if (err)
message.reply(err);
});
} }
/**@param param*/ /**@param param*/
function handleInternalCommand(message, split) { function handleInternalCommand(message, split) {
if (split[1].toLowerCase() === "version") if (split[1].toLowerCase() === "version")
message.reply(`${ParentPackageJSON.name} v${ParentPackageJSON.version}`); message.reply(`${ParentPackageJSON.name} v${ParentPackageJSON.version}`);
else if(split[1].toLowerCase() === "help") else if (split[1].toLowerCase() === "help")
message.reply(createHelpEmbed()); message.reply(createHelpEmbed());
} }