-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Update thread name on message updates (#468)
- Loading branch information
1 parent
1f6e65f
commit 51acc1b
Showing
6 changed files
with
76 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ClientEvents } from "discord.js"; | ||
import NeedleEventListener from "../models/NeedleEventListener.js"; | ||
import ListenerRunType from "../models/enums/ListenerRunType.js"; | ||
import ThreadCreationService from "../services/ThreadCreationService.js"; | ||
import NeedleBot from "../NeedleBot.js"; | ||
import ObjectFactory from "../ObjectFactory.js"; | ||
import MessageVariables from "../models/MessageVariables.js"; | ||
|
||
export default class MessageUpdateEventListener extends NeedleEventListener { | ||
public readonly name = "messageUpdate"; | ||
public readonly runType = ListenerRunType.EveryTime; | ||
|
||
private readonly threadCreator: ThreadCreationService; | ||
|
||
constructor(bot: NeedleBot) { | ||
super(bot); | ||
this.threadCreator = ObjectFactory.createThreadCreationService(true); | ||
} | ||
|
||
public async handle([oldMessage, newMessage]: ClientEvents["messageUpdate"]): Promise<void> { | ||
await newMessage.fetch(); | ||
|
||
if (!newMessage.hasThread) return; | ||
if (newMessage.thread?.ownerId !== this.bot.client.user?.id) return; | ||
if (!oldMessage.inGuild() || !newMessage.inGuild()) return; | ||
|
||
const guildConfig = this.bot.configs.get(newMessage.guildId); | ||
const channelConfig = guildConfig.threadChannels?.find( | ||
c => c.channelId === newMessage.channelId || c.channelId === newMessage.channel.parentId, | ||
); | ||
|
||
if (!channelConfig) return; | ||
|
||
const oldMessageVariables = new MessageVariables() | ||
.setChannel(oldMessage.channel) | ||
.setUser(oldMessage.member ?? oldMessage.author) | ||
.setThread(oldMessage.thread); | ||
|
||
const newMessageVariables = new MessageVariables() | ||
.setChannel(newMessage.channel) | ||
.setUser(newMessage.member ?? newMessage.author) | ||
.setThread(newMessage.thread); | ||
|
||
const expectedOldName = await this.threadCreator.getThreadName(oldMessage, channelConfig, oldMessageVariables); | ||
const actualOldName = oldMessage.thread?.name ?? ""; | ||
// If the previous title is the default title, do not overwrite it | ||
if (expectedOldName !== actualOldName) return; | ||
|
||
await this.threadCreator.createOrUpdateThreadOnMessage(newMessage, newMessageVariables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters