2023-02-10 04:25:25 +00:00
|
|
|
/* eslint-disable comma-dangle */
|
|
|
|
// dotenv for handling environment variables
|
|
|
|
const dotenv = require('dotenv');
|
|
|
|
dotenv.config();
|
|
|
|
const isDev = process.env.isDev;
|
|
|
|
|
|
|
|
// filesystem
|
|
|
|
const fs = require('fs');
|
2023-08-13 02:10:05 +00:00
|
|
|
const https = require('https');
|
2023-02-10 04:25:25 +00:00
|
|
|
|
|
|
|
// Discord.js
|
|
|
|
const Discord = require('discord.js');
|
|
|
|
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = Discord;
|
|
|
|
|
|
|
|
// Various imports from other files
|
|
|
|
const config = require('../data/config.json');
|
|
|
|
const strings = require('../data/strings.json');
|
|
|
|
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
|
|
|
|
|
|
|
|
const functions = {
|
|
|
|
// Functions for managing and creating Collections
|
2023-02-11 03:41:47 +00:00
|
|
|
collectionBuilders: {
|
2023-02-10 04:25:25 +00:00
|
|
|
// Create the collection of slash commands
|
|
|
|
slashCommands(client) {
|
|
|
|
if (!client.slashCommands) client.slashCommands = new Discord.Collection();
|
|
|
|
client.slashCommands.clear();
|
|
|
|
for (const file of slashCommandFiles) {
|
|
|
|
const slashCommand = require(`../slash-commands/${file}`);
|
|
|
|
if (slashCommand.data != undefined) {
|
|
|
|
client.slashCommands.set(slashCommand.data.name, slashCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isDev) console.log('Slash Commands Collection Built');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builders: {
|
2023-02-11 03:41:47 +00:00
|
|
|
actionRows: {
|
|
|
|
acceptRules() {
|
|
|
|
// Create the Action Row with the Button in it, to be sent with the Embed
|
|
|
|
return new ActionRowBuilder()
|
|
|
|
.addComponents(
|
|
|
|
this.buttons.acceptRules()
|
|
|
|
);
|
|
|
|
},
|
2023-08-13 02:10:05 +00:00
|
|
|
roleMenu() {
|
2023-02-11 03:41:47 +00:00
|
|
|
return new ActionRowBuilder()
|
|
|
|
.addComponents(
|
|
|
|
this.buttons.waterPing(),
|
2023-08-13 02:10:05 +00:00
|
|
|
this.buttons.fruitPing(),
|
|
|
|
this.buttons.statusPing()
|
2023-02-11 03:41:47 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
buttons: {
|
|
|
|
acceptRules() {
|
|
|
|
return new ButtonBuilder()
|
|
|
|
.setCustomId('acceptrules')
|
|
|
|
.setLabel(`${strings.emoji.confirm} Accept Rules`)
|
|
|
|
.setStyle(ButtonStyle.Primary);
|
|
|
|
},
|
|
|
|
waterPing() {
|
|
|
|
return new ButtonBuilder()
|
|
|
|
.setCustomId('waterpingrole')
|
|
|
|
.setLabel(strings.emoji.water)
|
|
|
|
.setStyle(ButtonStyle.Primary);
|
|
|
|
},
|
|
|
|
fruitPing() {
|
|
|
|
return new ButtonBuilder()
|
|
|
|
.setCustomId('fruitpingrole')
|
|
|
|
.setLabel(strings.emoji.fruit)
|
|
|
|
.setStyle(ButtonStyle.Primary);
|
2023-08-13 02:10:05 +00:00
|
|
|
},
|
|
|
|
statusPing() {
|
|
|
|
return new ButtonBuilder()
|
|
|
|
.setCustomId('statuspingrole')
|
|
|
|
.setLabel(strings.emoji.status)
|
|
|
|
.setStyle(ButtonStyle.Primary);
|
2023-02-11 03:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-10 04:25:25 +00:00
|
|
|
},
|
2023-02-11 03:41:47 +00:00
|
|
|
embeds: {
|
|
|
|
helpEmbed(content, private) {
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(strings.embeds.color)
|
|
|
|
.setTitle('Grow A Tree Analyzer Help')
|
|
|
|
.setDescription(content)
|
|
|
|
.setFooter({ text: strings.embeds.footer });
|
|
|
|
const privateBool = private == 'true';
|
|
|
|
const messageContents = { embeds: [embed], ephemeral: privateBool };
|
|
|
|
return messageContents;
|
|
|
|
},
|
|
|
|
errorEmbed(content) {
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(0xFF0000)
|
|
|
|
.setTitle('Error!')
|
|
|
|
.setDescription(content)
|
|
|
|
.setFooter({ text: strings.embeds.footer });
|
|
|
|
const messageContents = { embeds: [embed], ephemeral: true };
|
|
|
|
return messageContents;
|
|
|
|
},
|
|
|
|
info(content) {
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(0x8888FF)
|
|
|
|
.setTitle('Information')
|
|
|
|
.setDescription(content)
|
|
|
|
.setFooter({ text: strings.embeds.footer });
|
|
|
|
const messageContents = { embeds: [embed], ephemeral: true };
|
|
|
|
return messageContents;
|
|
|
|
},
|
|
|
|
rules() {
|
|
|
|
const actionRow = functions.builders.actionRows.acceptRules();
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(strings.embeds.color)
|
|
|
|
.setTitle(strings.embeds.rulesTitle)
|
|
|
|
.setDescription(strings.embeds.rules)
|
|
|
|
.setFooter({ text: strings.embeds.rulesFooter });
|
|
|
|
return { embeds: [embed], components: [actionRow] };
|
|
|
|
},
|
2023-08-13 02:10:05 +00:00
|
|
|
roleMenu() {
|
|
|
|
const actionRow = functions.builders.actionRows.roleMenu();
|
2023-02-11 03:41:47 +00:00
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setColor(strings.embeds.color)
|
|
|
|
.setTitle(strings.embeds.roleMenuTitle)
|
2023-08-13 02:10:05 +00:00
|
|
|
.setDescription(strings.embeds.roleMenuDesc)
|
2023-02-11 03:41:47 +00:00
|
|
|
.setFooter({ text: strings.embeds.roleMenuFooter });
|
|
|
|
return { embeds: [embed], components: [actionRow] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
roles: {
|
|
|
|
async fetchRole(guild, roleId) {
|
|
|
|
return await guild.roles.fetch(roleId).catch(err => console.error("Error fetching the role: " + err));
|
2023-02-10 04:25:25 +00:00
|
|
|
},
|
2023-02-11 03:41:47 +00:00
|
|
|
async giveRole(member, role) {
|
|
|
|
await member.roles.add(role).catch(err => console.error("Error giving the role: " + err));
|
2023-02-10 04:25:25 +00:00
|
|
|
},
|
2023-02-11 03:41:47 +00:00
|
|
|
async takeRole(member, role) {
|
|
|
|
await member.roles.remove(role).catch(err => console.error("Error taking the role: " + err));
|
2023-02-10 04:25:25 +00:00
|
|
|
}
|
|
|
|
},
|
2023-02-11 03:41:47 +00:00
|
|
|
buttonHandlers: {
|
|
|
|
async fruitPing(interaction) {
|
|
|
|
const role = await functions.roles.fetchRole(interaction.guild, strings.roleIds.fruitPings);
|
|
|
|
if (interaction.member.roles.cache.some(role => role.id == strings.roleIds.fruitPings)) {
|
|
|
|
functions.roles.takeRole(interaction.member, role);
|
|
|
|
} else {
|
|
|
|
functions.roles.giveRole(interaction.member, role);
|
|
|
|
}
|
|
|
|
await interaction.reply(functions.builders.embeds.info("Roles updated!")).catch(err => console.error(err));
|
|
|
|
},
|
|
|
|
async waterPing(interaction) {
|
|
|
|
const role = await functions.roles.fetchRole(interaction.guild, strings.roleIds.waterPings);
|
|
|
|
if (interaction.member.roles.cache.some(role => role.id == strings.roleIds.waterPings)) {
|
|
|
|
functions.roles.takeRole(interaction.member, role);
|
|
|
|
} else {
|
|
|
|
functions.roles.giveRole(interaction.member, role);
|
|
|
|
}
|
|
|
|
await interaction.reply(functions.builders.embeds.info("Roles updated!")).catch(err => console.error(err));
|
|
|
|
},
|
2023-08-13 02:10:05 +00:00
|
|
|
async statusPing(interaction) {
|
|
|
|
const role = await functions.roles.fetchRole(interaction.guild, strings.roleIds.statusPings);
|
|
|
|
if (interaction.member.roles.cache.some(role => role.id == strings.roleIds.statusPings)) {
|
|
|
|
functions.roles.takeRole(interaction.member, role);
|
|
|
|
} else {
|
|
|
|
functions.roles.giveRole(interaction.member, role);
|
|
|
|
}
|
|
|
|
await interaction.reply(functions.builders.embeds.info("Roles updated!")).catch(err => console.error(err));
|
|
|
|
},
|
2023-02-11 03:41:47 +00:00
|
|
|
async acceptRules(interaction) {
|
|
|
|
const role = await functions.roles.fetchRole(interaction.guild, strings.roleIds.member);
|
|
|
|
functions.roles.giveRole(interaction.member, role).catch(err => console.error(err));
|
|
|
|
await interaction.reply(functions.builders.embeds.info("Roles updated!")).catch(err => console.error(err));
|
|
|
|
}
|
2023-08-13 02:10:05 +00:00
|
|
|
},
|
|
|
|
async sendHeartbeat(url) {
|
|
|
|
if (isDev) console.log("Heartbeat Sent: " + url);
|
|
|
|
https.get(url, async (response) => {
|
|
|
|
let data = '';
|
|
|
|
|
|
|
|
response.on('data', (chunk) => data += chunk);
|
|
|
|
|
|
|
|
response.on('end', () => {
|
|
|
|
parsedData = JSON.parse(data);
|
|
|
|
if ( !(parsedData.ok) ) console.error("Heartbeat failed");
|
|
|
|
});
|
|
|
|
}).on("error", (error) => console.error(error));
|
2023-02-10 04:25:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = functions;
|