Skip to content

Commit

Permalink
Update code for new discord.js version
Browse files Browse the repository at this point in the history
  • Loading branch information
violine1101 committed Oct 2, 2024
1 parent a3a64d6 commit 8780162
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/MojiraBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export default class MojiraBot {
// Filter feed tasks.
for ( const config of BotConfig.filterFeeds ) {
const channel = await DiscordUtil.getChannel( config.channel );
if ( channel === undefined ) continue;
if ( channel === undefined || !channel.isSendable() ) continue;

if ( config.cached ) {
TaskScheduler.addTask(
Expand All @@ -247,7 +247,7 @@ export default class MojiraBot {
// Version feed tasks.
for ( const config of BotConfig.versionFeeds ) {
const channel = await DiscordUtil.getChannel( config.channel );
if ( channel === undefined ) continue;
if ( channel === undefined || !channel.isSendable() ) continue;

TaskScheduler.addTask(
new VersionFeedTask( config, channel ),
Expand Down
4 changes: 4 additions & 0 deletions src/commands/MentionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export default class MentionCommand extends Command {
public async run( message: Message, args: string[] ): Promise<boolean> {
if ( ChannelConfigUtil.mentionsDisabled( message.channel ) ) return false;

if ( !message.channel.isSendable() ) {
return false;
}

const mention = MentionRegistry.getMention( args, message.channel );

let embed: EmbedBuilder;
Expand Down
4 changes: 4 additions & 0 deletions src/events/internal/InternalProgressEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default class InternalProgressEventHandler implements EventHandler<'messa

// This syntax is used to ensure that `this` refers to the `InternalProgressEventHandler` object
public onEvent = async ( origin: Message ): Promise<void> => {
if ( !origin.channel.isSendable() ) {
return;
}

const messageId = origin.content.split( /\s/ )[0];
if ( !this.isValidId( messageId ) ) {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/events/message/MessageDeleteEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, MessageType, Snowflake } from 'discord.js';
import { Message, MessageType, OmitPartialGroupDMChannel, PartialMessage, Snowflake } from 'discord.js';

Check warning on line 1 in src/events/message/MessageDeleteEventHandler.ts

View workflow job for this annotation

GitHub Actions / Build (18.x)

'OmitPartialGroupDMChannel' is defined but never used
import BotConfig from '../../BotConfig.js';
import DiscordUtil from '../../util/DiscordUtil.js';
import EventHandler from '../EventHandler.js';
Expand All @@ -18,7 +18,7 @@ export default class MessageDeleteEventHandler implements EventHandler<'messageD
}

// This syntax is used to ensure that `this` refers to the `MessageDeleteEventHandler` object
public onEvent = async ( message: Message ): Promise<void> => {
public onEvent = async ( message: Message | PartialMessage ): Promise<void> => {
message = await DiscordUtil.fetchMessage( message );

if (
Expand Down
4 changes: 4 additions & 0 deletions src/events/modmail/ModmailEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class ModmailEventHandler implements EventHandler<'messageCreate'

// This syntax is used to ensure that `this` refers to the `ModmailEventHandler` object
public onEvent = async ( origin: Message ): Promise<void> => {
if ( !origin.channel.isSendable() ) {
return;
}

const modmailChannel = await DiscordUtil.getChannel( BotConfig.modmailChannel );

const banStatus = BotConfig.database.prepare( 'SELECT user FROM modmail_bans WHERE user = ?' ).get( origin.author.id );
Expand Down
6 changes: 4 additions & 2 deletions src/events/request/RequestDeleteEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, Snowflake, TextChannel } from 'discord.js';
import { Message, PartialMessage, Snowflake, TextChannel } from 'discord.js';
import log4js from 'log4js';
import EventHandler from '../EventHandler.js';
import { RequestsUtil } from '../../util/RequestsUtil.js';
Expand All @@ -20,7 +20,9 @@ export default class RequestDeleteEventHandler implements EventHandler<'messageD
}

// This syntax is used to ensure that `this` refers to the `RequestDeleteEventHandler` object
public onEvent = async ( origin: Message ): Promise<void> => {
public onEvent = async ( origin: Message | PartialMessage ): Promise<void> => {
origin = await DiscordUtil.fetchMessage( origin );

this.logger.info( `User ${ origin.author.tag }'s request ${ origin.id } in channel ${ origin.channel.id } was deleted` );

const internalChannelId = this.internalChannels.get( origin.channel.id );
Expand Down
8 changes: 6 additions & 2 deletions src/events/request/RequestEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder, Message, MessageType, Snowflake, TextChannel } from 'discord.js';
import { EmbedBuilder, GuildChannel, Message, MessageType, Snowflake, TextChannel } from 'discord.js';
import log4js from 'log4js';
import BotConfig, { PrependResponseMessageType } from '../../BotConfig.js';
import DiscordUtil from '../../util/DiscordUtil.js';
Expand Down Expand Up @@ -33,7 +33,11 @@ export default class RequestEventHandler implements EventHandler<'messageCreate'
return;
}

if ( origin.channel instanceof TextChannel ) {
if ( !origin.channel.isSendable() ) {
return;
}

if ( origin.channel instanceof GuildChannel ) {
this.logger.info( `${ origin.author.tag } posted request ${ origin.id } in #${ origin.channel.name }` );
}

Expand Down
7 changes: 5 additions & 2 deletions src/events/request/TestingRequestEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, TextChannel } from 'discord.js';
import { GuildChannel, Message } from 'discord.js';
import log4js from 'log4js';
import BotConfig from '../../BotConfig.js';
import DiscordUtil from '../../util/DiscordUtil.js';
Expand All @@ -12,7 +12,10 @@ export default class TestingRequestEventHandler implements EventHandler<'message

// This syntax is used to ensure that `this` refers to the `TestingRequestEventHandler` object
public onEvent = async ( request: Message ): Promise<void> => {
if ( request.channel instanceof TextChannel ) {
if ( !request.channel.isSendable() ) {
return;
}
if ( request.channel instanceof GuildChannel ) {
this.logger.info( `${ request.author.tag } posted request ${ request.id } in #${ request.channel.name }` );
}

Expand Down
6 changes: 3 additions & 3 deletions src/tasks/CachedFilterFeedTask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MentionRegistry } from '../mentions/MentionRegistry.js';
import { FilterFeedConfig } from '../BotConfig.js';
import { Message, TextBasedChannel } from 'discord.js';
import { Message, SendableChannels } from 'discord.js';
import log4js from 'log4js';
import Task from './Task.js';
import { NewsUtil } from '../util/NewsUtil.js';
Expand All @@ -10,7 +10,7 @@ export default class CachedFilterFeedTask extends Task {
private static logger = log4js.getLogger( 'CachedFilterFeedTask' );
private static lastRunRegex = /\{\{lastRun\}\}/g;

private channel: TextBasedChannel;
private channel: SendableChannels;
private jql: string;
private jqlRemoved?: string;
private filterFeedEmoji: string;
Expand All @@ -22,7 +22,7 @@ export default class CachedFilterFeedTask extends Task {

private lastRun: number;

constructor( feedConfig: FilterFeedConfig, channel: TextBasedChannel ) {
constructor( feedConfig: FilterFeedConfig, channel: SendableChannels ) {
super();

this.channel = channel;
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/FilterFeedTask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MentionRegistry } from '../mentions/MentionRegistry.js';
import { FilterFeedConfig } from '../BotConfig.js';
import { Message, TextBasedChannel } from 'discord.js';
import { Message, SendableChannels } from 'discord.js';
import log4js from 'log4js';
import Task from './Task.js';
import { NewsUtil } from '../util/NewsUtil.js';
Expand All @@ -11,7 +11,7 @@ export default class FilterFeedTask extends Task {
private static logger = log4js.getLogger( 'FilterFeedTask' );
private static lastRunRegex = /\{\{lastRun\}\}/g;

private channel: TextBasedChannel;
private channel: SendableChannels;
private jql: string;
private filterFeedEmoji: string;
private title: string;
Expand All @@ -20,7 +20,7 @@ export default class FilterFeedTask extends Task {

private lastRun: number;

constructor( feedConfig: FilterFeedConfig, channel: TextBasedChannel ) {
constructor( feedConfig: FilterFeedConfig, channel: SendableChannels ) {
super();

this.channel = channel;
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/VersionFeedTask.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder, TextBasedChannel } from 'discord.js';
import { EmbedBuilder, SendableChannels } from 'discord.js';
import log4js from 'log4js';
import { VersionConfig, VersionFeedConfig } from '../BotConfig.js';
import { NewsUtil } from '../util/NewsUtil.js';
Expand Down Expand Up @@ -51,7 +51,7 @@ interface JiraVersionMap {
export default class VersionFeedTask extends Task {
private static logger = log4js.getLogger( 'VersionFeedTask' );

private channel: TextBasedChannel;
private channel: SendableChannels;
private projects: VersionConfig[];
private versionFeedEmoji: string;
private scope: number;
Expand All @@ -60,7 +60,7 @@ export default class VersionFeedTask extends Task {

private cachedVersions: JiraVersionMap = {};

constructor( feedConfig: VersionFeedConfig, channel: TextBasedChannel ) {
constructor( feedConfig: VersionFeedConfig, channel: SendableChannels ) {
super();

this.channel = channel;
Expand Down
9 changes: 6 additions & 3 deletions src/util/DiscordUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log4js from 'log4js';
import MojiraBot from '../MojiraBot.js';
import { TextChannel, Message, Guild, GuildMember, MessageReaction, User, Snowflake, PartialMessage, TextBasedChannel, ReplyMessageOptions } from 'discord.js';
import { TextChannel, Message, Guild, GuildMember, MessageReaction, User, Snowflake, PartialMessage, TextBasedChannel, MessageReplyOptions } from 'discord.js';

export default class DiscordUtil {
private static logger = log4js.getLogger( 'DiscordUtil' );
Expand Down Expand Up @@ -52,7 +52,7 @@ export default class DiscordUtil {
} );
}

public static async sendMentionMessage( origin: Message, content: ReplyMessageOptions ): Promise<void> {
public static async sendMentionMessage( origin: Message, content: MessageReplyOptions ): Promise<void> {
try {
if ( origin.reference?.messageId ) {
const replyTo = await origin.fetchReference();
Expand All @@ -64,7 +64,10 @@ export default class DiscordUtil {
await replyTo.reply( { ...content, allowedMentions: { repliedUser: false } } );
}
} else {
await origin.channel.send( content );
const channel = await origin.channel.fetch();
if ( channel.isSendable() ) {
await channel.send( content );
}
}
} catch ( e ) {
this.logger.error( e );
Expand Down

0 comments on commit 8780162

Please sign in to comment.