In this snippet you can set up an awesome achievement system for your bot! There are 2 commands that will give you achievements on the first time you use them, there is a command to view if you have done those achievements yet and a command to reset them. The commands are !help, !random, and !achievements. The ! will change based on what you entered for the env variable.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const jimp = require('jimp');
let botName = process.env.botname;
let event = context.params.event;
let member = event.author.id;
//Text that will go onto the first achievement
let achievement1 = `Commands!`;
let achievement1Text = `Use ${botName}'s Help command!`;
//Text that will go onto the second achievement
let achievement2 = `Randomizer King!!`;
let achievement2Text = `Use the random color command!`;
//The first command, trigger is [YOUR-PREFIX]help
if (event.content.startsWith(`${process.env.prefix}help`)) {
//Checks if a user has used the help command
let usedHelpBefore = await lib.utils.kv['@0.1.16'].get({
key: `UsedHelp_${member}`,
defaultValue: false,
});
//If a user has not used the help command it will trigger this which will make the achievement happen
if (usedHelpBefore === false) {
//All the jimp stuff that makes the achievment card
let user = await lib.discord.users['@0.1.4'].retrieve({
user_id: member,
});
let img = await jimp.read(
'https://cdn.discordapp.com/attachments/898724881015906325/929452959606124564/blankAchievement.jpg'
);
let avatar_jimp = await jimp.read(user.avatar_url);
avatar_jimp.circle();
avatar_jimp.resize(400, 400);
img.composite(avatar_jimp, 34, 50);
await jimp.loadFont(jimp.FONT_SANS_128_WHITE).then((font) => {
img.print(
font,
700,
115,
{
//Can be changed on line 9
text: `${achievement1}`,
},
900,
260
);
});
await jimp.loadFont(jimp.FONT_SANS_64_WHITE).then((font) => {
img.print(
font,
600,
250,
{
//Can be changed on line 10
text: `${achievement1Text}`,
},
900,
290
);
});
let buffer = await img.getBufferAsync(jimp.MIME_PNG);
//Sends the achievement card
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
filename: 'helpcmdAchievment.png',
file: buffer,
});
//Tells the code that the user has now used the commands and to not send it again
await lib.utils.kv['@0.1.16'].set({
key: `UsedHelp_${member}`,
value: true,
});
//Counts how many achievements the user has (this snippet only has 2)
let startCount = 0;
//Gets the current count, if there isn't one it says 0
let count = await lib.utils.kv['@0.1.16'].get({
key: `achievmentCount?_${member}`,
defaultValue: startCount,
});
//Sets the new count to the current one + 1
await lib.utils.kv['@0.1.16'].set({
key: `achievmentCount?_${member}`,
value: count + 1,
});
//Sends the help command after the achievement has been sent
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ``,
embeds: [
{
type: `rich`,
title: `${botName}'s Commands`,
description: `Here are all of my commands!\n\n\`Command1\`, \`Command2\``,
color: 0x00ffff,
},
],
});
//If they have used the command before it will just send the help command
} else if (usedHelpBefore === true) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ``,
embeds: [
{
type: `rich`,
title: `${botName}'s Commands`,
description: `Here are all of my commands!\n\n\`Command1\`, \`Command2\``,
color: 0x00ffff,
},
],
});
}
}
//First command done
//If you say [YOUR-PREFIX]random it will trigger this command
if (event.content.startsWith(`${process.env.prefix}random`)) {
//Checks if the user has used the command before
let UsedBeforeRandom = await lib.utils.kv['@0.1.16'].get({
key: `hasUsedRandom_${member}`,
defaultValue: false,
});
//If they have not used the command before it will trigger this which will make the achievement card
if (UsedBeforeRandom === false) {
//All of the jimp stuff to make the card
let user = await lib.discord.users['@0.1.4'].retrieve({
user_id: member,
});
let img = await jimp.read(
'https://cdn.discordapp.com/attachments/898724881015906325/929452959606124564/blankAchievement.jpg'
);
let avatar_jimp = await jimp.read(user.avatar_url);
avatar_jimp.circle();
avatar_jimp.resize(400, 400);
img.composite(avatar_jimp, 34, 50);
await jimp.loadFont(jimp.FONT_SANS_128_WHITE).then((font) => {
img.print(
font,
600,
115,
{
//Can be changed on line 13
text: `${achievement2}`,
},
1100,
260
);
});
await jimp.loadFont(jimp.FONT_SANS_64_WHITE).then((font) => {
img.print(
font,
600,
250,
{
//Can be changed on line 14
text: `${achievement2Text}`,
},
1000,
290
);
});
let buffer = await img.getBufferAsync(jimp.MIME_PNG);
//Sends the achievement card that was sent through the jimp machine
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: event.channel_id,
content: ``,
filename: 'randomColorAchievment.png',
file: buffer,
});
//Tells the code that the user has used the command before
await lib.utils.kv['@0.1.16'].set({
key: `hasUsedRandom_${member}`,
value: true,
});
//Checks how many achievements the user has done (this snippet only has 2)
let startCount = 0;
let count = await lib.utils.kv['@0.1.16'].get({
key: `achievmentCount?_${member}`,
defaultValue: startCount,
});
//Adds +1 to the current count
await lib.utils.kv['@0.1.16'].set({
key: `achievmentCount?_${member}`,
value: count + 1,
});
//Starts the actual command
//Color options
let colorOptions = [0x00ffff, 0x000000, 0xffffff, 0x123456];
//Randomly picks a color
let selectedColor = Math.floor(Math.random() * colorOptions.length);
let color = colorOptions[selectedColor];
//The command that is sent after the achievement
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ``,
embeds: [
{
type: `rich`,
title: `Random Color`,
description: `Your random color is..........\n\n\`${color}\``,
color: color,
},
],
});
//If the user has used the command before then it will trigger just the command not the achievement
} else if (UsedBeforeRandom === true) {
let colorOptions = [0x00ffff, 0x000000, 0xffffff, 0x123456];
let selectedColor = Math.floor(Math.random() * colorOptions.length);
let color = colorOptions[selectedColor];
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ``,
embeds: [
{
type: `rich`,
title: `Random Color`,
description: `Your random color is..........\n\n\`${color}\``,
color: color,
},
],
});
}
}
//Command 2 done
if (event.content.startsWith(`${process.env.prefix}achievements`)) {
//Gets the current count of achievements completed
let count = await lib.utils.kv['@0.1.16'].get({
key: `achievmentCount?_${member}`,
defaultValue: 0,
});
//Checks if a user has used the help command
let helpCmd = await lib.utils.kv['@0.1.16'].get({
key: `UsedHelp_${member}`,
defaultValue: false,
});
//If the have not used the command it will trigger this
if (helpCmd === false) {
//Sets the check to Incomplete
let helpCmdCheck = `Incomplete!`;
//Saves the value in kv so that it can escape the if statement
await lib.utils.kv['@0.1.16'].set({
key: `helpCmdChecking_${member}`,
value: `${helpCmdCheck}`,
});
//If they have used the command it will trigger this
} else if (helpCmd === true) {
//Sets the check to Complete
let helpCmdCheck = `Complete!`;
//Saves the value in kv so that it can be free from the if statement
await lib.utils.kv['@0.1.16'].set({
key: `helpCmdChecking_${member}`,
value: `${helpCmdCheck}`,
});
}
//Gets the kv value that was saved inside of the if statement for help command
let helpCmdCheck = await lib.utils.kv['@0.1.16'].get({
key: `helpCmdChecking_${member}`,
});
//Checks if the user has used the random command
let usedRandom = await lib.utils.kv['@0.1.16'].get({
key: `hasUsedRandom_${member}`,
defaultValue: false,
});
//If the user has not used the command it triggers this
if (usedRandom === false) {
//Sets the value to Incomplete
let usedRandomChecking = `Incomplete!`;
//Saves the value in kv so that is can be free of the if statement
await lib.utils.kv['@0.1.16'].set({
key: `usedRandomChecking_${member}`,
value: `${usedRandomChecking}`,
});
//If the user has used the command it will trigger this
} else if (usedRandom === true) {
//Sets the value to Complete
let usedRandomChecking = `Complete!`;
//Saves the value in kv so that it can escape the if statement
await lib.utils.kv['@0.1.16'].set({
key: `usedRandomChecking_${member}`,
value: `${usedRandomChecking}`,
});
}
//Gets the value that was made in the if statement
let usedRandomCheck = await lib.utils.kv['@0.1.16'].get({
key: `usedRandomChecking_${member}`,
});
//Makes the message
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: ``,
embeds: [
{
type: `rich`,
title: `${event.author.username}'s Achievments`,
description: `Achievement Count: ${count}/2`,
color: 0x123456,
fields: [
{
name: `${achievement1}`,
value: `${achievement1Text}\n\nStatus: \`${helpCmdCheck}\``,
inline: true,
},
{
name: `${achievement2}`,
value: `${achievement2Text}\n\nStatus: \`${usedRandomCheck}\``,
inline: true,
},
],
},
],
});
}
//Command that resets the kv values
if (event.content.startsWith(`${process.env.prefix}reset`)) {
await lib.utils.kv['@0.1.16'].clear({
key: `UsedHelp_${member}`,
});
await lib.utils.kv['@0.1.16'].clear({
key: `hasUsedRandom_${member}`,
});
await lib.utils.kv['@0.1.16'].clear({
key: `achievmentCount?_${member}`,
});
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: `I've reset the achievments!`,
});
}
//This command will correct you if you spell achievements wrong,
//Why? Because I spelt achievements wrong multiple times in testing and that set me back multiple steps, multiple times
if (
event.content.match(
/\bacheivements\b|\bachievments\b|\bachievemants\b|\bacheivmants\b/i
)
) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${event.channel_id}`,
content: `Hey! It looks like you spelled achievements wrong, ||noob||, ||jkjk||\n\nYour command is \`${process.env.prefix}achievements\``,
});
}