Skip to content

Instantly share code, notes, and snippets.

@christophernarciso
Created April 29, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophernarciso/4c611c037df60d2cecb4998e7a2e2a9b to your computer and use it in GitHub Desktop.
Save christophernarciso/4c611c037df60d2cecb4998e7a2e2a9b to your computer and use it in GitHub Desktop.
Comment Watcher for OSBot.org Script Writers
const configData = require('./config');
const webhook = require('webhook-discord');
const cloudscraper = require('cloudscraper');
const JSSoup = require('jssoup').default;
const Hook = new webhook.Webhook(configData.webhook);
const ENDING = 'page/5000/';
const THREADS = configData.threads;
const DATE_CACHE = configData.cache;
const getRandomColor = () => {
return '#' + Math.floor(Math.random() * 16777215).toString(16);
};
const timeout = millis => new Promise(resolve => setTimeout(resolve, millis));
const sendMessage = (script, thread, date, author, message) => {
if (date === null) {
const messageBuilder = new webhook.MessageBuilder()
.setName(configData.botname)
.setColor(getRandomColor())
.addField('Date', new Date().toString())
.addField('Author', author)
.addField('Message', message);
return Hook.send(messageBuilder);
}
const messageBuilder = new webhook.MessageBuilder()
.setName(configData.botname)
.setColor(getRandomColor())
.addField('Topic', script)
.addField('Thread Link', thread)
.addField('Author', author)
.addField('Comment', message)
.addField('Date', new Date(date).toString());
Hook.send(messageBuilder);
};
(async function main() {
sendMessage(null, null, null, configData.name, configData.start.concat(` version: ${configData.version}`, ` timeout(ms): ${configData.timeout}`));
while (true) {
for (idx in THREADS) {
const url = THREADS[idx].concat(ENDING);
const source = await cloudscraper.get(url);
if (source) {
const soup = new JSSoup(source);
const t = soup.find('script', {'type': 'application/ld+json'}).text;
const json = JSON.parse(t);
const scriptName = json['name'];
const comments = json['comment'];
console.log(`Loading comments for ${scriptName}`);
comments.forEach(function (data) {
const date = new Date(data['dateCreated']).getTime();
const author = data['author'].name;
const thread = data['url'];
const message = data['text'];
if (author === configData.name)
return;
if (date > DATE_CACHE[scriptName]) {
console.log(`Found a recent comment by user ${author} on topic [${scriptName}][${date}].`);
DATE_CACHE[scriptName] = date;
sendMessage(scriptName, thread, date, author, message);
}
});
}
}
// Delay next thread check
console.log('Waiting a bit for next cycle');
await timeout(120000);
}
})();
@christophernarciso
Copy link
Author

config.json

{ "_comment0": "Please edit name, botname, start, timeout, webook", "name": "Chris", "botname": "CWS", "about": "Decided to create this with inspiration from Malcolm@OSBot.org python src.", "start": "Starting comment watcher support", "timeout": 120000, "version": 1, "webhook": "WEBHOOK_URL_HERE", "_comment1": "threads... The list of threads to use", "threads": [ "https://osbot.org/forum/topic/157064-excellent-vorkath/", "https://osbot.org/forum/topic/104463-excellent-dragons/", "https://osbot.org/forum/topic/125680-excellent-agility/", "https://osbot.org/forum/topic/155717-legendary-lava-dragons/" ], "_comment2": "cache key:value... The key should be the thread title. The value will default to today at 10am ish EST unless you change it by new Date().getTime() in your browser console", "cache": { "Excellent Vorkath": 1588170081901, "Excellent Agility": 1588170081901, "Excellent Dragons": 1588170081901, "Legendary Lava Dragons": 1588170081901 } }

@christophernarciso
Copy link
Author

{
"_comment0": "Please edit name, botname, start, timeout, webook",
"name": "Chris",
"botname": "CWS",
"about": "Decided to create this with inspiration from Malcolm@OSBot.org python src.",
"start": "Starting comment watcher support",
"timeout": 120000,
"version": 1,
"webhook": "WEBHOOK_URL_HERE",
"_comment1": "threads... The list of threads to use",
"threads": [
"https://osbot.org/forum/topic/157064-excellent-vorkath/",
"https://osbot.org/forum/topic/104463-excellent-dragons/",
"https://osbot.org/forum/topic/125680-excellent-agility/",
"https://osbot.org/forum/topic/155717-legendary-lava-dragons/"
],
"_comment2": "cache key:value... The key should be the thread title. The value will default to today at 10am ish EST unless you change it by new Date().getTime() in your browser console",
"cache": {
"Excellent Vorkath": 1588170081901,
"Excellent Agility": 1588170081901,
"Excellent Dragons": 1588170081901,
"Legendary Lava Dragons": 1588170081901
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment