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(TOKEN); await client.gatewayConnect(); client.gateway.on('connect', () => { const category = client.gateway.subscribeToCategoryChat(LISTEN_ON); client.gateway.on('message', (e) => { if (e.author._id === client.user._id) return; 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; } } }); }); }; main();