Browse Source

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

Require command invoke methods to return a promise
master
benji7425 4 years ago
parent
commit
5b2916bf77
  1. 20
      app/commands.json
  2. 34
      app/commands/add-feed.js
  3. 4
      app/commands/remove-feed.js
  4. 4
      app/commands/view-feeds.js
  5. 8
      app/index.js
  6. 5
      app/models/guild-data.js
  7. 7
      discord-bot-core/Client.js
  8. 16
      discord-bot-core/HandleMessage.js

20
app/commands.json

@ -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
}
}

34
app/commands/add-feed.js

@ -29,20 +29,22 @@ function invoke({ message, params, guildData, client }) {
maxCacheSize: Config.maxCacheSize
});
//ask the user if they're happy with the details they set up, save if yes, don't if no
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 (responseMessage.content.toLowerCase() === "yes") {
if (!guildData)
guildData = new GuildData({ id: message.guild.id, feeds: [] });
guildData.feeds.push(feedData);
guildData.cachePastPostedLinks(message.guild)
.then(() => message.reply("Your new feed has been saved!"));
}
else
message.reply("Your feed has not been saved, please add it again with the correct details");
});
return new Promise((resolve, reject) => {
//ask the user if they're happy with the details they set up, save if yes, don't if no
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 (responseMessage.content.toLowerCase() === "yes") {
if (!guildData)
guildData = new GuildData({ id: message.guild.id, feeds: [] });
guildData.feeds.push(feedData);
guildData.cachePastPostedLinks(message.guild)
.then(() => resolve("Your new feed has been saved!"));
}
else
reject("Your feed has not been saved, please add it again with the correct details");
});
});
}

4
app/commands/remove-feed.js

@ -11,8 +11,8 @@ module.exports = new Core.Command({
function invoke({ message, params, guildData, client }) {
const idx = guildData.feeds.findIndex(feed => feed.id === params[2]);
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);
message.reply("Feed removed!");
return Promise.resolve("Feed removed!");
}

4
app/commands/view-feeds.js

@ -10,7 +10,7 @@ module.exports = new Core.Command({
function invoke({ message, params, guildData, client }) {
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"));
}

8
app/index.js

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

5
app/models/guild-data.js

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

7
discord-bot-core/Client.js

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

16
discord-bot-core/HandleMessage.js

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

Loading…
Cancel
Save