From dd9c79fec8553d502f3767ffa92e4350ffda605a Mon Sep 17 00:00:00 2001
From: Tagadda <36127788+Tagadda@users.noreply.github.com>
Date: Thu, 14 Jun 2018 04:11:44 +0200
Subject: [PATCH] formatting, rearrange, doc
---
bot.js | 19 ++++-----
config.json.example | 4 +-
index.js | 94 ++++++++++++++++++++-------------------------
3 files changed, 54 insertions(+), 63 deletions(-)
diff --git a/bot.js b/bot.js
index 55a18e6..259b350 100644
--- a/bot.js
+++ b/bot.js
@@ -28,8 +28,7 @@ class Bot extends EventEmitter {
access_token: this.access_token
});
-
- for(const listen of this.to_listen) {
+ for (const listen of this.to_listen) {
this.listeners.set(listen, this.M.stream('streaming/' + listen.api_point));
this.listeners.get(listen).on('message', (msg) => {
@@ -60,14 +59,14 @@ class Bot extends EventEmitter {
if (next_id === 0) {
test = await this.M.get('accounts/' + this.me.id + '/following', {limit: 80});
} else {
- test = await this.M.get('accounts/' + this.me.id + '/following', {limit: 80, max_id:next_id});
+ test = await this.M.get('accounts/' + this.me.id + '/following', {limit: 80, max_id: next_id});
}
- next_id = parseInt(wurl('?max_id' ,test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
+ next_id = parseInt(wurl('?max_id', test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
result = result.concat(test.data);
- if(test.data.length < 80) break;
+ if (test.data.length < 80) break;
}
return result;
@@ -87,14 +86,16 @@ class Bot extends EventEmitter {
if (next_id === 0) {
test = await client.M.get('accounts/' + client.me.id + '/followers', {limit: 80});
} else {
- test = await client.M.get('accounts/' + client.me.id + '/followers', {limit: 80, max_id:next_id});
+ test = await client.M.get('accounts/' + client.me.id + '/followers', {limit: 80, max_id: next_id});
}
- next_id = parseInt(wurl('?max_id' ,test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
+ // Sorry for this brainfart...
+ // test.resp.headers.link = <https://crazynoisybizarre.town/api/v1/accounts/29127/following?max_id=2808>; rel="next", <https://crazynoisybizarre.town/api/v1/accounts/29127/following?since_id=4024>; rel="prev"
+ next_id = parseInt(wurl('?max_id', test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
result = result.concat(test.data);
- if(test.data.length < 80) break;
+ if (test.data.length < 80) break;
}
return result;
@@ -163,7 +164,7 @@ class Bot extends EventEmitter {
* @param {string} domain
*/
block_domain(domain) {
- this.M.post('domain_blocks', { domain });
+ this.M.post('domain_blocks', {domain});
}
}
diff --git a/config.json.example b/config.json.example
index bdefa0d..dce8623 100644
--- a/config.json.example
+++ b/config.json.example
@@ -1,5 +1,7 @@
{
"access_token": "",
"api_url": "https://botsin.space/api/v1/",
- "admins": ["admin_username"]
+ "admins": [
+ "admin_username"
+ ]
}
\ No newline at end of file
diff --git a/index.js b/index.js
index 915f5f3..8ce5745 100644
--- a/index.js
+++ b/index.js
@@ -4,9 +4,9 @@ const fs = require('fs');
const config = require('./config.json');
// Init
-const client = new Bot(config, [{api_point: "public", event: "update", emit_on: "federated"},
- {api_point: "user", event: "notification", emit_on: "mentions"},
- {api_point: "user", event: "update", emit_on: "home"}]);
+const client = new Bot(config, [{api_point: "public", event: "update", emit_on: "federated"},
+ {api_point: "user", event: "notification", emit_on: "mentions"},
+ {api_point: "user", event: "update", emit_on: "home"}]);
const admins = new Set(config.admins);
@@ -23,7 +23,7 @@ for (const file of commandFiles) {
// Start the bot and populate following
client.start().then(() => {
client.following_list().then((result) => {
- for(const account of result) following.add(account.acct)
+ for (const account of result) following.add(account.acct)
console.log(`I'm currently following ${following.size} accounts.`)
});
@@ -41,13 +41,42 @@ client.on('home', (msg) => {
if (msg.reblog !== null) follow_or_not_follow(msg.reblog);
});
+function follow_or_not_follow(msg) {
+ const acct = msg.account.acct;
+ const id = parseInt(msg.account.id);
+ const acct_parts = acct.split('@');
+
+ // Don't follow local accounts
+ if (acct_parts.length === 1) return;
+
+ // Don't follow locked accounts
+ if (msg.account.locked === true) return;
+
+ // Don't follow other bots
+ if (msg.account.bot === true) return;
+
+ // Respect #nobot
+ if (striptags(msg.account.note).match(/#nobot/i)) {
+ client.mute_user(id);
+ console.log("MUTED #nobot: " + acct);
+ return;
+ }
+
+ // Already following. (This will be... not optimal I think.)
+ if (following.has(acct)) return;
+
+ following.add(acct);
+ client.follow(id);
+ console.log("NOW FOLLOWS: " + acct);
+}
+
// When a toot mention the bot
client.on("mentions", (msg) => {
if (msg.type !== "mention") return;
const status = striptags(msg.status.content);
- let full_acct = '@' + client.me.acct;
+ const full_acct = '@' + client.me.acct;
// Starting with @username@domain or @username (local toots)
if (!status.startsWith(full_acct) || !status.startsWith('@' + client.me.username)) return;
@@ -55,24 +84,23 @@ client.on("mentions", (msg) => {
const args = status.slice(client.me.acct.length + 1).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
- //Check if command exists
- if (!commands.has(commandName)) {
- return;
- }
+ // Check if command exists
+ if (!commands.has(commandName)) return;
+
const command = commands.get(commandName);
- //Check if command is disabled
+ // Check if command is disabled
if (command.disabled === true) return;
- //Check if command is AdminOnly
- if(command.admin_only === true && !admins.has(msg.account.acct)) return;
+ // Check if command is AdminOnly
+ if (command.admin_only === true && !admins.has(msg.account.acct)) return;
//Check args length
if (command.required_args > args) return;
console.log(`CMD: ${commandName} from ${msg.account.acct}`);
- //Execute the command
+ // Execute the command
try {
command.execute(msg, args);
}
@@ -85,43 +113,3 @@ process.on('SIGINT', () => {
console.info("Exiting...");
process.exit();
});
-
-function follow_or_not_follow(msg) {
- const acct = msg.account.acct;
- const id = parseInt(msg.account.id);
- const acct_parts = acct.split('@');
-
- if (acct_parts.length === 1) {
- //console.log('LOCAL ACCOUNT: ' + acct);
- return;
- }
-
- // Don't follow locked accounts
- if(msg.account.locked === true) {
- //console.log("LOCKED: " + acct);
- return;
- }
-
- // Don't follow other bots
- if(msg.account.bot === true) {
- //console.log("BOT: " + acct);
- return;
- }
-
- // Respect #nobot
- if (striptags(msg.account.note).match(/#nobot/i)) {
- client.mute_user(id);
- console.log("MUTED #nobot: " + acct);
- return;
- }
-
- // This will be... not optimal I think.
- if (following.has(acct)) {
- //console.log('ALREADY FOLLOWS: ' + acct);
- return;
- }
-
- following.add(acct);
- client.follow(id);
- console.log("NOW FOLLOWS: " + acct);
-}
\ No newline at end of file
--
GitLab