-
Notifications
You must be signed in to change notification settings - Fork 0
API implementation for Discord module to allow multiple guilds. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benrobson
wants to merge
15
commits into
staging
Choose a base branch
from
feat-api-imp
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c0d1e91
Append .js to some files and update deps.
benrobson 54a3e7f
Start draft on API discord instance.
benrobson 8ae54ae
Implement devotion completion button and stat
benrobson 795aad8
Add stat tracking for VOTD and checks for duplicates on VOTD and Devo…
benrobson ad2e384
Start work on tenant config command.
benrobson 5d1b791
Fix tenant update configuration command
benrobson 6bb823c
Added tenant controller and fixed configure view
benrobson 13a1e8b
Move cron to 6am, make devotion and VOTD send at global time.
benrobson 068b84b
Remove config.json and all reference due to API.
benrobson 024422d
Added some logging to GuildCreate and updated packages.
benrobson 9d467c5
Log response for troubleshooting
benrobson 048dfcb
Fix apiKey to APIKEY
benrobson 448d18c
Fix APIKEY error on devotion post
benrobson ec40cd9
Added message create and delete events, removed faulty admin catch error
benrobson edbcbdb
Have getTenants retrieve all guilds.
benrobson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
node_modules | ||
.env | ||
config.json | ||
.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
|
||
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."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
📝 Committable suggestion