Skip to content

Commit

Permalink
Do not attempt to auto-publish unpublishable messages
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-roesch committed Jul 20, 2024
1 parent 0ec8519 commit 9d458df
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
21 changes: 16 additions & 5 deletions src/cmds/auto-publish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { ComplexCommand } from '../commands';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, ChatInputCommandInteraction, Client, ColorResolvable, EmbedBuilder, Message, TextChannel } from 'discord.js';
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChannelType,
ChatInputCommandInteraction,
Client,
ColorResolvable,
EmbedBuilder,
Message,
TextChannel
} from 'discord.js';
import { guilds as storage } from '../utils/storage';
import logger from '../utils/logger';

Expand All @@ -21,7 +32,7 @@ async function publishMessage(message: Message): Promise<boolean> {
return true;
}

if (message.channel.type !== ChannelType.GuildNews) return;
if (!message.crosspostable || message.channel.isDMBased()) return;
const channelName = `#${message.channel.name}`;

try {
Expand Down Expand Up @@ -67,7 +78,7 @@ export default {
async start(client: Client, interaction: ChatInputCommandInteraction<'cached'>) {
const channel = interaction.options.getChannel('channel', true);

if (channel.type !== ChannelType.GuildNews) {
if (channel.type !== ChannelType.GuildAnnouncement) {
await interaction.reply({
embeds: [
buildEmbed(
Expand Down Expand Up @@ -123,7 +134,7 @@ export default {
async stop(client: Client, interaction: ChatInputCommandInteraction<'cached'>) {
const channel = interaction.options.getChannel('channel', true);

if (channel.type === ChannelType.GuildNews && !channel.permissionsFor(interaction.user).has('ManageMessages')) {
if (channel.type === ChannelType.GuildAnnouncement && !channel.permissionsFor(interaction.user).has('ManageMessages')) {
await interaction.reply({
embeds: [
buildEmbed(
Expand Down Expand Up @@ -204,7 +215,7 @@ export default {
),
additionalHandlers: {
async messageCreate(client: Client, message: Message): Promise<void> {
if (message.channel.type !== ChannelType.GuildNews) {
if (!message.crosspostable || message.channel.isDMBased()) {
return;
}

Expand Down
37 changes: 29 additions & 8 deletions src/cmds/edit-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ActionRowBuilder, ChannelSelectMenuBuilder, ModalActionRowComponentBuil
import { ChannelType, ComponentType, Snowflake, TextInputStyle } from 'discord-api-types/v10';
import {
Client,
DiscordjsError,
DiscordjsErrorCodes,
MessageContextMenuCommandInteraction,
ModalBuilder,
ModalSubmitInteraction,
Expand Down Expand Up @@ -56,11 +58,21 @@ export default {
await interaction.showModal(modal);

const filter = (interaction: ModalSubmitInteraction) => interaction.customId === modalId;
const answer = await interaction.awaitModalSubmit({
filter,
time: 300_000
});
const newContent = answer.fields.getTextInputValue('content').trim();
let newContent = message.content;
let answer: ModalSubmitInteraction<'cached'>;
try {
answer = await interaction.awaitModalSubmit({
filter,
time: 10_000
});
newContent = answer.fields.getTextInputValue('content').trim();
await answer.deferReply({ ephemeral: true });
} catch (error) {
if (error instanceof DiscordjsError && error.code === DiscordjsErrorCodes.InteractionCollectorError) {
return;
}
throw error;
}

try {
await webhook.editMessage(interaction.targetMessage, { content: newContent });
Expand All @@ -78,7 +90,7 @@ export default {
return;
}

await answer.reply({ content: 'Webhook message succesfully edited', ephemeral: true });
await answer.reply({ content: 'Webhook message successfully edited', ephemeral: true });
}
),
new SimpleCommand(
Expand All @@ -104,7 +116,16 @@ export default {
.setChannelTypes(ChannelType.GuildText, ChannelType.PublicThread, ChannelType.PrivateThread, ChannelType.GuildForum)
);
const reply = await interaction.editReply({ content: 'Which channel should conversation be directed to?', components: [row] });
const targetChannel = (await reply.awaitMessageComponent({ componentType: ComponentType.ChannelSelect })).values[0];
let targetChannel: string;
try {
targetChannel = (await reply.awaitMessageComponent({ componentType: ComponentType.ChannelSelect, time: 60_000 })).values[0];
} catch (error) {
if (error instanceof DiscordjsError && error.code === DiscordjsErrorCodes.InteractionCollectorError) {
await interaction.editReply({ content: 'Did not receive response in time!', components: [] });
return;
}
throw error;
}
let content = message.content;
const cta = `-# Want to talk about this? Go to <#${targetChannel}>!`;
const regex = /^-# Want to talk about this\? Go to <#[0-9]+>!/m;
Expand All @@ -130,7 +151,7 @@ export default {
return;
}

await interaction.editReply({ content: 'Webhook message succesfully edited', components: [] });
await interaction.editReply({ content: 'Webhook message successfully edited', components: [] });
}
)
]
Expand Down

0 comments on commit 9d458df

Please sign in to comment.