diff --git a/.gitignore b/.gitignore index e1da6ae..fe01895 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Config +config.json + # Logs logs *.log diff --git a/config-template.json b/config-template.json new file mode 100644 index 0000000..9a86590 --- /dev/null +++ b/config-template.json @@ -0,0 +1,17 @@ +{ + "motd": "Bot is running...", + "status": "your wishes", + "statusType": "LISTENING", + "guildId": "", + "token": "", + "checkTime": 7, + "goods": { + "PlayStation 5": [ + {"name": "", "url": "", "delimiter": "", "elm": "", "text": ""}, + {"name": "Amazon", "url": "https://www.amazon.com/PlayStation-5-Console/dp/B08FC5L3RG", "delimiter": "a-size-medium a-color-price", "elm": "span", "text": "Currently unavailable."} + ] + }, + "data": { + "PlayStation 5": {"role": "", "channel": ""} + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..6a5ae0a --- /dev/null +++ b/index.js @@ -0,0 +1,66 @@ +'use strict'; + +const Discord = require('discord.js'); +const fetch = require('node-fetch'); +const bot = new Discord.Client(); +const cfg = require('./config.json'); + +bot.on('ready', () => { + console.clear(); + console.log(cfg.motd); + bot.user.setActivity(cfg.status, { type: cfg.statusType }); + initCheckout(bot); +}); + +bot.login(cfg.token); + +const initCheckout = (bot) => { + bot.guilds.fetch(cfg.guildId) + .then(guild => { + doCheckout(bot, guild); + setInterval(() => { + doCheckout(bot, guild); + }, cfg.checkTime*60000); + }); +}; + +const doCheckout = (bot, guild) => { + const goods = cfg.goods; + const data = cfg.data; + for (var k in goods) { + if (goods.hasOwnProperty(k)) { + goods[k].forEach((v) => { + guild.roles.fetch(data[k].role) + .then(role => { + bot.channels.fetch(data[k].channel) + .then(ch => { + checkStock(ch, role, k, v.name, v.url, v.delimiter, v.elm, v.text); + }); + }); + }); + } + } +}; + +const checkStock = (ch, role, good, name, url, delimiter, elm, text) => { + fetch(url, { + method: 'GET', + follow: 10, + headers: { + 'User-Agent': 'GoodsGirl/OwO' + } + }) + .then(res => res.text()) + .then(body => { + if (body.indexOf(delimiter) != -1) { + var check = `