A work command with cooldown for Starblaster's Economy App. You will need to make a copy of this google sheet if you havent done so already: https://docs.google.com/spreadsheets/d/1eetqNHt6wa0_XOUhHloKP-B9krikpshk1IqEPigSKCY/template/preview
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const working = Math.floor(Math.random() * 1420);
let workRandom = [
`at Apple \`๐\``,
`at McDonalds \`๐\``,
`as a Cleaner \`๐งน\``,
`as a Discord Bot Dev \`๐พ\``,
`as a Mobile phone technician \`๐ฑ\``,
];
module.exports = async (event, context) => {
let workChoice = Math.floor(Math.random() * workRandom.length);
let work = workRandom[workChoice];
let userId =
context.params.event.mentions[0]?.id || context.params.event.author.id;
let trigger = await lib.discord.users['@release'].retrieve({
user_id: context.params.event.author.id,
});
let member = await lib.discord.guilds['@release'].members.retrieve({
user_id: userId,
guild_id: context.params.event.guild_id,
});
let database = await lib.googlesheets.query['@0.3.0'].distinct({
range: `A:E`,
bounds: `FIRST_EMPTY_ROW`,
where: [
{
user_id__is: event.author.id,
},
],
field: `money`,
});
if (!database.distinct.values[0]) {
await lib.discord.channels['@0.3.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
message_reference: {message_id: context.params.event.id},
tts: false,
embeds: [
{
type: 'rich',
title: ``,
description: `**You dont have an account**\n\n**Type** \`!createbank\` **to open one.**`,
color: 0x64005b,
},
],
});
return;
}
let cooldown_time = 60; // You can change this for a different cooldown time
let wcooldown = await lib.utils.kv['@0.1.16'].get({
key: `${context.params.event.author.id}_wcooldown`,
});
if (wcooldown) {
let remaining_time = Math.floor(
parseInt(wcooldown + cooldown_time * 1000) - new Date().getTime()
);
let time_sec = Math.floor(remaining_time / 1000);
await lib.discord.channels['@0.3.1'].messages.create({
channel_id: context.params.event.channel_id,
content: '',
message_reference: {message_id: context.params.event.id},
tts: false,
embeds: [
{
type: 'rich',
title: '',
description: `You can work again in \`${time_sec}sec\``,
color: 0x64005b, // You can change the color
author: {
name: `Please wait to work again..`,
icon_url: member.user.avatar_url,
},
footer: {
text: `The default cooldown is 60s`,
},
},
],
});
} else {
await lib.utils.kv['@0.1.16'].set({
key: `${context.params.event.author.id}_wcooldown`,
value: new Date().getTime(),
ttl: cooldown_time,
});
let updatedSheet = await lib.googlesheets.query['@0.3.0'].update({
range: `A:E`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
user_id__is: event.author.id,
},
],
fields: {
money: parseInt(database.distinct.values[0]) + working,
},
});
await lib.discord.channels['@0.3.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
message_reference: {message_id: context.params.event.id},
tts: false,
embeds: [
{
type: 'rich',
title: '',
description: `You worked ${work}
**You Earned:** ${working}
**Current balance**: ${updatedSheet.rows[0].fields['money']}`,
color: 0x64005b, // You can change the color
author: {
name: ``,
icon_url: member.user.avatar_url,
},
footer: {
text: `Nice work`,
},
},
],
});
}
};