Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 79 additions & 21 deletions Minecraft/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A while true loop hangs the program I think

if (!sending) {
bot.write('chat_message', {
message: message.author.tag + ' > ' + messageQueue[0],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this with messageQueue.shift()

timestamp: BigInt(Date.now()),
salt: 0,
signature: Buffer.alloc(0),
signedPreview: false,
previousMessages: [],
lastMessage: null
}); // Send the message to minecraft chat

messageQueue.shift();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then remove this

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
});
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.