forked from hippoz/brainlet
add epic bot
This commit is contained in:
parent
4147e4f6ca
commit
06f6a3287d
2 changed files with 85 additions and 8 deletions
|
@ -81,6 +81,8 @@ GatewayConnection.prototype.subscribeToCategoryChat = function(categoryId) {
|
||||||
console.log('[*] [gateway] Subscribing to channel(s)', request);
|
console.log('[*] [gateway] Subscribing to channel(s)', request);
|
||||||
|
|
||||||
this.socket.emit('subscribe', request);
|
this.socket.emit('subscribe', request);
|
||||||
|
|
||||||
|
return categoryId;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = GatewayConnection;
|
module.exports = GatewayConnection;
|
|
@ -1,22 +1,97 @@
|
||||||
const Client = require('./index');
|
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 main = async () => {
|
||||||
const client = new Client('http://localhost:3000', {
|
const client = new Client('http://localhost:3000', {
|
||||||
throwErrors: true
|
throwErrors: true
|
||||||
});
|
});
|
||||||
|
await client.setToken(TOKEN);
|
||||||
await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxOTE5NjAsImV4cCI6MTYwNzIwMjc2MH0.dfI6CMeDNck-ubqwEmiMFdHbRR4iiuooeTuzgVc_8rY');
|
|
||||||
|
|
||||||
await client.gatewayConnect();
|
await client.gatewayConnect();
|
||||||
|
|
||||||
client.gateway.on('connect', () => {
|
client.gateway.on('connect', () => {
|
||||||
client.gateway.subscribeToCategoryChat('5fc829314e96e00725c17fd8');
|
const category = client.gateway.subscribeToCategoryChat(LISTEN_ON);
|
||||||
|
|
||||||
client.gateway.on('message', (e) => {
|
client.gateway.on('message', (e) => {
|
||||||
if (e.author._id === client.user._id) return;
|
if (e.author._id === client.user._id) return;
|
||||||
client.gateway.sendMessage('5fc829314e96e00725c17fd8', e.content, {
|
if (!e.content.startsWith(PREFIX)) return;
|
||||||
nickAuthor: {username: e.author.username},
|
if (e.category._id !== category) return;
|
||||||
//destUser: {_id: e.author._id}
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue