-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from freemocap/jon/cleanup
Discord UI/UX improvements and general cleanup
- Loading branch information
Showing
7 changed files
with
347 additions
and
257 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
176 changes: 89 additions & 87 deletions
176
src/interfaces/discord/services/discord-context.service.ts
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 |
---|---|---|
@@ -1,108 +1,110 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ReactionEmoji, TextChannel, ThreadChannel } from 'discord.js'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { | ||
Message, | ||
ReactionEmoji, | ||
TextChannel, | ||
ThreadChannel, | ||
ChannelType, | ||
Guild, | ||
GuildEmoji, | ||
} from 'discord.js'; | ||
import { DiscordContextRouteFactory } from '../../../core/database/collections/ai-chats/context-route.provider'; | ||
|
||
@Injectable() | ||
export class DiscordContextService { | ||
private readonly logger = new Logger(DiscordContextService.name); | ||
instructionsChannelPattern = new RegExp('bot-instructions.*', 'i'); | ||
|
||
getContextRoute(channel: TextChannel, thread?: ThreadChannel) { | ||
// TODO - Handle Direct Messages | ||
return DiscordContextRouteFactory.create( | ||
false, | ||
{ | ||
type: 'channel', | ||
contextId: channel.id, | ||
contextName: channel.name, | ||
}, | ||
{ | ||
type: 'server', | ||
contextId: channel.guild?.id, | ||
contextName: channel.guild?.name, | ||
}, | ||
{ | ||
type: 'category', | ||
contextId: channel?.parentId, | ||
contextName: channel.parent?.name, | ||
}, | ||
{ | ||
type: 'thread', | ||
contextId: thread?.id, | ||
contextName: thread?.name, | ||
}, | ||
); | ||
try { | ||
return DiscordContextRouteFactory.create( | ||
false, | ||
{ | ||
type: 'channel', | ||
contextId: channel.id, | ||
contextName: channel.name, | ||
}, | ||
{ | ||
type: 'server', | ||
contextId: channel.guild?.id, | ||
contextName: channel.guild?.name, | ||
}, | ||
{ | ||
type: 'category', | ||
contextId: channel?.parentId, | ||
contextName: channel.parent?.name, | ||
}, | ||
{ | ||
type: 'thread', | ||
contextId: thread?.id, | ||
contextName: thread?.name, | ||
}, | ||
); | ||
} catch (error) { | ||
this.logger.error(`Failed to get context route: ${error.message}`); | ||
throw error; | ||
} | ||
} | ||
|
||
async getContextInstructions(channel: TextChannel) { | ||
const channelInstructions = channel.topic || ''; | ||
const categoryInstructions = await this.getCategoryInstructions(channel); | ||
const serverInstructions = await this.getServerInstructions(channel); | ||
return [serverInstructions, categoryInstructions, channelInstructions].join( | ||
'\n\n', | ||
); | ||
} | ||
|
||
private async getCategoryInstructions(channel: TextChannel): Promise<string> { | ||
const category = channel.parent; | ||
if (!category) { | ||
return ''; | ||
try { | ||
const server = await channel.client.guilds.fetch(channel.guildId); | ||
const channelInstructions = channel.topic || ''; | ||
const categoryInstructions = await this.getInstructions( | ||
server, | ||
channel.parentId, | ||
); | ||
const serverInstructions = await this.getInstructions(server, null); | ||
return [ | ||
serverInstructions, | ||
categoryInstructions, | ||
channelInstructions, | ||
].join('\n\n'); | ||
} catch (error) { | ||
this.logger.error( | ||
`Failed to get context instructions: ${error.message} - returning empty string!`, | ||
); | ||
throw error; | ||
} | ||
const categoryChannels = channel.guild.channels.cache.filter( | ||
(ch) => ch.parentId === channel.parentId, | ||
); | ||
|
||
const botConfigChannel = categoryChannels.find((ch) => | ||
/bot-config.*?/i.test(ch.name), | ||
) as TextChannel | undefined; | ||
if (!botConfigChannel) { | ||
return ''; | ||
} | ||
|
||
const messages = await botConfigChannel.messages.fetch({ limit: 100 }); | ||
|
||
const instructionMessages = messages.filter((message) => | ||
message.reactions.cache.some((reaction) => | ||
// @ts-ignore | ||
this.isBotInstructionEmoji(reaction.emoji), | ||
), | ||
); | ||
|
||
const instructions = instructionMessages | ||
.map((message) => message.content) | ||
.join('\n'); | ||
|
||
return instructions; | ||
} | ||
|
||
private async getServerInstructions(channel: TextChannel): Promise<string> { | ||
if (!channel.guild) { | ||
return ''; | ||
} | ||
const topLevelChannels = channel.guild.channels.cache.filter( | ||
(ch) => !ch.parent, // only get channels that are not in a category | ||
); | ||
private async getInstructions( | ||
server: Guild, | ||
categoryId: string | null, | ||
): Promise<string> { | ||
try { | ||
const channels = server.channels.cache.filter( | ||
(ch) => ch.parentId === categoryId && ch.type === ChannelType.GuildText, | ||
); | ||
|
||
const botConfigChannel = topLevelChannels.find((ch) => | ||
/bot-config.*?/i.test(ch.name), | ||
) as TextChannel | undefined; | ||
if (!botConfigChannel) { | ||
return ''; | ||
} | ||
const botConfigChannel = channels.find((ch) => | ||
this.instructionsChannelPattern.test(ch.name), | ||
) as TextChannel | undefined; | ||
|
||
const messages = await botConfigChannel.messages.fetch({ limit: 100 }); | ||
if (!botConfigChannel) { | ||
return ''; | ||
} | ||
|
||
const instructionMessages = messages.filter((message) => | ||
message.reactions.cache.some((reaction) => | ||
// @ts-ignore | ||
this.isBotInstructionEmoji(reaction.emoji), | ||
), | ||
); | ||
const messages = await botConfigChannel.messages.fetch({ limit: 100 }); | ||
|
||
const instructions = instructionMessages | ||
.map((message) => message.content) | ||
.join('\n'); | ||
const instructionMessages = messages.filter((message: Message) => | ||
message.reactions.cache.some((reaction) => | ||
this.isBotInstructionEmoji(reaction.emoji), | ||
), | ||
); | ||
|
||
return instructions; | ||
return instructionMessages | ||
.map((message: Message) => message.content) | ||
.join('\n'); | ||
} catch (error) { | ||
this.logger.error(`Failed to get instructions: ${error.message}`); | ||
return ''; // In case of an error, return an empty string to keep the bot operational. | ||
} | ||
} | ||
private isBotInstructionEmoji(emoji: ReactionEmoji): boolean { | ||
|
||
private isBotInstructionEmoji(emoji: GuildEmoji | ReactionEmoji): boolean { | ||
// Ensure emoji.name is not `null` before comparison | ||
return emoji.name === '🤖'; | ||
} | ||
} |
Oops, something went wrong.