Compete with each others on how many messages you sent using this point system! Modify it to suit your needs! Make sure that you create a Google Sheets using the template: https://docs.google.com/spreadsheets/d/15Boliwwph7nspMYEGVy_MyCQY7PrbvpE1iUCIL_Z6-o/template/preview | Have fun and happy hacking!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//create a database with the following columns (case sensitive):
//ID, Username, Points. You can also refer to the googlesheets template:
//https://docs.google.com/spreadsheets/d/15Boliwwph7nspMYEGVy_MyCQY7PrbvpE1iUCIL_Z6-o/template/preview
let database = await lib.googlesheets.query['@0.3.0'].select({
//select the range of where you wanna start collecting datas from Google Sheets
range: `A:C`,
bounds: 'FULL_RANGE',
where: [
{
ID__contains: `${context.params.event.author.id}`,
},
],
limit: {
count: 0,
offset: 0,
},
});
let points = 0; //default points
if (database.rows.length !== 0) {
//if database have the user's name
points = parseInt(database.rows[0].fields['Points']) + parseInt(1); //adds 1 point to Points row each time
}
let timeout = await lib.utils.kv['@0.1.16'].get({
key: `timeout`,
defaultValue: false,
});
if (!timeout) {
//if the user is not timeout
if (database.rows.length === 0) {
//database doesn't have the user's name
await lib.googlesheets.query['@0.3.0'].insert({
//create a profile
range: `A:D`,
fieldsets: [
{
ID: `${context.params.event.author.id}`, //gets the id of user and insert into ID's row
Username: `${context.params.event.author.username}`, //gets the username and insert into Username's row
Points: `0`, //sets point to 0 for the user
},
],
});
} else {
await lib.googlesheets.query['@0.3.0'].update({
//else, update the google sheets
range: `A:D`,
bounds: 'FULL_RANGE',
where: [
{
ID__contains: `${context.params.event.author.id}`,
},
],
limit: {
count: 0,
offset: 0,
},
fields: {
Username: `${context.params.event.author.username}`,
Points: points, //updates the point
},
});
await lib.utils.kv['@0.1.16'].set({
//sets a timeout
key: `timeout`,
value: true,
ttl: 5,
});
}
}
//if you would like, you can change the prefix of the following prefix command
if (context.params.event.content.startsWith('!points')) {
//if the prefix command !points is triggered
if (database.rows.length === 0) {
//and the database doesn't have the user's info stored
await lib.discord.channels['@0.1.1'].messages.create({
//the bot sends this message
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embed: {
type: 'rich',
title: `${context.params.event.author.username}#${context.params.event.author.discriminator}'s Points:`,
description: '',
color: 0x9b21ff,
fields: [
{
name: 'Points:',
value: '0',
},
],
},
});
} else {
await lib.discord.channels['@0.1.1'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embed: {
type: 'rich',
title: `${context.params.event.author.username}#${context.params.event.author.discriminator}'s Points:`,
description: '',
color: 0x9b21ff,
fields: [
{
name: 'Points:',
value: points,
},
],
},
});
}
}
//leaderboard
if (context.params.event.content.startsWith(`!leaderboard`)) {
//if !leaderboard prefix command is triggered
let database = await lib.googlesheets.query['@0.3.0'].select({
//select the range of row A to C
range: `A:C`,
bounds: `FIRST_EMPTY_ROW`,
});
database.rows.sort((a, b) => {
//sorts the leaderboard numerically descending, from largest to smallest whereby b is largest and a is smallest.
return parseInt(b.fields.Points) - parseInt(a.fields.Points);
//you can always exchange b fields and a fields so that it sorts ascendingly from least points to most points
});
let leaderBoardFields = [];
database.rows.slice(0, 10).forEach((row) => {
let userName = row.fields.Username; //sets userName as Google Sheets' username
let userValue = `Points: ${row.fields.Points}`; //sets userValue as Points: ${gets the Points from Google Sheets Database}
leaderBoardFields.push({name: userName, value: userValue}); //push the userName and userValue to leaderboard's field
});
await lib.discord.channels['@0.1.1'].messages.create({
//sends this message when !leaderboard is triggered
channel_id: context.params.event.channel_id, //gets the channel id
content: '',
embed: {
title: '** Leaderboard **', //default title
type: 'rich',
color: 0x0000aa,
description: '',
fields: leaderBoardFields, // the pushed info will be displayed here
},
});
}