Skip to content

Commit

Permalink
Allow announcement channels to be configured for notifications (wise-…
Browse files Browse the repository at this point in the history
  • Loading branch information
rorro authored Oct 18, 2024
1 parent bb778ce commit d12a2b6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
22 changes: 17 additions & 5 deletions src/commands/instances/config/ConfigNotificationsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
ChannelType,
ChatInputCommandInteraction,
EmbedBuilder,
PermissionFlagsBits
PermissionFlagsBits,
PermissionsBitField
} from 'discord.js';
import config from '../../../config';
import { updateBotDefaultChannel, updateNotificationPreferences } from '../../../services/prisma';
Expand Down Expand Up @@ -39,7 +40,7 @@ const CONFIG: CommandConfig = {
required: true,
name: 'notification_channel',
description: 'The channel to which notifications are sent.',
channelType: ChannelType.GuildText
channelTypes: [ChannelType.GuildText, ChannelType.GuildAnnouncement]
},
{
type: ApplicationCommandOptionType.String,
Expand Down Expand Up @@ -80,14 +81,25 @@ class ConfigNotificationsCommand extends Command {
const channelPermissions =
channel.client.user !== null ? channel.permissionsFor(channel.client.user) : null;

if (channelPermissions !== null && !channelPermissions.has(PermissionFlagsBits.ViewChannel)) {
throw new CommandError(`Error: The bot does not have access to <#${channel.id}>.`);
if (channelPermissions !== null) {
if (!channelPermissions.has(PermissionFlagsBits.ViewChannel)) {
throw new CommandError(
`Error: The bot does not have \`View Channel\` permissions for <#${channel.id}>.`
);
} else if (!channelPermissions.has(PermissionFlagsBits.SendMessages)) {
throw new CommandError(
`Error: The bot does not have \`Send Messages\` permissions for <#${channel.id}>.`
);
}
}

const missingPermissions = getMissingPermissions(channel);

if (missingPermissions.length > 0) {
const missingPermissionsList = missingPermissions.map(p => `\`${p}\``).join(', ');
const missingPermissionsList = new PermissionsBitField(missingPermissions)
.toArray()
.map(p => `\`${p}\``)
.join(', ');

throw new CommandError(
`Error: The bot is missing the following permissions on <#${channel.id}>: \n\n${missingPermissionsList}`
Expand Down
4 changes: 2 additions & 2 deletions src/utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ interface StringOption extends BaseOption {

interface ChannelOption extends BaseOption {
type: ApplicationCommandOptionType.Channel;
channelType?: ApplicationCommandOptionAllowedChannelTypes;
channelTypes?: Array<ApplicationCommandOptionAllowedChannelTypes>;
}

interface UserOption extends BaseOption {
Expand Down Expand Up @@ -168,7 +168,7 @@ function attachOptions(
opt.setName(option.name).setDescription(option.description);

if (option.required) opt.setRequired(true);
if (option.channelType) opt.addChannelTypes(option.channelType);
if (option.channelTypes) opt.addChannelTypes(...option.channelTypes);

return opt;
});
Expand Down
18 changes: 3 additions & 15 deletions src/utils/discord.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { GroupRole, Metric } from '@wise-old-man/utils';
import {
Channel,
DMChannel,
Guild,
GuildMember,
EmbedBuilder,
NewsChannel,
PermissionResolvable,
TextChannel,
User,
Expand Down Expand Up @@ -418,21 +416,11 @@ export function getMissingPermissions(channel: TextChannel) {
});
}

export function isChannelSendable(channel: Channel | undefined | null): channel is TextChannel {
if (!channel) return false;
if (channel.type !== ChannelType.GuildText) return false;
if (!('guild' in channel)) return true;
export function isChannelSendable(channel: Channel): channel is TextChannel {
if (!channel || channel.isDMBased()) return false;

const canView = clientUserPermissions(channel as TextChannel)?.has(PermissionFlagsBits.ViewChannel);

if (
!(channel instanceof DMChannel) &&
!(channel instanceof NewsChannel) &&
!(channel instanceof TextChannel) &&
canView
) {
if (channel.type !== ChannelType.GuildAnnouncement && channel.type !== ChannelType.GuildText)
return false;
}

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChannelType, Client, EmbedBuilder, TextChannel } from 'discord.js';
import { ChannelType, Client, EmbedBuilder } from 'discord.js';
import { getServers, getNotificationPreferences } from '../services/prisma';

export const NotificationType = {
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function propagateMessage(
const channel = await client.channels.fetch(targetChannelId);

if (!channel) return;
if (!((channel): channel is TextChannel => channel.type === ChannelType.GuildText)(channel))
if (channel.type !== ChannelType.GuildText && channel.type !== ChannelType.GuildAnnouncement)
return;

try {
Expand Down

0 comments on commit d12a2b6

Please sign in to comment.