-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: Prefix Command Management #85
base: staging
Are you sure you want to change the base?
Changes from all commits
22f4d96
763c6a0
7a21eff
d4d401f
b9905bc
96c1c3a
4a76dfa
03163ee
fc35d7c
4546bfe
73ca86e
3dd97ce
52a7351
114ed1f
ce78316
7dd6207
3caa8fa
32b6c53
8224fab
68c8de9
de2356e
9fbbf33
9238f42
152d834
fb5db69
5b867cf
a4351c7
984b1db
2df024c
23ce2f6
72fa602
035b415
26973ba
d3e0165
98eabc8
8076f0b
dc11d6e
f00ed5c
573862d
5bfc280
1b771fb
539eae2
0b2119b
f26c5ae
ffd3888
551b7eb
556caee
1ddc66e
d07d7be
b650b38
f3893d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ChatInputCommandInteraction, Colors, TextChannel, 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 | ||
const modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS) as TextChannel; | ||
if (!modLogsChannel) { | ||
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 }); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||||||||
import { ChatInputCommandInteraction, Colors, TextChannel, 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 | ||||||||||||
const modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS) as TextChannel; | ||||||||||||
if (!modLogsChannel) { | ||||||||||||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Might be safer to actually check if the channel is a text channel? Especially if we want to offer the codebase to other projects. Then, if it's not a text channel set it to null to prevent accidental attempts to send into a non-text channel. |
||||||||||||
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 }); | ||||||||||||
} | ||||||||||||
} |
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.
Might be safer to actually check if the channel is a text channel? Especially if we want to offer the codebase to other projects. Then, if it's not a text channel set it to null to prevent accidental attempts to send into a non-text channel.