Starboard system, once enough stars on a message it gets send to the starboard channel for all to see how good the message is. Use this sheets => https://docs.google.com/spreadsheets/d/1himAZNXX-f30qXBBsb5b-zdnUIXbYdc-JhSRWl_XH9c/edit?usp=sharing.
// Sheets Example Link: https://docs.google.com/spreadsheets/d/1himAZNXX-f30qXBBsb5b-zdnUIXbYdc-JhSRWl_XH9c/edit?usp=sharing
// 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 event = context.params.event;
// Boolean variable for custom emoji or not.
const isCustomEmoji = !!event.emoji.id;
// Emoji variable.
const emoji = isCustomEmoji
? `<:${event.emoji.name}:${event.emoji.id}>`
: event.emoji.name;
// Stop if the reaction is the wrong emoji.
if (event.emoji.name !== process.env.STAR_EMOJI) return;
// Retrieve the reactions for the amount.
const starReactions = await lib.discord.channels[
'@0.3.1'
].messages.reactions.list({
emoji: process.env.STAR_EMOJI,
message_id: event.message_id,
channel_id: event.channel_id,
limit: 100,
});
// Retrieve the message.
const message = await lib.discord.channels['@0.3.1'].messages.retrieve({
message_id: event.message_id,
channel_id: event.channel_id,
});
// Filter out the reactions from the message author.
const filteredStarReactions = starReactions.filter(
(reaction) => reaction.id !== message.author.id
);
// Author avatar url.
const authorAvatar = message.author.avatar
? `https://cdn.discordapp.com/avatars/${message.author.id}/${
message.author.avatar
}.${message.author.avatar.startsWith('a_') ? 'gif' : 'png'}`
: '';
// Querry sheets for rows with the message id.
const starboardMessage = await lib.googlesheets.query['@0.3.0'].select({
range: `A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
Message__is: event.message_id,
},
],
limit: {
count: 0,
offset: 0,
},
});
// If the starcount meets the requirements and the message isn't on the starboard yet, send a message to the starboard and insert it into sheets.
if (
filteredStarReactions.length >= parseInt(process.env.STARS_REQUIRED) &&
!starboardMessage.rows.length
) {
const msg = await lib.discord.channels['@0.3.1'].messages.create({
channel_id: process.env.CHANNEL_ID,
content: `:star: **${filteredStarReactions.length}**`,
embed: {
type: 'rich',
title: 'Original Message',
url: `https://discord.com/channels/${event.guild_id}/${message.channel_id}/${message.id}`,
author: {
name: message.author.username,
icon_url: authorAvatar,
},
description: message.content || `No content.`,
image: message.attachments.length
? {url: message.attachments[0].url}
: {},
timestamp: message.timestamp,
color: 0xffff00,
},
});
await lib.googlesheets.query['@0.3.0'].insert({
range: `A:C`,
fieldsets: [
{
Message: message.id,
Starcount: filteredStarReactions.length,
StarboardMessage: msg.id,
},
],
});
}
// If message is already on the starboard, update the starcount and update the sheets.
if (starboardMessage.rows.length) {
await lib.discord.channels['@0.3.1'].messages.update({
message_id: starboardMessage.rows[0].fields.StarboardMessage,
channel_id: process.env.CHANNEL_ID,
content: `:star: **${filteredStarReactions.length}**`,
});
await lib.googlesheets.query['@0.3.0'].update({
range: `A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
Message__is: message.id,
},
],
limit: {
count: 0,
offset: 0,
},
fields: {
Starcount: filteredStarReactions.length,
},
});
}