diff --git a/bot.js b/bot.js index 55a18e6c055296784851a635b47b17511d250687..259b3502a087a18bcb4b099540dfde68564d21c2 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 bdefa0d682702bb9376f8f15fbdedd386d1e808a..dce86234d24c93f1507585af529061b4fbf37eff 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 915f5f30c32d28e883cd7a75f23311e27001b440..8ce57458d8ec896c144ced410b1c02ba364e3d29 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