diff options
Diffstat (limited to 'packages/gateway/src')
| -rw-r--r-- | packages/gateway/src/listeners/messageCreate/dailyConversationStarter.ts | 85 | ||||
| -rw-r--r-- | packages/gateway/src/listeners/messageCreate/personaRandomMessage.ts | 87 |
2 files changed, 74 insertions, 98 deletions
diff --git a/packages/gateway/src/listeners/messageCreate/dailyConversationStarter.ts b/packages/gateway/src/listeners/messageCreate/dailyConversationStarter.ts index 1f0c80a..43ef429 100644 --- a/packages/gateway/src/listeners/messageCreate/dailyConversationStarter.ts +++ b/packages/gateway/src/listeners/messageCreate/dailyConversationStarter.ts @@ -19,38 +19,48 @@ class DailyConversationStarterSystem { isActive: false, }; private webhookClient: WebhookClient | null = null; - private webhookId: string | null = null; - private webhookToken: string | null = null; private client: Client | null = null; constructor() { // } - public setClient(client: Client) { + public async setClient(client: Client) { this.client = client; + await this.initializePersistentWebhook(); + if (ENABLED_PRIMER_MESSAGE) this.sendPrimerMessage(); } - private async initializeWebhook(personaName: string, personaAvatar: string) { + private async initializePersistentWebhook() { if (!this.client) return; try { const channel = this.client.channels.cache.get(TARGET_CHANNEL_ID); - if (channel && "createWebhook" in channel) { - const webhook = await channel.createWebhook({ - name: personaName, - avatar: personaAvatar, - }); + if (channel && "fetchWebhooks" in channel) { + const webhooks = await channel.fetchWebhooks(); + const existingWebhook = webhooks.find( + (webhook) => webhook.name === "UmaBot Conversation", + ); + + if (existingWebhook) { + this.webhookClient = new WebhookClient({ url: existingWebhook.url }); + } else if ("createWebhook" in channel) { + const webhook = await channel.createWebhook({ + name: "UmaBot Conversation", + avatar: "https://cdn.discordapp.com/embed/avatars/0.png", + }); - this.webhookClient = new WebhookClient({ url: webhook.url }); - this.webhookId = webhook.id; - this.webhookToken = webhook.token; + this.webhookClient = new WebhookClient({ url: webhook.url }); + } } } catch (error) { - console.error("Failed to create conversation starter webhook:", error); + console.error( + "Failed to initialize persistent conversation starter webhook:", + error, + ); } } @@ -64,17 +74,19 @@ class DailyConversationStarterSystem { false, ); - await this.initializeWebhook(randomPersona.name, randomPersona.avatar); - if (!this.webhookClient) { console.error( - "Failed to create webhook for conversation starter primer message", + "Failed to send conversation starter primer message - webhook not initialized", ); return; } - const sentMessage = await this.webhookClient.send(formattedMessage); + const sentMessage = await this.webhookClient.send({ + content: formattedMessage, + username: randomPersona.name, + avatarURL: randomPersona.avatar, + }); const sentMessageLink = `https://discord.com/channels/${this.client!.guilds.cache.first()?.id}/${TARGET_CHANNEL_ID}/${sentMessage.id}`; await sendPersonaLog( @@ -87,8 +99,6 @@ class DailyConversationStarterSystem { ); this.tracker.lastMessageTime = Date.now(); - - await this.cleanupWebhook(); } catch (error) { console.error( "Failed to send conversation starter primer message:", @@ -98,7 +108,7 @@ class DailyConversationStarterSystem { } public async handleMessage(message: Message): Promise<void> { - if (!this.client) this.setClient(message.client); + if (!this.client) await this.setClient(message.client); if (message.channelId !== TARGET_CHANNEL_ID) return; @@ -125,17 +135,19 @@ class DailyConversationStarterSystem { false, ); - await this.initializeWebhook(randomPersona.name, randomPersona.avatar); - if (!this.webhookClient) { console.error( - "Failed to create webhook for conversation starter message", + "Failed to send conversation starter message - webhook not initialized", ); return; } - const sentMessage = await this.webhookClient.send(formattedMessage); + const sentMessage = await this.webhookClient.send({ + content: formattedMessage, + username: randomPersona.name, + avatarURL: randomPersona.avatar, + }); const sentMessageLink = `https://discord.com/channels/${this.client!.guilds.cache.first()?.id}/${TARGET_CHANNEL_ID}/${sentMessage.id}`; await sendPersonaLog( @@ -148,36 +160,11 @@ class DailyConversationStarterSystem { ); this.tracker.lastMessageTime = Date.now(); - - await this.cleanupWebhook(); } catch (error) { console.error("Failed to send conversation starter message:", error); } } - private async cleanupWebhook() { - try { - if (this.webhookId && this.webhookToken && this.client) { - const channel = this.client.channels.cache.get(TARGET_CHANNEL_ID); - - if (channel && "fetchWebhooks" in channel) { - const webhooks = await channel.fetchWebhooks(); - const webhook = webhooks.get(this.webhookId); - - if (webhook) await webhook.delete(); - } - } - } catch (error) { - console.error("Failed to cleanup conversation starter webhook:", error); - } finally { - this.webhookClient?.destroy(); - - this.webhookClient = null; - this.webhookId = null; - this.webhookToken = null; - } - } - public destroy(): void { this.webhookClient?.destroy(); } diff --git a/packages/gateway/src/listeners/messageCreate/personaRandomMessage.ts b/packages/gateway/src/listeners/messageCreate/personaRandomMessage.ts index e23d68e..c5d9d0b 100644 --- a/packages/gateway/src/listeners/messageCreate/personaRandomMessage.ts +++ b/packages/gateway/src/listeners/messageCreate/personaRandomMessage.ts @@ -23,38 +23,45 @@ class PersonaRandomMessageSystem { isActive: false, }; private webhookClient: WebhookClient | null = null; - private webhookId: string | null = null; - private webhookToken: string | null = null; private client: Client | null = null; constructor() { // } - public setClient(client: Client) { + public async setClient(client: Client) { this.client = client; + await this.initializePersistentWebhook(); + if (ENABLED_PRIMER_MESSAGE) this.sendPrimerMessage(); } - private async initializeWebhook(personaName: string, personaAvatar: string) { + private async initializePersistentWebhook() { if (!this.client) return; try { const channel = this.client.channels.cache.get(TARGET_CHANNEL_ID); - if (channel && "createWebhook" in channel) { - const webhook = await channel.createWebhook({ - name: personaName, - avatar: personaAvatar, - }); - - this.webhookClient = new WebhookClient({ url: webhook.url }); - this.webhookId = webhook.id; - this.webhookToken = webhook.token; + if (channel && "fetchWebhooks" in channel) { + const webhooks = await channel.fetchWebhooks(); + const existingWebhook = webhooks.find( + (webhook) => webhook.name === "UmaBot Persona", + ); + + if (existingWebhook) { + this.webhookClient = new WebhookClient({ url: existingWebhook.url }); + } else if ("createWebhook" in channel) { + const webhook = await channel.createWebhook({ + name: "UmaBot Persona", + avatar: "https://cdn.discordapp.com/embed/avatars/0.png", + }); + + this.webhookClient = new WebhookClient({ url: webhook.url }); + } } } catch (error) { - console.error("Failed to create persona webhook:", error); + console.error("Failed to initialize persistent persona webhook:", error); } } @@ -68,15 +75,19 @@ class PersonaRandomMessageSystem { true, ); - await this.initializeWebhook(randomPersona.name, randomPersona.avatar); - if (!this.webhookClient) { - console.error("Failed to create webhook for primer message"); + console.error( + "Failed to send primer message - webhook not initialized", + ); return; } - const sentMessage = await this.webhookClient.send(formattedMessage); + const sentMessage = await this.webhookClient.send({ + content: formattedMessage, + username: randomPersona.name, + avatarURL: randomPersona.avatar, + }); const sentMessageLink = `https://discord.com/channels/${this.client!.guilds.cache.first()?.id}/${TARGET_CHANNEL_ID}/${sentMessage.id}`; await sendPersonaLog( @@ -87,14 +98,13 @@ class PersonaRandomMessageSystem { true, sentMessageLink, ); - await this.cleanupWebhook(); } catch (error) { console.error("Failed to send primer persona message:", error); } } public async handleMessage(message: Message): Promise<void> { - if (!this.client) this.setClient(message.client); + if (!this.client) await this.setClient(message.client); if (message.channelId !== TARGET_CHANNEL_ID) return; @@ -122,15 +132,19 @@ class PersonaRandomMessageSystem { true, ); - await this.initializeWebhook(randomPersona.name, randomPersona.avatar); - if (!this.webhookClient) { - console.error("Failed to create webhook for persona message"); + console.error( + "Failed to send persona message - webhook not initialized", + ); return; } - const sentMessage = await this.webhookClient.send(formattedMessage); + const sentMessage = await this.webhookClient.send({ + content: formattedMessage, + username: randomPersona.name, + avatarURL: randomPersona.avatar, + }); const sentMessageLink = `https://discord.com/channels/${this.client!.guilds.cache.first()?.id}/${TARGET_CHANNEL_ID}/${sentMessage.id}`; await sendPersonaLog( @@ -144,36 +158,11 @@ class PersonaRandomMessageSystem { this.tracker.messageCount = 0; this.tracker.lastRandomMessageTime = Date.now(); - - await this.cleanupWebhook(); } catch (error) { console.error("Failed to send persona message:", error); } } - private async cleanupWebhook() { - try { - if (this.webhookId && this.webhookToken && this.client) { - const channel = this.client.channels.cache.get(TARGET_CHANNEL_ID); - - if (channel && "fetchWebhooks" in channel) { - const webhooks = await channel.fetchWebhooks(); - const webhook = webhooks.get(this.webhookId); - - if (webhook) await webhook.delete(); - } - } - } catch (error) { - console.error("Failed to cleanup persona webhook:", error); - } finally { - this.webhookClient?.destroy(); - - this.webhookClient = null; - this.webhookId = null; - this.webhookToken = null; - } - } - public destroy(): void { this.webhookClient?.destroy(); } |