Add guild link images

This commit is contained in:
illyum 2024-07-15 20:03:41 -06:00
parent fc7feb214f
commit c5e33775ea

122
main.js
View File

@ -70,43 +70,97 @@ function parseGuildMessage(jsonMsg) {
chatEvents.emit(eventType, guildMessage) chatEvents.emit(eventType, guildMessage)
} }
async function sendWebhookMessage(guildMsg) { function getHexFromIdentifyer(identifyer) {
const canvas = createCanvas(800, 200) let fillStyle;
const context = canvas.getContext('2d') 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 return fillStyle
context.fillStyle = '#313338' }
context.fillRect(0, 0, canvas.width, canvas.height)
context.font = '24px minecraft' async function sendWebhookMessage(guildMsg) {
const canvas = createCanvas(800, 45)
const context = canvas.getContext('2d')
context.font = '28px minecraft'
const username = guildMsg.sender.rawUsername const username = guildMsg.sender.rawUsername
const hypixelRank = guildMsg.sender.rawHypixelRank const hypixelRank = guildMsg.sender.rawHypixelRank
const guildRank = guildMsg.sender.rawGuildRank const guildRank = guildMsg.sender.rawGuildRank
const message = guildMsg.message const message = guildMsg.message
let x = 10 let x = 28
let y = 50 let y = 50
// Draw text with colors // Draw text with colors
context.fillStyle = '#FFAA00' // Example color, modify as needed let string_to_build = `${hypixelRank} ${username}`
context.fillText(hypixelRank, x, y) let index = 0
x += context.measureText(hypixelRank).width + 10 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 index++
context.fillText(username, x, y) }
x += context.measureText(username).width + 10 context.fillStyle = '#ffffff'
context.fillText(`: ${message}`, x, y)
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
const buffer = canvas.toBuffer('image/png') const buffer = canvas.toBuffer('image/png')
// Send image to Discord webhook
const form = new FormData() const form = new FormData()
form.append('file', new Blob([Buffer.from(buffer)]), 'message.png') 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 webhookUrl = 'https://discord.com/api/webhooks/1262482329671176232/yAHgxkP2JSBPpdjmz6mdABu5yRkXxatXcbzI91iAjy0jM0z1kRzE1HZqzK5OGXCmkqPk'
const webhookClient = new WebhookClient({ url: webhookUrl }); const webhookClient = new WebhookClient({ url: webhookUrl });
const chatEvents = new EventEmitter() // const chatEvents = new EventEmitter()
const bot = new ProudCircleBot('ProudCircle') // 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();