From 06f6a3287dd5c2a8e977f43ac55e1267510b95c8 Mon Sep 17 00:00:00 2001 From: hippoz Date: Sat, 5 Dec 2020 22:56:41 +0200 Subject: [PATCH] add epic bot --- libbrainlet/gatewayconnection.js | 2 + libbrainlet/test.js | 91 +++++++++++++++++++++++++++++--- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/libbrainlet/gatewayconnection.js b/libbrainlet/gatewayconnection.js index e28529a..729fb93 100644 --- a/libbrainlet/gatewayconnection.js +++ b/libbrainlet/gatewayconnection.js @@ -81,6 +81,8 @@ GatewayConnection.prototype.subscribeToCategoryChat = function(categoryId) { console.log('[*] [gateway] Subscribing to channel(s)', request); this.socket.emit('subscribe', request); + + return categoryId; }; module.exports = GatewayConnection; \ No newline at end of file diff --git a/libbrainlet/test.js b/libbrainlet/test.js index f8d662a..5f8063f 100644 --- a/libbrainlet/test.js +++ b/libbrainlet/test.js @@ -1,22 +1,97 @@ const Client = require('./index'); +const LISTEN_ON = '5fc829314e96e00725c17fd8'; +const TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxOTE5NjAsImV4cCI6MTYwNzIwMjc2MH0.dfI6CMeDNck-ubqwEmiMFdHbRR4iiuooeTuzgVc_8rY'; +const PREFIX = '::'; +const ADMIN_ID = '5fc828ea4e96e00725c17fd7'; + +const joined = []; +const selected = []; + +const getRandomUser = (self, count=0) => { + if (count > 3) return joined[0]; + count++; + + let final; + + let chosen = joined[Math.floor(Math.random() * joined.length)]; + final = chosen; + if (chosen._id === self._id) final = getRandomUser(self, count); + if ((selected.indexOf(chosen)) !== 1) final = getRandomUser(self, count); + if (!final) final = getRandomUser(self, count); + + + + return final || null; +}; + const main = async () => { const client = new Client('http://localhost:3000', { throwErrors: true }); - - await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxOTE5NjAsImV4cCI6MTYwNzIwMjc2MH0.dfI6CMeDNck-ubqwEmiMFdHbRR4iiuooeTuzgVc_8rY'); - + await client.setToken(TOKEN); await client.gatewayConnect(); client.gateway.on('connect', () => { - client.gateway.subscribeToCategoryChat('5fc829314e96e00725c17fd8'); + const category = client.gateway.subscribeToCategoryChat(LISTEN_ON); + client.gateway.on('message', (e) => { if (e.author._id === client.user._id) return; - client.gateway.sendMessage('5fc829314e96e00725c17fd8', e.content, { - nickAuthor: {username: e.author.username}, - //destUser: {_id: e.author._id} - }); + if (!e.content.startsWith(PREFIX)) return; + if (e.category._id !== category) return; + + const cmdString = e.content.substring(PREFIX.length); + const cmdFull = cmdString.split(' '); + const cmd = cmdFull[0] || 'NO_CMD'; + + console.log(cmdFull); + + switch (cmd) { + case 'join': { + const existing = joined.findIndex((o) => { + return o._id === e.author._id; + }); + + if (existing !== -1) { + client.gateway.sendMessage(category, 'Already joined', { + nickAuthor: { username: 'Error' }, + destUser: { _id: e.author._id } + }); + break; + } + + joined.push(e.author); + console.log(`[*] User joined`, e.author); + client.gateway.sendMessage(category, `${e.author.username} joined!`, { + nickAuthor: { username: 'New join!' } + }); + break; + } + case 'roll': { + if (e.author._id !== ADMIN_ID) { + client.gateway.sendMessage(category, 'Access denied', { + nickAuthor: { username: 'Error' }, + destUser: { _id: e.author._id } + }); + break; + } + client.gateway.sendMessage(category, 'Rolling...', { + nickAuthor: { username: 'Woo' } + }); + + joined.forEach((e) => { + const chosen = getRandomUser(e); + + selected.push(chosen); + + client.gateway.sendMessage(category, `${e.username} - ${chosen.username}`, { + nickAuthor: { username: 'Your result' }, + destUser: { _id: e._id } + }); + }); + break; + } + } }); }); };