This snippet posts a custom image containing the person's avatar whenever someone new joins the server. Must have member intents on in developer portal.
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {registerFont, createCanvas, loadImage} = require('canvas')
// We're going to use the best font ever for our image... Comic Sans!
// Let's import it now so that we can use it in our app!
const comicsans = require('@canvas-fonts/comic-sans-ms');
registerFont(comicsans, {family: 'Comic Sans'});
// Cool, next up we need to create a canvas that we're going to draw on
// We're then going to get our Canvas' 2d context so we can draw on it
const canvas = createCanvas(400, 200)
const ctx = canvas.getContext('2d')
// Awesome, now lets fill in our canvas background with a colour of our choice
ctx.fillStyle = "#5352ed";
ctx.fillRect(0, 0, canvas.width, canvas.height)
// Time to write some text to welcome our user
ctx.font = 'bold 30px "Comic Sans"';
ctx.fillStyle = '#FFFFFF';
ctx.textAlign = 'center'
ctx.fillText(`Welcome`, 200, 140);
ctx.fillText(`${context.params.event.user.username}!`, 200, 165)
// Okay, finally we want to overlay the user's profile picture onto the canvas too
// First up, let's grab their user info
// Shout out to MEIRABA's snippet for inspiring this code!
let user = await lib.discord.users['@0.1.5'].retrieve({
user_id: context.params.event.user.id,
});
// Next up, lets grab their avatar URL
// If they don't have an avatar, we'll use a default one
let avatar_url = user.avatar
? `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`
: `https://discordapp.com/assets/322c936a8c8be1b803cd94861bdfa868.png`;
// Then we'll download the image into Autocode
// Once it's downloaded we'll overlay it onto our welcome message
let image = await loadImage(avatar_url);
ctx.drawImage(image, 150, 25, 70, 70);
// And finally we'll post our image into our channel whenever someone joins!
// You should set the channel ID to whatever is correct for you!
return lib.discord.channels['@0.2.1'].messages.create({
channel_id: `000000000000000000`, // set this to your channel ID!
content: ``,
file: canvas.toBuffer(),
filename: 'welcome.png',
});