-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Prefix Command Management (#85)
Co-authored-by: ExampleWasTaken <[email protected]>
- Loading branch information
1 parent
55cfd0e
commit e5f5a12
Showing
45 changed files
with
5,464 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
98 changes: 98 additions & 0 deletions
98
src/commands/moderation/prefixCommands/functions/addCategory.ts
This file contains 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,98 @@ | ||
import { ChatInputCommandInteraction, Colors, User } from 'discord.js'; | ||
import { constantsConfig, getConn, PrefixCommandCategory, Logger, makeEmbed, loadSinglePrefixCommandCategoryToCache } from '../../../../lib'; | ||
|
||
const noConnEmbed = makeEmbed({ | ||
title: 'Prefix Commands - Add Category - No Connection', | ||
description: 'Could not connect to the database. Unable to add the prefix command category.', | ||
color: Colors.Red, | ||
}); | ||
|
||
const failedEmbed = (category: string) => makeEmbed({ | ||
title: 'Prefix Commands - Add Category - Failed', | ||
description: `Failed to add the prefix command category ${category}.`, | ||
color: Colors.Red, | ||
}); | ||
|
||
const alreadyExistsEmbed = (category: string) => makeEmbed({ | ||
title: 'Prefix Commands - Add Category - Already exists', | ||
description: `The prefix command category ${category} already exists. Not adding again.`, | ||
color: Colors.Red, | ||
}); | ||
|
||
const successEmbed = (category: string) => makeEmbed({ | ||
title: `Prefix command category ${category} was added successfully.`, | ||
color: Colors.Green, | ||
}); | ||
|
||
const modLogEmbed = (moderator: User, category: string, emoji: string, categoryId: string) => makeEmbed({ | ||
title: 'Prefix command category added', | ||
fields: [ | ||
{ | ||
name: 'Category', | ||
value: category, | ||
}, | ||
{ | ||
name: 'Moderator', | ||
value: `${moderator}`, | ||
}, | ||
{ | ||
name: 'Emoji', | ||
value: emoji, | ||
}, | ||
], | ||
footer: { text: `Category ID: ${categoryId}` }, | ||
color: Colors.Green, | ||
}); | ||
|
||
const noModLogs = makeEmbed({ | ||
title: 'Prefix Commands - Add Category - No Mod Log', | ||
description: 'I can\'t find the mod logs channel. Please check the channel still exists.', | ||
color: Colors.Red, | ||
}); | ||
|
||
export async function handleAddPrefixCommandCategory(interaction: ChatInputCommandInteraction<'cached'>) { | ||
await interaction.deferReply({ ephemeral: true }); | ||
|
||
const conn = getConn(); | ||
if (!conn) { | ||
await interaction.followUp({ embeds: [noConnEmbed], ephemeral: true }); | ||
return; | ||
} | ||
|
||
const name = interaction.options.getString('name')!; | ||
const emoji = interaction.options.getString('emoji') || ''; | ||
const moderator = interaction.user; | ||
|
||
//Check if the mod logs channel exists | ||
let modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS); | ||
if (!modLogsChannel || !modLogsChannel.isTextBased()) { | ||
modLogsChannel = null; | ||
await interaction.followUp({ embeds: [noModLogs], ephemeral: true }); | ||
} | ||
|
||
const existingCategory = await PrefixCommandCategory.findOne({ name }); | ||
|
||
if (!existingCategory) { | ||
const prefixCommandCategory = new PrefixCommandCategory({ | ||
name, | ||
emoji, | ||
}); | ||
try { | ||
await prefixCommandCategory.save(); | ||
await loadSinglePrefixCommandCategoryToCache(prefixCommandCategory); | ||
await interaction.followUp({ embeds: [successEmbed(name)], ephemeral: true }); | ||
if (modLogsChannel) { | ||
try { | ||
await modLogsChannel.send({ embeds: [modLogEmbed(moderator, name, emoji, prefixCommandCategory.id)] }); | ||
} catch (error) { | ||
Logger.error(`Failed to post a message to the mod logs channel: ${error}`); | ||
} | ||
} | ||
} catch (error) { | ||
Logger.error(`Failed to add a prefix command category ${name}: ${error}`); | ||
await interaction.followUp({ embeds: [failedEmbed(name)], ephemeral: true }); | ||
} | ||
} else { | ||
await interaction.followUp({ embeds: [alreadyExistsEmbed(name)], ephemeral: true }); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/commands/moderation/prefixCommands/functions/addChannelPermission.ts
This file contains 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,113 @@ | ||
import { ChatInputCommandInteraction, Colors, User } from 'discord.js'; | ||
import { constantsConfig, getConn, PrefixCommand, Logger, makeEmbed, refreshSinglePrefixCommandCache } from '../../../../lib'; | ||
|
||
const noConnEmbed = makeEmbed({ | ||
title: 'Prefix Commands - Add Channel - No Connection', | ||
description: 'Could not connect to the database. Unable to add the prefix command channel.', | ||
color: Colors.Red, | ||
}); | ||
|
||
const noCommandEmbed = (command: string) => makeEmbed({ | ||
title: 'Prefix Commands - Add Channel - No Command', | ||
description: `Failed to add the prefix command channel for command ${command} as the command does not exist or there are more than one matching.`, | ||
color: Colors.Red, | ||
}); | ||
|
||
const failedEmbed = (command: string, channel: string) => makeEmbed({ | ||
title: 'Prefix Commands - Add Channel - Failed', | ||
description: `Failed to add the prefix command channel <#${channel}> for command ${command}.`, | ||
color: Colors.Red, | ||
}); | ||
|
||
const alreadyExistsEmbed = (command: string, channel: string) => makeEmbed({ | ||
title: 'Prefix Commands - Add Channel - Already exists', | ||
description: `A prefix command channel <#${channel}> for command ${command} already exists. Not adding again.`, | ||
color: Colors.Red, | ||
}); | ||
|
||
const successEmbed = (command: string, channel: string) => makeEmbed({ | ||
title: `Prefix command channel <#${channel}> added for command ${command}.`, | ||
color: Colors.Green, | ||
}); | ||
|
||
const modLogEmbed = (moderator: User, command: string, channel: string) => makeEmbed({ | ||
title: 'Add prefix command channel permission', | ||
fields: [ | ||
{ | ||
name: 'Command', | ||
value: command, | ||
}, | ||
{ | ||
name: 'Channel', | ||
value: `<#${channel}>`, | ||
}, | ||
{ | ||
name: 'Moderator', | ||
value: `${moderator}`, | ||
}, | ||
], | ||
color: Colors.Green, | ||
}); | ||
|
||
const noModLogs = makeEmbed({ | ||
title: 'Prefix Commands - Add Channel - No Mod Log', | ||
description: 'I can\'t find the mod logs channel. Please check the channel still exists.', | ||
color: Colors.Red, | ||
}); | ||
|
||
export async function handleAddPrefixCommandChannelPermission(interaction: ChatInputCommandInteraction<'cached'>) { | ||
await interaction.deferReply({ ephemeral: true }); | ||
|
||
const conn = getConn(); | ||
if (!conn) { | ||
await interaction.followUp({ embeds: [noConnEmbed], ephemeral: true }); | ||
return; | ||
} | ||
|
||
const command = interaction.options.getString('command')!; | ||
const channel = interaction.options.getChannel('channel')!; | ||
const moderator = interaction.user; | ||
|
||
//Check if the mod logs channel exists | ||
let modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS); | ||
if (!modLogsChannel || !modLogsChannel.isTextBased()) { | ||
modLogsChannel = null; | ||
await interaction.followUp({ embeds: [noModLogs], ephemeral: true }); | ||
} | ||
|
||
let foundCommands = await PrefixCommand.find({ name: command }); | ||
if (!foundCommands || foundCommands.length > 1) { | ||
foundCommands = await PrefixCommand.find({ aliases: { $in: [command] } }); | ||
} | ||
if (!foundCommands || foundCommands.length > 1) { | ||
await interaction.followUp({ embeds: [noCommandEmbed(command)], ephemeral: true }); | ||
return; | ||
} | ||
const [foundCommand] = foundCommands; | ||
const { id: channelId } = channel; | ||
|
||
const existingChannelPermission = foundCommand.permissions.channels?.includes(channelId); | ||
if (!existingChannelPermission) { | ||
if (!foundCommand.permissions.channels) { | ||
foundCommand.permissions.channels = []; | ||
} | ||
foundCommand.permissions.channels.push(channelId); | ||
try { | ||
await foundCommand.save(); | ||
await refreshSinglePrefixCommandCache(foundCommand, foundCommand); | ||
await interaction.followUp({ embeds: [successEmbed(command, channelId)], ephemeral: true }); | ||
if (modLogsChannel) { | ||
try { | ||
await modLogsChannel.send({ embeds: [modLogEmbed(moderator, command, channelId)] }); | ||
} catch (error) { | ||
Logger.error(`Failed to post a message to the mod logs channel: ${error}`); | ||
} | ||
} | ||
} catch (error) { | ||
Logger.error(`Failed to add prefix command channel <#${channel}> for command ${command}: ${error}`); | ||
await interaction.followUp({ embeds: [failedEmbed(command, channelId)], ephemeral: true }); | ||
} | ||
} else { | ||
await interaction.followUp({ embeds: [alreadyExistsEmbed(command, channelId)], ephemeral: true }); | ||
} | ||
} |
Oops, something went wrong.