Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DISCORDAPIKEY=TOKEN
APIURL=http://example.com
APIKEY=KEY
TZ=Australia/Sydney
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
.env
config.json
.env
25 changes: 14 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ dotenv.config();
// Discord
//
const client = new SapphireClient({
intents: [
'GUILDS',
'GUILD_MESSAGES',
'GUILD_MEMBERS',
intents: [
"GUILDS",
"GUILD_MESSAGES",
"GUILD_MEMBERS",
"GUILD_MESSAGE_REACTIONS",
],
presence: {
status: "online",
activities: [
{
name: "https://modularsoft.org/docs/products/devoteMe/",
type: "PLAYING",
},
],
presence: {
status: "online",
activities: [{
name: 'https://modularsoft.org/docs/products/devoteMe/',
type: 'PLAYING'
}]
}
},
});

//
Expand Down
163 changes: 163 additions & 0 deletions commands/configure.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { Command, RegisterBehavior } from "@sapphire/framework";
import { MessageEmbed, Permissions } from "discord.js";
import fetch from "node-fetch";
import { getTenantConfiguration } from "../controller/tenantController.js";

export class ConfigureCommand extends Command {
constructor(context, options) {
super(context, {
...options,
description: "Configure various settings for the server.",
chatInputCommand: {
register: true,
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
});
}

registerApplicationCommands(registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName("configure")
.setDescription(this.description)
.addSubcommand((subcommand) =>
subcommand
.setName("votd-channel")
.setDescription(
"Set the VOTD channel (only text channels can be selected)."
)
.addChannelOption(
(option) =>
option
.setName("channel")
.setDescription("The channel to set as VOTD channel")
.setRequired(true)
.addChannelTypes(0) // Only text channels
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("devotion-channel")
.setDescription("Set the Devotion channel.")
.addChannelOption(
(option) =>
option
.setName("channel")
.setDescription("The channel to set as Devotion channel")
.setRequired(true)
.addChannelTypes(0) // Only text channels
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("view")
.setDescription(
"View the current VOTD and Devotion channel configuration."
)
)
);
}

async chatInputRun(interaction) {
if (!interaction.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
const embed = new MessageEmbed()
.setColor("RED")
.setTitle("Permission Denied")
.setDescription("You need to be an admin to run this command.");
return interaction.reply({ embeds: [embed], ephemeral: true });
}

const subcommand = interaction.options.getSubcommand();

try {
if (subcommand === "votd-channel") {
const channel = interaction.options.getChannel("channel");
await this.updateChannelConfig(
interaction.guild.id,
"votd_channel",
channel.id
);

const embed = new MessageEmbed()
.setColor("GREEN")
.setTitle("Configuration Updated")
.setDescription(`VOTD channel has been set to <#${channel.id}>.`);
return interaction.reply({ embeds: [embed], ephemeral: true });
}

if (subcommand === "devotion-channel") {
const channel = interaction.options.getChannel("channel");
await this.updateChannelConfig(
interaction.guild.id,
"devotion_channel",
channel.id
);

const embed = new MessageEmbed()
.setColor("GREEN")
.setTitle("Configuration Updated")
.setDescription(`Devotion channel has been set to <#${channel.id}>.`);
return interaction.reply({ embeds: [embed], ephemeral: true });
}

if (subcommand === "view") {
const config = await getTenantConfiguration(interaction.guild.id);
const embed = new MessageEmbed()
.setTitle("Current Configuration")
.setColor("#0099ff")
.addFields(
{
name: "VOTD Channel",
value: config.votd_channel
? `<#${config.votd_channel}>`
: "Not configured",
},
{
name: "Devotion Channel",
value: config.devotion_channel
? `<#${config.devotion_channel}>`
: "Not configured",
}
);

return interaction.reply({ embeds: [embed], ephemeral: true });
}
} catch (error) {
console.error(error);
const embed = new MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription("There was an error while configuring the channels.");
return interaction.reply({ embeds: [embed], ephemeral: true });
}
Comment on lines +126 to +132
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure error replies are only sent once

When an error occurs, check if an interaction response has already been sent before attempting to reply again. This prevents potential exceptions due to multiple replies.

Apply this diff to check the interaction status:

 try {
   // ... existing code ...
 } catch (error) {
   console.error(error);
   const embed = new MessageEmbed()
     .setColor("RED")
     .setTitle("Error")
     .setDescription("There was an error while configuring the channels.");
-  return interaction.reply({ embeds: [embed], ephemeral: true });
+  if (interaction.replied || interaction.deferred) {
+    return interaction.followUp({ embeds: [embed], ephemeral: true });
+  } else {
+    return interaction.reply({ embeds: [embed], ephemeral: true });
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.error(error);
const embed = new MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription("There was an error while configuring the channels.");
return interaction.reply({ embeds: [embed], ephemeral: true });
}
console.error(error);
const embed = new MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription("There was an error while configuring the channels.");
if (interaction.replied || interaction.deferred) {
return interaction.followUp({ embeds: [embed], ephemeral: true });
} else {
return interaction.reply({ embeds: [embed], ephemeral: true });
}
}

}

async updateChannelConfig(tenantId, channelType, channelId) {
try {
const fetchURL = `${process.env.APIURL}/api/tenant/update`;
const response = await fetch(fetchURL, {
method: "POST",
headers: {
"x-access-token": process.env.APIKEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
tenantId: tenantId,
[channelType]: channelId,
}),
});

if (!response.ok) {
throw new Error(`Failed to update ${channelType}`);
}

const data = await response.json();
if (!data.success) {
throw new Error(`API response: ${data.message}`);
}
} catch (error) {
console.error(`Error updating channel configuration: ${error.message}`);
throw new Error("Failed to update channel configuration.");
}
}
}
34 changes: 27 additions & 7 deletions commands/devotion.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Command, RegisterBehavior } from '@sapphire/framework';
import { compileDevotionMessage, getDevotion } from '../controller/devotionController';
import { Command, RegisterBehavior } from "@sapphire/framework";
import {
compileDevotionMessage,
getDevotion,
} from "../controller/devotionController.js";
import { MessageEmbed } from "discord.js";

export class DevotionCommand extends Command {
constructor(context, options) {
super(context, {
...options,
description: 'Display today\'s Devotion.',
description: "Display today's Devotion.",
chatInputCommand: {
register: true,
behaviorWhenNotIdentical: RegisterBehavior.Overwrite
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
});
}
Expand All @@ -22,9 +26,25 @@ export class DevotionCommand extends Command {
}

async chatInputRun(interaction) {
const devotionData = await getDevotion();
const embed = await compileDevotionMessage(devotionData);
try {
const devotionData = await getDevotion();
const embed = await compileDevotionMessage(devotionData);

return interaction.reply({ embeds: [embed] });
// Reply with the devotion message embed
await interaction.reply({ embeds: [embed] });
} catch (error) {
console.error(error);

// Create an error embed
const errorEmbed = new MessageEmbed()
.setColor("RED")
.setTitle("Error")
.setDescription(
"An error occurred while processing the devotion. Please try again later."
);

// Reply with the error embed
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
}
}
}
2 changes: 1 addition & 1 deletion commands/votd.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, RegisterBehavior } from '@sapphire/framework';
import { compileVotdMessage, getVotd } from '../controller/votdController';
import { compileVotdMessage, getVotd } from '../controller/votdController.js';

export class VotdCommand extends Command {
constructor(context, options) {
Expand Down
9 changes: 0 additions & 9 deletions config.json.example

This file was deleted.

Loading