Skip to content

lockdown commands #2

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

Merged
merged 1 commit into from
Mar 5, 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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as lockdown from "./lockdown";
import * as moderation from "./moderation";
import * as ping from "./ping";
import * as refresh from "./refresh";
Expand All @@ -6,6 +7,7 @@ import * as verification from "./verification";
import * as verify from "./verify";

export const commands = {
lockdown,
moderation,
ping,
refresh,
Expand Down
47 changes: 47 additions & 0 deletions src/commands/lockdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Actions } from "@/types";
import {
CommandInteraction,
InteractionType,
PermissionFlagsBits,
SlashCommandBuilder,
} from "discord.js";
import { channel, server } from "./lockdowns";

export const data = new SlashCommandBuilder()
.setName("lockdown")
.setDescription("Lockdown related commands")
.addSubcommand((subcommand) =>
subcommand
.setName("channel")
.setDescription("Lock a channel")
.addChannelOption((option) =>
option
.setName("channel")
.setDescription("Channel to lock. Defaults to the current channel")
.setRequired(false),
),
)
.addSubcommand((subcommand) =>
subcommand.setName("server").setDescription("Lock the entire server"),
)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.setDMPermission(false);

export const execute = async (interaction: CommandInteraction) => {
if (
interaction.type !== InteractionType.ApplicationCommand ||
!interaction.isChatInputCommand()
)
return;

const command = interaction.options.getSubcommand();

await interaction.deferReply({ ephemeral: true });

const actions: Actions = {
channel: channel,
server: server,
};

return actions[command](interaction);
};
25 changes: 25 additions & 0 deletions src/commands/lockdowns/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CommandInteraction, EmbedBuilder, TextChannel } from "discord.js";
import { lockChannel, unlockChannel } from "./util";

const channel = async (interaction: CommandInteraction) => {
let message = "";

const channel = (interaction.options.get("channel")?.channel ||
interaction.channel) as TextChannel;

try {
if (channel.topic?.includes("This channel is currently locked.")) {
await unlockChannel(interaction, channel);
message = `Succesfully unlocked ${channel}`;
} else {
await lockChannel(interaction, channel);
message = `Succesfully locked ${channel}`;
}
} catch (error) {
message = `An error occurred while editing permissions for ${channel}: ${error}`;
}

return interaction.editReply(message);
};

export default channel;
4 changes: 4 additions & 0 deletions src/commands/lockdowns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import channel from "./channel";
import server from "./server";

export { channel, server };
26 changes: 26 additions & 0 deletions src/commands/lockdowns/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ChannelType, CommandInteraction } from "discord.js";
import { lockChannel, unlockChannel } from "./util";

const server = async (interaction: CommandInteraction) => {
let message = "";

try {
interaction.guild?.channels.cache.forEach(async (channel) => {
if (channel.type !== ChannelType.GuildText) return;

if (channel.topic?.includes("This channel is currently locked.")) {
await unlockChannel(interaction, channel);
message = `Succesfully unlocked every channel`;
} else {
await lockChannel(interaction, channel);
message = `Succesfully locked every channel`;
}
});
} catch (error) {
message = `An error occurred while editing permissions for the server: ${error}`;
}

return interaction.editReply(message);
};

export default server;
83 changes: 83 additions & 0 deletions src/commands/lockdowns/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
CommandInteraction,
EmbedBuilder,
Guild,
TextChannel,
} from "discord.js";
import { verificationConfig } from "@/config";

const lockChannel = async (
interaction: CommandInteraction,
channel: TextChannel,
) => {
const embed = new EmbedBuilder();

if (!verificationConfig[(interaction.guild as Guild)?.id]) {
embed
.setColor(0xff0000)
.setTitle("Error")
.setDescription(
"This server has not been properly setup yet! Please run the ``/verification setup`` command to continue!",
);

return interaction.editReply({ embeds: [embed] });
}

await channel?.permissionOverwrites.edit(
verificationConfig[(interaction.guild as Guild)?.id].role,
{
SendMessages: false,
},
);

await channel.setTopic(
(channel.topic || "") + " This channel is currently locked.",
);

embed
.setColor(0xff0000)
.setTitle("Channel Locked")
.setDescription(`This channel has been locked by ${interaction.user}`);

return channel.send({ embeds: [embed] });
};

const unlockChannel = async (
interaction: CommandInteraction,
channel: TextChannel,
) => {
const embed = new EmbedBuilder();

if (!verificationConfig[(interaction.guild as Guild)?.id]) {
embed
.setColor(0xff0000)
.setTitle("Error")
.setDescription(
"This server has not been properly setup yet! Please run the ``/verification setup`` command to continue!",
);

return interaction.editReply({ embeds: [embed] });
}

await channel?.permissionOverwrites.edit(
verificationConfig[(interaction.guild as Guild)?.id].role,
{
SendMessages: true,
},
);

channel.setTopic(
channel.topic?.replaceAll("This channel is currently locked.", "") as
| string
| null,
);

embed
.setColor(0x00ff00)
.setTitle("Channel Unlocked")
.setDescription(`This channel has been unlocked by ${interaction.user}`);

return channel.send({ embeds: [embed] });
};

export { lockChannel, unlockChannel };
2 changes: 1 addition & 1 deletion src/commands/verifications/manual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const manual = async (interaction: CommandInteraction) => {
const embed = new EmbedBuilder();

if (!verificationConfig[(interaction.guild as Guild)?.id]) {
const embed = new EmbedBuilder()
embed
.setColor(0xff0000)
.setTitle("Error")
.setDescription(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const execute = async (interaction: CommandInteraction) => {
const embed = new EmbedBuilder();

if (!verificationConfig[(interaction.guild as Guild)?.id]) {
const embed = new EmbedBuilder()
embed
.setColor(0xff0000)
.setTitle("Error")
.setDescription(
Expand Down