Skip to content
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

Add permissions for running commands #62

Merged
merged 4 commits into from
Mar 2, 2024
Merged
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DISCORD_TOKEN=<string: 'Discord Bot Token'>
GUILD_ID=<string: 'Discord Server ID'>
DISCORD_CLIENT_ID=<string: 'Discord App Client ID'>
DATABASE_URI=<string: 'MongoDB URI'>
DATABASE_URI=<string: 'MongoDB URI'>
MOD_CHANNEL_ID=<string 'Channel ID'>
3 changes: 3 additions & 0 deletions src/commands/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -14,6 +15,7 @@ export default {
.setName("announce")
.setDescription("announcement the world something")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand All @@ -25,6 +27,7 @@ export default {
option.setName("mention").setDescription("Who to mention").setRequired(false),
) as SlashCommandBuilder,

isMod: true,
async execute(interaction) {
if (!interaction.guild) return;

Expand Down
4 changes: 4 additions & 0 deletions src/commands/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -14,6 +15,7 @@ export default {
.setName("echo")
.setDescription("Announce a message to a channel")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand All @@ -25,6 +27,8 @@ export default {
option.setName("mention").setDescription("Who to mention").setRequired(false),
) as SlashCommandBuilder,

isMod: true,

async execute(interaction) {
if (!interaction.guild) return;
const channelId = (interaction.options.getChannel("channel")?.id || interaction.channelId) as string;
Expand Down
4 changes: 4 additions & 0 deletions src/commands/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SlashCommandChannelOption,
TextInputBuilder,
TextInputStyle,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";

Expand All @@ -14,6 +15,7 @@ export default {
.setName("images")
.setDescription("send images")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addChannelOption((option: SlashCommandChannelOption) => {
return option
.setName("channel")
Expand All @@ -22,6 +24,8 @@ export default {
.addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement);
}) as SlashCommandBuilder,

isMod: true,

async execute(interaction) {
if (!interaction.guild) return;
const channelId = (interaction.options.getChannel("channel")?.id || interaction.channelId) as string;
Expand Down
2 changes: 2 additions & 0 deletions src/commands/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Command } from "../interface";
export default {
data: new SlashCommandBuilder().setName("ping").setDescription("Replies with Pong!").setDMPermission(false),

isMod: true,

async execute(interaction) {
const message = await interaction.reply({ content: "Pong!", fetchReply: true });
await interaction.editReply(`Pong! Latency is ${Math.abs(Date.now() - message.createdTimestamp)}ms.`);
Expand Down
4 changes: 4 additions & 0 deletions src/commands/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StringSelectMenuBuilder,
SlashCommandSubcommandBuilder,
ChannelType,
PermissionFlagsBits,
} from "discord.js";
import { Command } from "../interface";
import db from "../utils/database";
Expand All @@ -16,6 +17,7 @@ export default {
.setName("template")
.setDescription("provides us with the templates")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addSubcommand((subcommand: SlashCommandSubcommandBuilder) =>
subcommand.setName("create").setDescription("Creates Templates"),
)
Expand All @@ -35,6 +37,8 @@ export default {
subcommand.setName("delete").setDescription("Deletes the Templates"),
) as SlashCommandBuilder,

isMod: true,

async execute(interaction) {
if (!interaction.guild) return;
const subcommand = interaction.options.getSubcommand();
Expand Down
28 changes: 20 additions & 8 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ export default {
await interaction.reply({ content: "Command not found", ephemeral: true });
return;
}
if (command.isMod) {
const interactionChannelId = interaction.channelId;
const envChannelId = process.env.CHANNEL_ID;

try {
command.execute(interaction);
} catch (err) {
console.error(err);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
if (interactionChannelId !== envChannelId) {
await interaction.reply({
content: "This command can only be executed in a specific channel.",
ephemeral: true,
});
return;
}

try {
command.execute(interaction);
} catch (err) {
console.error(err);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
},
};
1 change: 1 addition & 0 deletions src/interface/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";

export interface Command {
data: SlashCommandBuilder;
isMod: boolean;

execute(interaction: ChatInputCommandInteraction): void;
}
Loading