Added welcome message image url error workaround and added the ability to chose a custom welcome message'

This commit is contained in:
ION606
2022-06-10 20:48:43 +03:00
parent 5cb58bbfe0
commit d881aaf14b
5 changed files with 89 additions and 50 deletions
+7 -1
View File
@@ -36,11 +36,17 @@ async function execute(bot, message, args, command, Discord, mongouri, items, xp
//Chose the appropriate command //Chose the appropriate command
command = args[0]; command = args[0];
if (command == 'welcomechannel') { if (args[1] == undefined) {
return message.reply("Please specify what the new value is!");
}
if (command == 'welcome_channel') {
if (args.length != 2) { return message.reply('The command format is _!setup welcomechannel <channel name>_'); } if (args.length != 2) { return message.reply('The command format is _!setup welcomechannel <channel name>_'); }
// setWelcomeChannel(dbo, message, args[1]); // setWelcomeChannel(dbo, message, args[1]);
const channel = message.guild.channels.cache.find(ch => ch.name === args[1]); const channel = message.guild.channels.cache.find(ch => ch.name === args[1]);
dbo.updateOne({welcomechannel: {$exists: true}}, {$set: {welcomechannel: `${channel.id}`}}); dbo.updateOne({welcomechannel: {$exists: true}}, {$set: {welcomechannel: `${channel.id}`}});
} else if (command == 'welcome_message') {
dbo.updateOne({welcomemessage: {$exists: true}}, {$set: {welcomemessage: args[1]}})
} }
}); });
+65 -44
View File
@@ -1,5 +1,8 @@
const { MessageAttachment } = require('discord.js'); const { MessageAttachment } = require('discord.js');
const { readFile } = require('fs/promises'); // const { readFile } = require('fs/promises');
const fs = require("fs");
const fetch = require('node-fetch');
const arrayBufferToBuffer = require('arraybuffer-to-buffer');
const { request } = require('undici'); const { request } = require('undici');
const CanvasImport = require('@napi-rs/canvas'); const CanvasImport = require('@napi-rs/canvas');
@@ -7,70 +10,88 @@ const canvas = CanvasImport.createCanvas(700, 250)
const context = canvas.getContext('2d') const context = canvas.getContext('2d')
//https://some-random-api.ml/welcome //https://some-random-api.ml/welcome
async function welcome(member, welcomechannel, welcomebanner = null) { async function welcome(member, welcomechannel, welcomemessage = null, welcomebanner = null) {
//Draw Stuff //Draw Stuff
const context = canvas.getContext('2d'); const context = canvas.getContext('2d');
var bkimgsrc;
const backgroundFile = await readFile('https://github.com/ION606/selmerBot/blob/main/commands/admin/wallpaper.jpg'); let bkurl = 'https://github.com/ION606/selmerBot/blob/main/commands/admin/wallpaper.jpg';
const background = new CanvasImport.Image(); const response = await fetch(bkurl);
background.src = backgroundFile; response.arrayBuffer().then(async (data) => {
// This uses the canvas dimensions to stretch the image onto the entire canvas // const background = new CanvasImport.Image();
context.drawImage(background, 0, 0, canvas.width, canvas.height); // background.src = arrayBufferToBuffer(data);
//Draw the Border // This uses the canvas dimensions to stretch the image onto the entire canvas
context.strokeStyle = '#0099ff'; // context.drawImage(background, 0, 0, canvas.width, canvas.height);
context.strokeRect(0, 0, canvas.width, canvas.height); context.fillStyle = 'rgba(0,0,0,1)';
context.fillRect(0,0, canvas.width, canvas.height);
//Draw the Border
context.strokeStyle = '#0099ff';
context.strokeRect(0, 0, canvas.width, canvas.height);
//Add Text //Add Text
//have the function here, because returns are whack //have the function here, because returns are whack
const applyText = (canvas, text) => { const applyText = (canvas, text) => {
const context = canvas.getContext('2d'); const context = canvas.getContext('2d');
// Declare a base size of the font // Declare a base size of the font
let fontSize = 70; let fontSize = 70;
do { do {
// Assign the font to the context and decrement it so it can be measured again // Assign the font to the context and decrement it so it can be measured again
context.font = `italic ${fontSize -= 10}px sans-serif`; context.font = `italic ${fontSize -= 10}px sans-serif`;
// Compare pixel width of the text to the canvas minus the approximate avatar size // Compare pixel width of the text to the canvas minus the approximate avatar size
} while (context.measureText(text).width > canvas.width - 100); } while (context.measureText(text).width > canvas.width - 100);
// Return the result to use in the actual canvas // Return the result to use in the actual canvas
return context.font; return context.font;
}; };
//message.author.username == interaction.member.displayName //message.author.username == interaction.member.displayName
//message.guild.name == interaction.member.guild.name //message.guild.name == interaction.member.guild.name
const text = `Welcome to ${member.guild.name} ${member.user.username}#${member.user.discriminator}!`; let txt = 'Welcome to';
context.font = applyText(canvas, text); if (welcomemessage != null) { txt = welcomemessage}
context.fillStyle = '#ffffff';
context.fillText(text, (canvas.width/2) - (text.length * 7.5), canvas.height - 15); const text = `${txt} ${member.guild.name} ${member.user.username}#${member.user.discriminator}!`;
context.font = applyText(canvas, text);
context.fillStyle = '#ffffff';
context.fillText(text, (canvas.width/2) - (text.length * 7.5), canvas.height - 15);
//ANYTHING DRAWN AFTER THIS WILL BE CLIPPED!!! //Draw a white circle
//Make whatever image will be draw (the user's avatar) into a circular format context.beginPath();
context.beginPath(); context.arc((canvas.width/2), 90, 85, 0, 2 * Math.PI, false);
context.arc((canvas.width/2), 90, 80, 0, Math.PI * 2, true); context.fillStyle = 'white';
context.closePath(); context.fill();
context.closePath();
// Clip off the region you just drew (enforce template?) //ANYTHING DRAWN AFTER THIS WILL BE CLIPPED!!!
context.clip(); //Make whatever image will be draw (the user's avatar) into a circular format
context.beginPath();
context.arc((canvas.width/2), 90, 80, 0, Math.PI * 2, true);
context.closePath();
// Clip off the region you just drew (enforce template?)
context.clip();
//Add the user's profile pic (message.author == interaction.user) //Add the user's profile pic (message.author == interaction.user)
const { body } = await request(member.displayAvatarURL({ format: 'jpg' })); const { body } = await request(member.displayAvatarURL({ format: 'jpg' }));
const avatar = new CanvasImport.Image(); const avatar = new CanvasImport.Image();
avatar.src = Buffer.from(await body.arrayBuffer()); avatar.src = Buffer.from(await body.arrayBuffer());
context.drawImage(avatar, (canvas.width/2) - 80, 10, 160, 160); context.drawImage(avatar, (canvas.width/2) - 80, 10, 160, 160);
// Use the helpful Attachment class structure to process the file for you // Use the helpful Attachment class structure to process the file for you
const attachment = new MessageAttachment(canvas.toBuffer('image/png'), 'profile-image.png'); const attachment = new MessageAttachment(canvas.toBuffer('image/png'), 'profile-image.png');
welcomechannel.send({ files: [attachment] }); welcomechannel.send({ files: [attachment] });
})
} }
module.exports = { welcome } module.exports = { welcome }
+2 -2
View File
@@ -69,7 +69,7 @@ bot.on("guildCreate", guild => {
if (err) { return console.log(err); } if (err) { return console.log(err); }
const dbo = client.db(guild.id).collection('SETUP'); const dbo = client.db(guild.id).collection('SETUP');
dbo.insertMany([{_id: 'WELCOME', 'welcomechannel': null, 'welcomebanner': null}]); dbo.insertMany([{_id: 'WELCOME', 'welcomechannel': null, 'welcomemessage': null, 'welcomebanner': null}]);
}); });
client.close(); client.close();
@@ -221,7 +221,7 @@ bot.on('guildMemberAdd', async (member) => {
return console.log('No welcome channel detected'); return console.log('No welcome channel detected');
} }
await welcome(member, welcomechannel, docs[0].welcomebanner); await welcome(member, welcomechannel, docs[0].welcomemessage);
}) })
}) })
}); });
+11
View File
@@ -13,6 +13,7 @@
"@discordjs/voice": "^0.8.0", "@discordjs/voice": "^0.8.0",
"@napi-rs/canvas": "^0.1.22", "@napi-rs/canvas": "^0.1.22",
"apt": "^0.0.2", "apt": "^0.0.2",
"arraybuffer-to-buffer": "^0.0.7",
"axios": "^0.27.2", "axios": "^0.27.2",
"canvas": "^2.9.1", "canvas": "^2.9.1",
"cheerio": "^1.0.0-rc.10", "cheerio": "^1.0.0-rc.10",
@@ -653,6 +654,11 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/arraybuffer-to-buffer": {
"version": "0.0.7",
"resolved": "https://registry.npmjs.org/arraybuffer-to-buffer/-/arraybuffer-to-buffer-0.0.7.tgz",
"integrity": "sha512-WAIA2Mq+KLJ7Ua40KD6zMshvSsJbnXRuVG0/MNEIPhIMEWRkcmLMcQvx0OkAeMIZi2jHJOXxK9ZqVJ+A+Z6knw=="
},
"node_modules/asn1": { "node_modules/asn1": {
"version": "0.2.6", "version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
@@ -3823,6 +3829,11 @@
} }
} }
}, },
"arraybuffer-to-buffer": {
"version": "0.0.7",
"resolved": "https://registry.npmjs.org/arraybuffer-to-buffer/-/arraybuffer-to-buffer-0.0.7.tgz",
"integrity": "sha512-WAIA2Mq+KLJ7Ua40KD6zMshvSsJbnXRuVG0/MNEIPhIMEWRkcmLMcQvx0OkAeMIZi2jHJOXxK9ZqVJ+A+Z6knw=="
},
"asn1": { "asn1": {
"version": "0.2.6", "version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
+1
View File
@@ -4,6 +4,7 @@
"@discordjs/voice": "^0.8.0", "@discordjs/voice": "^0.8.0",
"@napi-rs/canvas": "^0.1.22", "@napi-rs/canvas": "^0.1.22",
"apt": "^0.0.2", "apt": "^0.0.2",
"arraybuffer-to-buffer": "^0.0.7",
"axios": "^0.27.2", "axios": "^0.27.2",
"canvas": "^2.9.1", "canvas": "^2.9.1",
"cheerio": "^1.0.0-rc.10", "cheerio": "^1.0.0-rc.10",