diff --git a/bot.js b/bot.js index 753137b0c11ee322c94891f4dc952511e58c367b..a9869f6bdd4e02cafd8cbbd9538728cf65b2021a 100644 --- a/bot.js +++ b/bot.js @@ -15,7 +15,7 @@ class Bot extends EventEmitter { this.access_token = access_token; this.to_listen = to_listen; - this.listeners = []; + this.listeners = new WeakMap(); } /** @@ -28,9 +28,9 @@ class Bot extends EventEmitter { }); for(const listen of this.to_listen) { - this.listeners[listen] = this.M.stream('streaming/' + listen.api_point); + this.listeners.set(listen, this.M.stream('streaming/' + listen.api_point)); - this.listeners[listen].on('message', (msg) => { + this.listeners.get(listen).on('message', (msg) => { for(const event of listen.events) { if (msg.event === event) { this.emit(msg.event, msg.data) @@ -39,7 +39,7 @@ class Bot extends EventEmitter { }) } - this.me = (await this.M.get('accounts/verify_credentials')).data; + this.me = (await this.verify_credentials()); } /** @@ -55,9 +55,7 @@ class Bot extends EventEmitter { * @returns {Array} */ async following_list() { - const following_list = await this.M.get('accounts/' + this.me.id + '/following'); - - return following_list.data; + return (await this.M.get('accounts/' + this.me.id + '/following')).data; } /** @@ -65,13 +63,11 @@ class Bot extends EventEmitter { * @returns {Array} */ async followers_list() { - const followers_list = await this.M.get('accounts/' + this.me.id + '/followers'); - - return followers_list.data; + return (await this.M.get('accounts/' + this.me.id + '/followers')).data; } async verify_credentials() { - return await this.M.get('accounts/verify_credentials'); + return (await this.M.get('accounts/verify_credentials')).data; } /** diff --git a/index.js b/index.js index fadc7a84a9164414e114877495b70fe12240b340..a7af4c4f7704a743eb096df0abf5509388e881e8 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,15 @@ const Bot = require("./bot"); const striptags = require('striptags'); const fs = require('fs'); -const Dict = require('collections/dict'); const config = require('./config.json'); // Hardcode admins since Mastodon API doesn't provide such thing... -const admins = ['tagadmin', 'Dhveszak', 'Incandescente']; +const admins = new Set(['tagadmin', 'Dhveszak', 'Incandescente']); const client = new Bot(config, [{api_point: "public", events: ["update"]}, {api_point: "user", events: ["notification"]}]); -const commands = new Dict(); +const commands = new Map(); const commandFiles = fs.readdirSync(__dirname + "/commands/"); for (const file of commandFiles) { const commandClass = require(__dirname + "/commands/" + file); @@ -19,12 +18,12 @@ for (const file of commandFiles) { } // Following list -const following = []; +const following = new Set(); client.start().then(() => { client.following_list().then((result) => { for(const account of result) { - following.push(account.acct) + following.add(account.acct) } }); }); @@ -41,20 +40,19 @@ client.on('update', (msg) => { } // This will be... not optimal I think. - for (const follow of following) { - if (follow === acct) { - console.log('ALREADY FOLLOWS: ' + acct); - return; - } + if (following.has(acct)) { + console.log('ALREADY FOLLOWS: ' + acct); + return; } + // Respect #nobot if (striptags(msg.account.note).match(/#nobot/i)) { client.mute_user(id); console.log("MUTED #nobot: " + acct); } - following.push(acct); + following.add(acct); client.follow(id); console.log("NOW FOLLOWS: " + acct); }); @@ -82,7 +80,7 @@ client.on("notification", (msg) => { if (command.disabled === true) return; //Check if command is AdminOnly - if(command.admin_only === true && !admins.some((e) => { return e === msg.account.acct;})) return; + if(command.admin_only === true && !admins.has(msg.account.acct)) return; //Check args length if (command.required_args > args) { diff --git a/package.json b/package.json index cd1f73ea62ce5e47e1bd3532ae2b7b072f003c58..8f668da0e0a835395877e8698e018cdbd40f81fc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "federationbot", "version": "0.0.1", "dependencies": { - "collections": "^5.1.2", "mastodon-api": "^1.3.0", "striptags": "^3.1.1" }