Skip to content

Commit

Permalink
refactor: change way guildOnly commands are handled (#501)
Browse files Browse the repository at this point in the history
Handle commands being disallowed from being used in DMs through Discord's native Slash Commands instead of through the bot's code.

Co-authored-by: Constantinos <37239383+Costpap@users.noreply.github.com>

Signed-off-by: Suspense <64612795+AlmostSuspense@users.noreply.github.com>
  • Loading branch information
AlmostSuspense committed Jul 18, 2022
1 parent 6d59678 commit f66f0a8
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 29 deletions.
8 changes: 3 additions & 5 deletions src/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ export default {
.addUserOption((option) => option.setName('user').setDescription('User to ban').setRequired(true))
.addStringOption((option) =>
option.setName('reason').setDescription('Reason for banning the user').setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
if (!interaction.guild.members.me.permissions.has(PermissionFlagsBits.BanMembers)) {
return interaction.reply({
content: '❌ Sorry, I need the `Ban Members` permission n order to execute this command.',
content: '❌ Sorry, I need the `Ban Members` permission in order to execute this command.',
ephemeral: true,
});
}
Expand Down
6 changes: 2 additions & 4 deletions src/commands/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ export default {
.addUserOption((option) => option.setName('user').setDescription('User to kick').setRequired(true))
.addStringOption((option) =>
option.setName('reason').setDescription('Reason for kicking the user').setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
if (!interaction.guild.members.me.permissions.has(PermissionFlagsBits.KickMembers)) {
return interaction.reply({
content: '❌ Sorry, I need the `Kick Members` in order to execute this command.',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ export default {
.setDescription('Deletes up to 100 messages.')
.addIntegerOption((option) =>
option.setName('amount').setDescription('Amount of messages to delete').setRequired(true),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
if (!interaction.guild.members.me.permissions.has(PermissionFlagsBits.ManageMessages)) {
return interaction.reply({
content: '❌ Sorry, I need the `Manage Messages` permission in order to execute this command.',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/role-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ export default {
.setName('show_permissions')
.setDescription("Whether to show the role's permissions")
.setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
const apiRole = interaction.options.getRole('role', true);
const role: Role = interaction.guild.roles.cache.get(apiRole.id);

Expand Down
6 changes: 2 additions & 4 deletions src/commands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ export default {
.setDescription('Displays info about your server')
.addBooleanOption((option) =>
option.setName('show_roles').setDescription("Whether to show the server's roles").setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction, client: Client) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
const embed = new EmbedBuilder()
.setColor('Random')
.setTitle(`${interaction.guild.name}`)
Expand Down
6 changes: 2 additions & 4 deletions src/commands/unban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ export default {
.addUserOption((option) => option.setName('user').setDescription('User to unban').setRequired(true))
.addStringOption((option) =>
option.setName('reason').setDescription('Reason for unbanning the user').setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction, client: Client) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
if (!interaction.guild.members.me.permissions.has(PermissionFlagsBits.BanMembers)) {
return interaction.reply({
content: '❌ Sorry, I need the `Ban Members` permission in order to execute this command.',
Expand Down
6 changes: 2 additions & 4 deletions src/commands/user-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ export default {
.setName('show_permissions')
.setDescription("Whether to show the user's permissions")
.setRequired(false),
),
)
.setDMPermission(false),
run: async (interaction: ChatInputCommandInteraction) => {
// Typeguard in order to ensure having access to ChatInputCommand interaction options.
if (!interaction.isChatInputCommand()) return;

if (interaction.inGuild() === false) {
return interaction.reply({ content: "❌ I can't execute this command inside DMs!", ephemeral: true });
}
const user = interaction.options?.getUser('user') ?? interaction.user;

const member = interaction.guild.members.cache.get(user.id);
Expand Down

0 comments on commit f66f0a8

Please sign in to comment.