1
0
Fork 0
polaris-task-force/src/bot/commands/announce.ts

77 lines
2.4 KiB
TypeScript

import {
EmbedBuilder,
MessageFlags,
SlashCommandBuilder,
type TextBasedChannelFields,
} from "discord.js";
import type { BotCommand } from "./index";
import { botConfig } from "@/bot/config";
import { isStaff } from "@/bot/lib/roles";
const ANNOUNCE_COLOR = 0xf59e0b;
export const announceCommand: BotCommand = {
builder: new SlashCommandBuilder()
.setName("announce")
.setDescription("Post an announcement to a channel")
.addStringOption((option) =>
option.setName("message").setDescription("The announcement text").setRequired(true),
)
.addChannelOption((option) =>
option.setName("channel").setDescription("Channel to post to"),
) as SlashCommandBuilder,
execute: async (interaction, payload) => {
if (!interaction.inGuild() || !interaction.guild) {
await interaction.reply({
content: "This command only works in a server.",
flags: MessageFlags.Ephemeral,
});
return;
}
const member = await interaction.guild.members.fetch(interaction.user.id);
if (!(await isStaff(member, payload))) {
await interaction.reply({
content: "You don't have permission to announce.",
flags: MessageFlags.Ephemeral,
});
return;
}
const message = interaction.options.getString("message", true);
const option = interaction.options.getChannel("channel");
const channelId = option?.id ?? botConfig.announceChannelId ?? botConfig.opsChannelId;
if (!channelId) {
await interaction.reply({
content: "No announce channel is configured and no channel was given.",
flags: MessageFlags.Ephemeral,
});
return;
}
const optionChannel = option && "send" in option ? (option as TextBasedChannelFields) : null;
const channel = optionChannel ?? (await interaction.guild.channels.fetch(channelId));
if (!channel || !("send" in channel)) {
await interaction.reply({
content: "That channel isn't a text channel.",
flags: MessageFlags.Ephemeral,
});
return;
}
await (channel as TextBasedChannelFields).send({
embeds: [
new EmbedBuilder()
.setTitle("Announcement")
.setDescription(message)
.setColor(ANNOUNCE_COLOR),
],
});
const name = (channel as unknown as { name?: string | null }).name ?? channelId;
await interaction.reply({
content: `Announcement posted to #${name}.`,
flags: MessageFlags.Ephemeral,
});
},
};