diff --git a/main.js b/main.js index 9dfc07f..b4b0c8d 100644 --- a/main.js +++ b/main.js @@ -70,43 +70,97 @@ function parseGuildMessage(jsonMsg) { chatEvents.emit(eventType, guildMessage) } -async function sendWebhookMessage(guildMsg) { - const canvas = createCanvas(800, 200) - const context = canvas.getContext('2d') +function getHexFromIdentifyer(identifyer) { + let fillStyle; + switch (identifyer) { + case '0': + fillStyle = '#000000' // Black + break + case '1': + fillStyle = '#0000AA' // Dark Blue + break + case '2': + fillStyle = '#00AA00' // Dark Green + break + case '3': + fillStyle = '#00AAAA' // Dark Aqua + break + case '4': + fillStyle = '#AA0000' // Dark Red + break + case '5': + fillStyle = '#AA00AA' // Dark Purple + break + case '6': + fillStyle = '#FFAA00' // Gold + break + case '7': + fillStyle = '#AAAAAA' // Gray + break + case '8': + fillStyle = '#555555' // Dark Gray + break + case '9': + fillStyle = '#5555FF' // Blue + break + case 'a': + fillStyle = '#55FF55' // Green + break + case 'b': + fillStyle = '#55FFFF' // Aqua + break + case 'c': + fillStyle = '#FF5555' // Red + break + case 'd': + fillStyle = '#FF55FF' // Light Purple + break + case 'e': + fillStyle = '#FFFF55' // Yellow + break + case 'f': + fillStyle = '#FFFFFF' // White + break + default: + fillStyle = '#000000' // Default to Black if no match + } - // Fill background - context.fillStyle = '#313338' - context.fillRect(0, 0, canvas.width, canvas.height) - context.font = '24px minecraft' + return fillStyle +} + +async function sendWebhookMessage(guildMsg) { + const canvas = createCanvas(800, 45) + const context = canvas.getContext('2d') + context.font = '28px minecraft' const username = guildMsg.sender.rawUsername const hypixelRank = guildMsg.sender.rawHypixelRank const guildRank = guildMsg.sender.rawGuildRank const message = guildMsg.message - let x = 10 + let x = 28 let y = 50 // Draw text with colors - context.fillStyle = '#FFAA00' // Example color, modify as needed - context.fillText(hypixelRank, x, y) - x += context.measureText(hypixelRank).width + 10 + let string_to_build = `${hypixelRank} ${username}` + let index = 0 + let fillStyle = '#AAAAAA' + while (index < string_to_build.length) { + if (string_to_build.charAt(index) === '§') { + fillStyle = getHexFromIdentifyer(string_to_build.charAt(index + 1)) + index++ + index++ + } + context.fillStyle = fillStyle + context.fillText(string_to_build.charAt(index), x, y) + x += context.measureText(string_to_build.charAt(index)).width // + 10 - context.fillStyle = '#55FF55' // Example color, modify as needed - context.fillText(username, x, y) - x += context.measureText(username).width + 10 - - context.fillStyle = '#AAAAFF' // Example color, modify as needed - context.fillText(guildRank, x, y) - x += context.measureText(guildRank).width + 10 - - context.fillStyle = '#FFFFFF' // Example color, modify as needed - context.fillText(message, x, y) - - // Convert canvas to buffer + index++ + } + context.fillStyle = '#ffffff' + context.fillText(`: ${message}`, x, y) + const buffer = canvas.toBuffer('image/png') - - // Send image to Discord webhook const form = new FormData() form.append('file', new Blob([Buffer.from(buffer)]), 'message.png') @@ -201,6 +255,20 @@ registerFont('fonts/Minecraftia-Regular.ttf', {family: 'Minecraft'}) const webhookUrl = 'https://discord.com/api/webhooks/1262482329671176232/yAHgxkP2JSBPpdjmz6mdABu5yRkXxatXcbzI91iAjy0jM0z1kRzE1HZqzK5OGXCmkqPk' const webhookClient = new WebhookClient({ url: webhookUrl }); -const chatEvents = new EventEmitter() -const bot = new ProudCircleBot('ProudCircle') +// const chatEvents = new EventEmitter() +// const bot = new ProudCircleBot('ProudCircle') +const test_str = '§b[MVP§0++§b] illyum §e[STAFF]' +const guildMsgSender = new GuildMessageSender('illyum', '§b[MVP§0++§b]', '§e[STAFF]') +const guildMessage = new GuildMessage('This is a test!', guildMsgSender) + +async function main() { + try { + await sendWebhookMessage(guildMessage); + // Additional async calls can be made here + } catch (error) { + console.error(error); + } +} + +main();