brainlet/libbrainlet/test.js
2020-12-05 23:23:50 +02:00

99 lines
No EOL
3.3 KiB
JavaScript

const Client = require('./index');
const LISTEN_ON = '5fcbf598b39160080e797ad6';
const TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IlNTQm90IiwiaWF0IjoxNjA3MjAyMjUxLCJleHAiOjE2MDcyMTMwNTF9.V9dL3cTe37M5-QDftK4XQwLMnPKoUcaGqL_YVstGTYo';
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('https://b.hippoz.xyz', {
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();