add attachments and reply support to minecraft bridge

This commit is contained in:
hippoz 2022-02-16 14:02:37 +02:00
parent 13ff55efe0
commit b9c25ec3e6
No known key found for this signature in database
GPG key ID: 7C52899193467641

View file

@ -148,14 +148,44 @@ async function sendBridgeMessageAs(guildId, channelId, content, username=undefin
});
}
async function sendMinecraftMessageAs(rcon, username, content) {
rcon.send(`tellraw @a ${JSON.stringify([
async function sendMinecraftMessageAs(rcon, username, content, attachments=[], referencedMessage=null) {
const tellrawPayload = [
{ text: "[" },
{ text: `${username}`, color: "gray" },
{ text: "]" },
{ text: " " },
{ text: content },
])}`);
];
if (referencedMessage) {
let trimmedContent = referencedMessage.content.substring(0, 50);
if (trimmedContent !== referencedMessage.content) {
trimmedContent += "...";
}
tellrawPayload.push({
text: `<replying to ${referencedMessage.author.username}: ${trimmedContent}>`
});
tellrawPayload.push({
text: " "
});
}
attachments.forEach((e) => {
tellrawPayload.push({
text: `<open attachment: ${e.filename || "[unknown]"}>`,
color: "gray",
clickEvent: {
action: "open_url",
value: e.proxy_url
}
});
tellrawPayload.push({
text: " "
});
});
tellrawPayload.push({ text: content });
rcon.send(`tellraw @a ${JSON.stringify(tellrawPayload)}`);
}
async function main() {
@ -171,7 +201,7 @@ async function main() {
const gateway = new GatewayClient(GATEWAY_ORIGIN);
gateway.onEvent = (e) => {
if (e.eventType === "MESSAGE_CREATE" && e.message.channel_id === TARGET_CHANNEL_ID && e.message.guild_id === TARGET_GUILD_ID && !e.message.webhook_id) {
sendMinecraftMessageAs(rconConnection, e.message.author.username, e.message.content);
sendMinecraftMessageAs(rconConnection, e.message.author.username, e.message.content, e.message.attachments, e.message.referenced_message);
}
};
rconConnection.connect();