fix message clumping and also add discord<->brainlet bridge bot

This commit is contained in:
hippoz 2020-12-07 23:25:59 +02:00
parent f0e6051fbe
commit b476466d53
8 changed files with 252 additions and 22 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@ node_modules/
node_modules/* node_modules/*
secret.js secret.js
bridgebotsecret.js

View file

@ -265,13 +265,16 @@ const app = new Vue({
if (!lastMessage) return; if (!lastMessage) return;
if (lastMessage.author._id === messageObject.author._id) { if (lastMessage.author._id === messageObject.author._id) {
if (lastMessage.nickAuthor && messageObject.nickAuthor && lastMessage.nickAuthor.username === messageObject.nickAuthor.username) { if (lastMessage.nickAuthor && messageObject.nickAuthor) {
if (lastMessage.nickAuthor.username === messageObject.nickAuthor.username) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
}
if (lastMessage.author._id === messageObject.author._id) return true; if (lastMessage.author._id === messageObject.author._id) return true;
return false;
}, },
processMessage: async function(messageObject) { processMessage: async function(messageObject) {
if (!this.messages[messageObject.category._id]) this.$set(this.messages, messageObject.category._id, []); if (!this.messages[messageObject.category._id]) this.$set(this.messages, messageObject.category._id, []);

View file

@ -1,8 +1,8 @@
module.exports = { module.exports = {
ports: { ports: {
mainServerPort: 3005, mainServerPort: 3000,
}, },
address: 'b.hippoz.xyz', address: 'localhost',
mongoUrl: 'mongodb://192.168.0.105:27017/app', mongoUrl: 'mongodb://192.168.0.105:27017/app',
bcryptRounds: 10, bcryptRounds: 10,
roleMap: { roleMap: {

74
libbrainlet/bridgebot.js Normal file
View file

@ -0,0 +1,74 @@
const Client = require('./index');
const secret = require('./bridgebotsecret');
const fetch = require('node-fetch');
const Discord = require('discord.js');
const LISTEN_ON = '5fce94aa37c98b23bcd140a0';
const DISCORD_LISTEN_ON = '785494888766373909';
const PREFIX = '::';
const ADMIN_ID = '5fc828ea4e96e00725c17fd7';
let discordWebhook;
const main = async () => {
const client = new Client('http://localhost:3000', {
throwErrors: true
});
const discord = new Discord.Client();
const res = await fetch(`${client.url}/api/v1/users/token/create`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: secret.brainlet.user.username,
password: secret.brainlet.user.password
})
});
const json = await res.json();
if (!res.ok || json.error) throw new Error('Failed to generate token from API endpoint');
await client.setToken(json.token);
await client.gatewayConnect();
client.gateway.on('connect', () => {
discord.on('ready', async () => {
const channel = discord.channels.cache.get(DISCORD_LISTEN_ON);
const webhooks = await channel.fetchWebhooks();
discordWebhook = webhooks.first();
if (!discordWebhook) throw new Error('No webhook in selected channel');
console.log(`[*] [DISCORD] Logged in as ${discord.user.tag}!`);
});
discord.login(secret.discord.token);
const category = client.gateway.subscribeToCategoryChat(LISTEN_ON);
discord.on('message', (e) => {
if (e.webhookID) return;
if (e.author.bot) return;
client.gateway.sendMessage(LISTEN_ON, e.content, {
nickAuthor: { username: e.author.username }
});
});
client.gateway.on('message', async (e) => {
if (e.author._id === client.user._id) return;
await discordWebhook.send(e.content, {
username: e.author.username
});
});
});
};
main();

View file

@ -0,0 +1,11 @@
module.exports = {
discord: {
token: 'Nzg1NDk0MjgxOTI2MjEzNjYz.X84qjg.3gAdgnp_VmTSLvgwYmCBceLGHcA'
},
brainlet: {
user: {
username: 'e',
password: 'e'
}
}
}

View file

@ -4,11 +4,39 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@discordjs/collection": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz",
"integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ=="
},
"@discordjs/form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
},
"@types/component-emitter": { "@types/component-emitter": {
"version": "1.2.10", "version": "1.2.10",
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.10.tgz", "resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.10.tgz",
"integrity": "sha512-bsjleuRKWmGqajMerkzox19aGbscQX5rmmvvXl3wlIp5gMG1HgkiwPxsN5p070fBDKTNSPgojVbuY1+HWMbFhg==" "integrity": "sha512-bsjleuRKWmGqajMerkzox19aGbscQX5rmmvvXl3wlIp5gMG1HgkiwPxsN5p070fBDKTNSPgojVbuY1+HWMbFhg=="
}, },
"abort-controller": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
"integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
"requires": {
"event-target-shim": "^5.0.0"
}
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"backo2": { "backo2": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
@ -19,6 +47,14 @@
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz", "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz",
"integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI=" "integrity": "sha1-mBjHngWbE1X5fgQooBfIOOkLqBI="
}, },
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"requires": {
"delayed-stream": "~1.0.0"
}
},
"component-bind": { "component-bind": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz", "resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz",
@ -37,6 +73,33 @@
"ms": "^2.1.1" "ms": "^2.1.1"
} }
}, },
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
},
"discord.js": {
"version": "12.5.1",
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz",
"integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==",
"requires": {
"@discordjs/collection": "^0.1.6",
"@discordjs/form-data": "^3.0.1",
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.1",
"prism-media": "^1.2.2",
"setimmediate": "^1.0.5",
"tweetnacl": "^1.0.3",
"ws": "^7.3.1"
},
"dependencies": {
"ws": {
"version": "7.4.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.1.tgz",
"integrity": "sha512-pTsP8UAfhy3sk1lSk/O/s4tjD0CRwvMnzvwr4OKGX7ZvqZtUyx4KIJB5JWbkykPoc55tixMGgTNoh3k4FkNGFQ=="
}
}
},
"engine.io-client": { "engine.io-client": {
"version": "4.0.4", "version": "4.0.4",
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-4.0.4.tgz", "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-4.0.4.tgz",
@ -59,11 +122,29 @@
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.1.tgz", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.1.tgz",
"integrity": "sha512-v5aZK1hlckcJDGmHz3W8xvI3NUHYc9t8QtTbqdR5OaH3S9iJZilPubauOm+vLWOMMWzpE3hiq92l9lTAHamRCg==" "integrity": "sha512-v5aZK1hlckcJDGmHz3W8xvI3NUHYc9t8QtTbqdR5OaH3S9iJZilPubauOm+vLWOMMWzpE3hiq92l9lTAHamRCg=="
}, },
"event-target-shim": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ=="
},
"has-cors": { "has-cors": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz", "resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz",
"integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk=" "integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk="
}, },
"mime-db": {
"version": "1.44.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
"integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg=="
},
"mime-types": {
"version": "2.1.27",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz",
"integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==",
"requires": {
"mime-db": "1.44.0"
}
},
"ms": { "ms": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@ -84,6 +165,16 @@
"resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.6.tgz", "resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.6.tgz",
"integrity": "sha512-AUjen8sAkGgao7UyCX6Ahv0gIK2fABKmYjvP4xmy5JaKvcbTRueIqIPHLAfq30xJddqSE033IOMUSOMCcK3Sow==" "integrity": "sha512-AUjen8sAkGgao7UyCX6Ahv0gIK2fABKmYjvP4xmy5JaKvcbTRueIqIPHLAfq30xJddqSE033IOMUSOMCcK3Sow=="
}, },
"prism-media": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz",
"integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A=="
},
"setimmediate": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz",
"integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU="
},
"socket.io-client": { "socket.io-client": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-3.0.3.tgz", "resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-3.0.3.tgz",
@ -109,6 +200,11 @@
"debug": "~4.1.0" "debug": "~4.1.0"
} }
}, },
"tweetnacl": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz",
"integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw=="
},
"ws": { "ws": {
"version": "7.2.5", "version": "7.2.5",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.2.5.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.5.tgz",

View file

@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"discord.js": "^12.5.1",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"socket.io-client": "^3.0.3" "socket.io-client": "^3.0.3"
} }

View file

@ -1,13 +1,36 @@
const Client = require('./index'); const Client = require('./index');
const fetch = require('node-fetch');
const LISTEN_ON = '5fcbf598b39160080e797ad6'; const LISTEN_ON = '5fcbf598b39160080e797ad6';
const TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IlNTQm90IiwiaWF0IjoxNjA3MjAyMjUxLCJleHAiOjE2MDcyMTMwNTF9.V9dL3cTe37M5-QDftK4XQwLMnPKoUcaGqL_YVstGTYo';
const PREFIX = '::'; const PREFIX = '::';
const ADMIN_ID = '5fc828ea4e96e00725c17fd7'; const ADMIN_ID = '5fc828ea4e96e00725c17fd7';
const joined = []; const joined = [];
const selected = []; const selected = [];
// https://stackoverflow.com/questions/17619741/randomly-generating-unique-number-in-pairs-multiple-times
const randomPairs = (players) => {
const pairs = [];
while(players.length) {
pairs.push([
pluckRandomElement(players),
pluckRandomElement(players)
]);
}
return pairs;
};
const pluckRandomElement = (array) => {
const i = randomInt(array.length);
return array.splice(i, 1)[0];
};
const randomInt = (limit) => {
return Math.floor(Math.random() * limit);
};
const getRandomUser = (self, count=0) => { const getRandomUser = (self, count=0) => {
if (count > 3) return joined[0]; if (count > 3) return joined[0];
count++; count++;
@ -16,9 +39,9 @@ const getRandomUser = (self, count=0) => {
let chosen = joined[Math.floor(Math.random() * joined.length)]; let chosen = joined[Math.floor(Math.random() * joined.length)];
final = chosen; final = chosen;
if (chosen._id === self._id) final = getRandomUser(self, count); if (chosen._id === self._id) return final = getRandomUser(self, count);
if ((selected.indexOf(chosen)) !== 1) final = getRandomUser(self, count); if ((selected.indexOf(chosen)) !== 1) return final = getRandomUser(self, count);
if (!final) final = getRandomUser(self, count); if (!final) return final = getRandomUser(self, count);
@ -29,7 +52,24 @@ const main = async () => {
const client = new Client('https://b.hippoz.xyz', { const client = new Client('https://b.hippoz.xyz', {
throwErrors: true throwErrors: true
}); });
await client.setToken(TOKEN);
const res = await fetch(`${client.url}/api/v1/users/token/create`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'D',
password: 'D'
})
});
const json = await res.json();
if (!res.ok || json.error) throw new Error('Failed to generate token from API endpoint');
await client.setToken(json.token);
await client.gatewayConnect(); await client.gatewayConnect();
client.gateway.on('connect', () => { client.gateway.on('connect', () => {
@ -79,16 +119,19 @@ const main = async () => {
nickAuthor: { username: 'Woo' } nickAuthor: { username: 'Woo' }
}); });
joined.forEach((e) => { const pairs = randomPairs(joined);
const chosen = getRandomUser(e);
selected.push(chosen); for (const pair of pairs) {
const p1 = pair[0];
client.gateway.sendMessage(category, `${e.username} - ${chosen.username}`, { const p2 = pair[1];
if (!p1 || !p2) continue;
for (const player of pair) {
client.gateway.sendMessage(category, `${p1.username} with ${p2.username}`, {
nickAuthor: { username: 'Your result' }, nickAuthor: { username: 'Your result' },
destUser: { _id: e._id } destUser: { _id: player._id }
});
}); });
}
}
break; break;
} }
} }