2017-09-14 15:09:43 +03:00
|
|
|
const Core = require("../discord-bot-core");
|
2017-09-24 23:31:30 +03:00
|
|
|
const GetUrls = require("get-urls");
|
2017-09-14 15:09:43 +03:00
|
|
|
const GuildData = require("./models/guild-data.js");
|
2017-09-24 23:31:30 +03:00
|
|
|
// @ts-ignore
|
2017-09-14 15:09:43 +03:00
|
|
|
const Config = require("./config.json");
|
|
|
|
|
2017-12-08 02:00:25 +02:00
|
|
|
const guildsIterator = (function* () {
|
|
|
|
while (true) {
|
|
|
|
if (client.guilds.size === 0)
|
|
|
|
yield null;
|
|
|
|
else
|
|
|
|
for (let i = 0; i < client.guilds.size; i++)
|
|
|
|
yield [...client.guilds.values()][i];
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2017-09-24 23:31:30 +03:00
|
|
|
const token = require("../" + process.argv[2]).token,
|
|
|
|
dataFile = process.argv[3];
|
2017-09-14 15:09:43 +03:00
|
|
|
|
2017-09-24 23:31:30 +03:00
|
|
|
const client = new Core.Client(token, dataFile, __dirname + "/commands", GuildData);
|
2017-09-14 15:09:43 +03:00
|
|
|
|
2017-12-08 02:00:25 +02:00
|
|
|
client.on("beforeLogin", () =>
|
|
|
|
setInterval(doGuildIteration, Config.feedCheckInterval));
|
2017-09-14 15:09:43 +03:00
|
|
|
|
2017-09-24 23:39:34 +03:00
|
|
|
client.on("ready", () => {
|
2017-12-08 02:00:25 +02:00
|
|
|
parseLinksInGuilds().then(doGuildIteration);
|
2017-12-07 03:35:00 +02:00
|
|
|
});
|
2017-09-14 15:09:43 +03:00
|
|
|
|
2017-12-07 03:35:00 +02:00
|
|
|
client.on("message", message => {
|
|
|
|
if (message.channel.type !== "text" || !message.member)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client.guildDataModel.findOne({ guildID: message.guild.id })
|
|
|
|
.then(guildData => {
|
2017-12-08 00:48:49 +02:00
|
|
|
if (guildData) {
|
|
|
|
guildData.feeds.forEach(feedData =>
|
|
|
|
message.channel.id === feedData.channelID && feedData.cache(...GetUrls(message.content)));
|
|
|
|
guildData.save();
|
|
|
|
}
|
2017-12-07 03:35:00 +02:00
|
|
|
});
|
2017-09-24 23:31:30 +03:00
|
|
|
});
|
2017-09-01 17:51:05 +03:00
|
|
|
|
2017-09-24 23:31:30 +03:00
|
|
|
client.bootstrap();
|
2017-09-01 17:51:05 +03:00
|
|
|
|
2017-09-20 02:04:04 +03:00
|
|
|
//INTERNAL FUNCTIONS//
|
2017-12-08 02:00:25 +02:00
|
|
|
function doGuildIteration() {
|
|
|
|
const guild = guildsIterator.next().value;
|
|
|
|
guild && client.guildDataModel.findOne({ guildID: guild.id })
|
|
|
|
.then(guildData => guildData && guildData.checkFeeds(guild));
|
2017-09-14 15:09:43 +03:00
|
|
|
}
|
2017-09-01 17:51:05 +03:00
|
|
|
|
2017-10-02 02:52:36 +03:00
|
|
|
function parseLinksInGuilds() {
|
2017-09-14 15:09:43 +03:00
|
|
|
const promises = [];
|
2017-12-07 03:35:00 +02:00
|
|
|
client.guildDataModel.find().then(guildDatas =>
|
2017-12-08 02:07:02 +02:00
|
|
|
guildDatas.forEach(guildData => {
|
|
|
|
if (client.guilds.get(guildData.guildID))
|
|
|
|
promises.push(guildData.cachePastPostedLinks(client.guilds.get(guildData.guildID)));
|
|
|
|
}));
|
2017-10-02 02:41:55 +03:00
|
|
|
|
2017-12-07 03:35:00 +02:00
|
|
|
return Promise.all(promises);
|
2017-09-24 23:31:30 +03:00
|
|
|
}
|