forked from hippoz/brainlet
allow bots to set custom nicknames for messages
This commit is contained in:
parent
29b068a1c4
commit
9a6576fe98
6 changed files with 51 additions and 14 deletions
|
@ -152,7 +152,7 @@ GatewayServer.prototype.eventSetup = function() {
|
|||
console.log(`[*] [gateway] [handshake] Got yoo from ${socket.user.username}, connection is finally completed!`);
|
||||
socket.isConnected = true;
|
||||
|
||||
socket.on('message', ({ category, content }) => {
|
||||
socket.on('message', ({ category, content, nickAuthor }) => {
|
||||
if (!category || !content || !socket.joinedCategories || !socket.isConnected || !socket.user || !(typeof content === 'string') || !(typeof category._id === 'string')) return;
|
||||
content = content.trim();
|
||||
if (!content || content === '' || content === ' ' || content.length >= 2000) return;
|
||||
|
@ -161,12 +161,11 @@ GatewayServer.prototype.eventSetup = function() {
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
// TODO: When/if category permissions are added, check if the user has permissions for that category
|
||||
const categoryTitle = socket.joinedCategories[category._id];
|
||||
if (!categoryTitle || !(typeof categoryTitle === 'string')) return;
|
||||
|
||||
const messageObject = {
|
||||
let messageObject = {
|
||||
author: {
|
||||
username: socket.user.username,
|
||||
_id: socket.user._id,
|
||||
|
@ -180,6 +179,17 @@ GatewayServer.prototype.eventSetup = function() {
|
|||
_id: uuid.v4()
|
||||
};
|
||||
|
||||
if (nickAuthor && nickAuthor.username && (typeof nickAuthor.username) === 'string' && nickAuthor.username.length <= 32 && nickAuthor.username.length >= 3) {
|
||||
if (socket.user.permissionLevel === config.roleMap.BOT) {
|
||||
messageObject = {
|
||||
nickAuthor: {
|
||||
username: nickAuthor.username
|
||||
},
|
||||
...messageObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (messageObject.content.startsWith(this._commandPrefix)) {
|
||||
this._processCommand(socket, messageObject);
|
||||
return;
|
||||
|
|
|
@ -164,7 +164,8 @@
|
|||
<div v-for="post,k in messages[selection.category._id]" v-if="selection.category.isChatContext" :key="post._id + post.author._id">
|
||||
<md-card class="message-card">
|
||||
<md-card-header>
|
||||
<a class="md-dense cursor md-title" v-on:click="viewProfile(post.author._id)" v-bind:style="{ 'color': post.author.color}"><span>{{ post.author.username }}</span></a>
|
||||
<a v-if="!post.nickAuthor" class="md-dense cursor md-title" v-on:click="viewProfile(post.author._id)" v-bind:style="{ 'color': post.author.color}"><span>{{ post.author.username }}</span></a>
|
||||
<a v-if="post.nickAuthor" class="md-dense cursor md-title" v-on:click="viewProfile(post.author._id)" v-bind:style="{ 'color': post.author.color}"><span>{{ post.nickAuthor.username }} (from bot "{{ post.author.username }}")</span></a>
|
||||
</md-card-header>
|
||||
|
||||
<md-card-content style="white-space: break-spaces !important;">{{ post.content }}</md-card-content>
|
||||
|
|
|
@ -259,19 +259,29 @@ const app = new Vue({
|
|||
});
|
||||
});
|
||||
},
|
||||
shouldMergeMessage: function(messageObject, categoryMessageList) {
|
||||
const lastMessageIndex = categoryMessageList.length-1;
|
||||
const lastMessage = categoryMessageList[lastMessageIndex];
|
||||
|
||||
if (!lastMessage) return;
|
||||
if (lastMessage.author._id === messageObject.author._id) {
|
||||
if (lastMessage.nickAuthor && messageObject.nickAuthor && lastMessage.nickAuthor.username === messageObject.nickAuthor.username) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (lastMessage.author._id === messageObject.author.id) return true;
|
||||
},
|
||||
processMessage: async function(messageObject) {
|
||||
if (!this.messages[messageObject.category._id]) this.$set(this.messages, messageObject.category._id, []);
|
||||
const categoryMessageList = this.messages[messageObject.category._id];
|
||||
|
||||
const lastMessageIndex = categoryMessageList.length-1;
|
||||
const lastMessage = categoryMessageList[lastMessageIndex];
|
||||
if (lastMessage && lastMessage.author._id === messageObject.author._id) {
|
||||
categoryMessageList[lastMessageIndex].content += `\n${messageObject.content}`
|
||||
|
||||
if (this.shouldMergeMessage(messageObject, categoryMessageList)) {
|
||||
categoryMessageList[lastMessageIndex].content += `\n${messageObject.content}`;
|
||||
} else {
|
||||
this.messages[messageObject.category._id].push(messageObject);
|
||||
}
|
||||
|
||||
|
||||
if (messageObject.category._id === this.selection.category._id) {
|
||||
this.$nextTick(() => {
|
||||
// TODO: When the user presses back, actually undo this scroll cause its annoying to scroll back up in the category list
|
||||
|
|
|
@ -3,13 +3,14 @@ module.exports = {
|
|||
mainServerPort: 3000,
|
||||
},
|
||||
address: 'localhost',
|
||||
mongoUrl: 'mongodb://localhost:27017/app',
|
||||
mongoUrl: 'mongodb://192.168.0.105:27017/app',
|
||||
bcryptRounds: 10,
|
||||
roleMap: {
|
||||
'BANNED': 0,
|
||||
'RESTRICTED': 1,
|
||||
'USER': 2,
|
||||
'ADMIN': 3
|
||||
'BOT': 3,
|
||||
'ADMIN': 4
|
||||
},
|
||||
gatewayStillNotConnectedTimeoutMS: 15*1000
|
||||
};
|
|
@ -59,10 +59,20 @@ GatewayConnection.prototype.connect = function(token) {
|
|||
this.socket.on('clientListUpdate', (e) => this.emit('clientListUpdate', e));
|
||||
};
|
||||
|
||||
GatewayConnection.prototype.sendMessage = function(categoryId, content) {
|
||||
GatewayConnection.prototype.sendMessage = function(categoryId, content, nickAuthor=undefined) {
|
||||
if (!this.isConnected) return 1;
|
||||
if (content.length >= 2000) return 1;
|
||||
|
||||
if (nickAuthor) {
|
||||
this.socket.emit('message', {
|
||||
category: {
|
||||
_id: categoryId
|
||||
},
|
||||
nickAuthor,
|
||||
content
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.socket.emit('message', {
|
||||
category: {
|
||||
_id: categoryId
|
||||
|
|
|
@ -5,13 +5,18 @@ const main = async () => {
|
|||
throwErrors: true
|
||||
});
|
||||
|
||||
await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE2MDcwMjc5MzIsImV4cCI6MTYwNzAzODczMn0.9sHWxfPetp5efm11kaJP8wzsFfWjntVJ6COdqKGEuX4');
|
||||
await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxNzkyMzAsImV4cCI6MTYwNzE5MDAzMH0.X7KJA-8KmcdwTORXXVZcQTId3iz4_c7H7FYgF2X6nq8');
|
||||
|
||||
await client.gatewayConnect();
|
||||
|
||||
client.gateway.on('connect', () => {
|
||||
client.gateway.subscribeToCategoryChat('5fc829314e96e00725c17fd8');
|
||||
client.gateway.on('message', console.log);
|
||||
client.gateway.on('message', (e) => {
|
||||
if (e.author._id === client.user._id) return;
|
||||
client.gateway.sendMessage('5fc829314e96e00725c17fd8', e.content, {
|
||||
username: e.author.username
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue