diff --git a/Minecraft/Chat.ts b/Minecraft/Chat.ts index efaf233..3a90db3 100644 --- a/Minecraft/Chat.ts +++ b/Minecraft/Chat.ts @@ -33,7 +33,7 @@ export function handleChat(client: DiscordClient) { const { id } = hoverEvent?.contents || {}; const player = bot.playerManager.getPlayerByUUID(id); - if(username === client.bot.username) return; + if (username === client.bot.username) return; if ( client.config['in-game-bot'].muted.find( (entry) => entry.name === username @@ -47,24 +47,31 @@ export function handleChat(client: DiscordClient) { case 'multiplayer.player.joined': if (color !== 'yellow' || !translate || !username || !id) return; - + client.sendEmbedMessage( client.config.guild.channels.relay_channel, username, `${username} has joined the game.`, - player?.getHeadURL() || client.config.constants.defaultProfile, + player?.getHeadURL() || + client.config.constants.defaultProfile, '#00ff00' ); break; case 'multiplayer.player.left': if (color !== 'yellow' || !translate || !raw.with) return; - if (client.config['in-game-bot'].muted.find((entry) => entry.name === raw?.with[0]?.text)) return; + if ( + client.config['in-game-bot'].muted.find( + (entry) => entry.name === raw?.with[0]?.text + ) + ) + return; client.sendEmbedMessage( client.config.guild.channels.relay_channel, raw?.with[0]?.text, `${raw?.with[0]?.text} has left the game.`, - player?.getHeadURL() || client.config.constants.defaultProfile, + player?.getHeadURL() || + client.config.constants.defaultProfile, '#9d3838' ); break; @@ -74,34 +81,85 @@ export function handleChat(client: DiscordClient) { client.config.guild.channels.relay_channel, `Sleeper count has changed!`, `There are now ${raw.with.join('/')} players sleeping.`, - player?.getHeadURL() || client.config.constants.defaultProfile, + player?.getHeadURL() || + client.config.constants.defaultProfile, '#c2c5cc' ); break; } }); + let messageQueue: string[] = []; + let sending = false; // Detects when a player sends a message in discord channel client.on('messageCreate', (message) => { if (message.author.bot) return; // Ignore messages from bot if (message.channel.id !== client.config.guild.channels.relay_channel) // Ignore messages from other channels return; - if (message.content.length > 255) { - // Ignore messages longer than the MC chat limit | Temporary - // TODO: Split long messages into multiple and send with delay! - message.react('❌') - return; + + // Split long messages to prevent kick from mc server + if (message.content.length > 255 - message.author.tag.length - 3) { + // 3 is to account for the " > " in the sent message + let originalMessage = message.content; + let splitMessage: string[] = []; + + // Repeat while the message is too long (I use 250 to account for the index of the split message added to the beggining of the message) + let j = 0; + while ( + originalMessage.length > + 250 - message.author.tag.length - 3 + ) { + // Find the space to split the message at + let i = 250 - message.author.tag.length - 3; + while (originalMessage[i] !== ' ' || i > 50) { + i--; + } + + // If there was no space found, split close to the 255 character limit + if (i <= 50 || originalMessage[i] !== ' ') { + splitMessage.push( + `[${j}] ` + + originalMessage.slice( + 0, + 250 - message.author.tag.length - 3 + ) + ); + originalMessage = originalMessage.slice( + 250 - message.author.tag.length - 3 + ); + } + // If there was a space found, split at the space + else { + splitMessage.push(`[${j}] ` + originalMessage.slice(0, i)); + originalMessage = originalMessage.slice(i + 1); + } + j++; + } + + // Add the split messages to the queue + messageQueue = messageQueue.concat(splitMessage); + } else { + // Add current message to queue + messageQueue.push(message.content); + } + + while (messageQueue.length > 0) { + if (!sending) { + bot.write('chat_message', { + message: message.author.tag + ' > ' + messageQueue[0], + timestamp: BigInt(Date.now()), + salt: 0, + signature: Buffer.alloc(0), + signedPreview: false, + previousMessages: [], + lastMessage: null + }); // Send the message to minecraft chat + + messageQueue.shift(); + sending = true; + setTimeout(() => (sending = false), 1000); + } } - - bot.write('chat_message', { - message: message.author.tag + ' > ' + message.content, - timestamp: BigInt(Date.now()), - salt: 0, - signature: Buffer.alloc(0), - signedPreview: false, - previousMessages: [], - lastMessage: null - }); // Send the message to minecraft chat }); } diff --git a/package-lock.json b/package-lock.json index 31ab263..686766b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4772,7 +4772,7 @@ }, "minecraft-protocol": { "version": "git+ssh://git@github.com/jtsiskin/node-minecraft-protocol.git#47cb103354d010339f6e58caf26930cc98322879", - "from": "minecraft-protocol@https://github.com/jtsiskin/node-minecraft-protocol.git#47cb103", + "from": "minecraft-protocol@github:jtsiskin/node-minecraft-protocol#47cb103", "requires": { "@types/readable-stream": "^2.3.13", "aes-js": "^3.1.2",