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
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>_'); }
// setWelcomeChannel(dbo, message, args[1]);
const channel = message.guild.channels.cache.find(ch => ch.name === args[1]);
dbo.updateOne({welcomechannel: {$exists: true}}, {$set: {welcomechannel: `${channel.id}`}});
} else if (command == 'welcome_message') {
dbo.updateOne({welcomemessage: {$exists: true}}, {$set: {welcomemessage: args[1]}})
}
});
+68 -47
View File
@@ -1,5 +1,8 @@
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 CanvasImport = require('@napi-rs/canvas');
@@ -7,70 +10,88 @@ const canvas = CanvasImport.createCanvas(700, 250)
const context = canvas.getContext('2d')
//https://some-random-api.ml/welcome
async function welcome(member, welcomechannel, welcomebanner = null) {
async function welcome(member, welcomechannel, welcomemessage = null, welcomebanner = null) {
//Draw Stuff
const context = canvas.getContext('2d');
var bkimgsrc;
const backgroundFile = await readFile('https://github.com/ION606/selmerBot/blob/main/commands/admin/wallpaper.jpg');
const background = new CanvasImport.Image();
background.src = backgroundFile;
let bkurl = 'https://github.com/ION606/selmerBot/blob/main/commands/admin/wallpaper.jpg';
const response = await fetch(bkurl);
response.arrayBuffer().then(async (data) => {
// const background = new CanvasImport.Image();
// background.src = arrayBufferToBuffer(data);
// This uses the canvas dimensions to stretch the image onto the entire canvas
context.drawImage(background, 0, 0, canvas.width, canvas.height);
// This uses the canvas dimensions to stretch the image onto the entire canvas
// context.drawImage(background, 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);
//Draw the Border
context.strokeStyle = '#0099ff';
context.strokeRect(0, 0, canvas.width, canvas.height);
//Add Text
//have the function here, because returns are whack
const applyText = (canvas, text) => {
const context = canvas.getContext('2d');
// Declare a base size of the font
let fontSize = 70;
do {
// Assign the font to the context and decrement it so it can be measured again
context.font = `italic ${fontSize -= 10}px sans-serif`;
// Compare pixel width of the text to the canvas minus the approximate avatar size
} while (context.measureText(text).width > canvas.width - 100);
// Return the result to use in the actual canvas
return context.font;
};
//have the function here, because returns are whack
const applyText = (canvas, text) => {
const context = canvas.getContext('2d');
// Declare a base size of the font
let fontSize = 70;
do {
// Assign the font to the context and decrement it so it can be measured again
context.font = `italic ${fontSize -= 10}px sans-serif`;
// Compare pixel width of the text to the canvas minus the approximate avatar size
} while (context.measureText(text).width > canvas.width - 100);
// Return the result to use in the actual canvas
return context.font;
};
//message.author.username == interaction.member.displayName
//message.guild.name == interaction.member.guild.name
const text = `Welcome to ${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);
//message.author.username == interaction.member.displayName
//message.guild.name == interaction.member.guild.name
let txt = 'Welcome to';
if (welcomemessage != null) { txt = welcomemessage}
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!!!
//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();
//Draw a white circle
context.beginPath();
context.arc((canvas.width/2), 90, 85, 0, 2 * Math.PI, false);
context.fillStyle = 'white';
context.fill();
context.closePath();
// Clip off the region you just drew (enforce template?)
context.clip();
//ANYTHING DRAWN AFTER THIS WILL BE CLIPPED!!!
//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)
const { body } = await request(member.displayAvatarURL({ format: 'jpg' }));
const avatar = new CanvasImport.Image();
avatar.src = Buffer.from(await body.arrayBuffer());
context.drawImage(avatar, (canvas.width/2) - 80, 10, 160, 160);
//Add the user's profile pic (message.author == interaction.user)
const { body } = await request(member.displayAvatarURL({ format: 'jpg' }));
const avatar = new CanvasImport.Image();
avatar.src = Buffer.from(await body.arrayBuffer());
context.drawImage(avatar, (canvas.width/2) - 80, 10, 160, 160);
// Use the helpful Attachment class structure to process the file for you
const attachment = new MessageAttachment(canvas.toBuffer('image/png'), 'profile-image.png');
// Use the helpful Attachment class structure to process the file for you
const attachment = new MessageAttachment(canvas.toBuffer('image/png'), 'profile-image.png');
welcomechannel.send({ files: [attachment] });
welcomechannel.send({ files: [attachment] });
})
}
module.exports = { welcome }
+2 -2
View File
@@ -69,7 +69,7 @@ bot.on("guildCreate", guild => {
if (err) { return console.log(err); }
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();
@@ -221,7 +221,7 @@ bot.on('guildMemberAdd', async (member) => {
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",
"@napi-rs/canvas": "^0.1.22",
"apt": "^0.0.2",
"arraybuffer-to-buffer": "^0.0.7",
"axios": "^0.27.2",
"canvas": "^2.9.1",
"cheerio": "^1.0.0-rc.10",
@@ -653,6 +654,11 @@
"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": {
"version": "0.2.6",
"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": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
+1
View File
@@ -4,6 +4,7 @@
"@discordjs/voice": "^0.8.0",
"@napi-rs/canvas": "^0.1.22",
"apt": "^0.0.2",
"arraybuffer-to-buffer": "^0.0.7",
"axios": "^0.27.2",
"canvas": "^2.9.1",
"cheerio": "^1.0.0-rc.10",