refactor: update message forwarding logic to support channel-based matching alongside role-based matching; enhance configuration structure to include channels; update documentation and example config
This commit is contained in:
parent
c4514cd4c4
commit
7ed373337f
7 changed files with 97 additions and 22 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import type { Message } from 'discord.js-selfbot-v13'
|
||||
import type { AttachmentInfo, RoleConfig } from '../types.js'
|
||||
import type { AttachmentInfo, ChannelConfig, RoleConfig } from '../types.js'
|
||||
import { config } from '../config.js'
|
||||
import { forwardMessage } from '../telegram/sender.js'
|
||||
import { discord } from './client.js'
|
||||
|
|
@ -8,6 +8,12 @@ export function setupHandlers(): void {
|
|||
discord.on('messageCreate', handleMessage)
|
||||
}
|
||||
|
||||
interface MatchResult {
|
||||
topicId: number
|
||||
label: string
|
||||
type: 'channel' | 'role'
|
||||
}
|
||||
|
||||
async function handleMessage(message: Message): Promise<void> {
|
||||
if (!message.guild || !message.member)
|
||||
return
|
||||
|
|
@ -16,8 +22,24 @@ async function handleMessage(message: Message): Promise<void> {
|
|||
if (!serverConfig)
|
||||
return
|
||||
|
||||
const matchedRole = findHighestPriorityRole(message.member.roles.cache, serverConfig.roles)
|
||||
if (!matchedRole)
|
||||
// Check channels first (higher priority), then roles
|
||||
let match: MatchResult | null = null
|
||||
|
||||
if (serverConfig.channels) {
|
||||
const channelMatch = findMatchingChannel(message.channel.id, serverConfig.channels)
|
||||
if (channelMatch) {
|
||||
match = { topicId: channelMatch.topicId, label: channelMatch.name, type: 'channel' }
|
||||
}
|
||||
}
|
||||
|
||||
if (!match && serverConfig.roles) {
|
||||
const roleMatch = findHighestPriorityRole(message.member.roles.cache, serverConfig.roles)
|
||||
if (roleMatch) {
|
||||
match = { topicId: roleMatch.topicId, label: roleMatch.name, type: 'role' }
|
||||
}
|
||||
}
|
||||
|
||||
if (!match)
|
||||
return
|
||||
|
||||
const attachments: AttachmentInfo[] = message.attachments.map(att => ({
|
||||
|
|
@ -27,24 +49,32 @@ async function handleMessage(message: Message): Promise<void> {
|
|||
}))
|
||||
|
||||
const messageLink = `https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.id}`
|
||||
const channelName = 'name' in message.channel ? (message.channel.name ?? 'unknown') : 'DM'
|
||||
|
||||
try {
|
||||
await forwardMessage({
|
||||
topicId: matchedRole.topicId,
|
||||
topicId: match.topicId,
|
||||
author: message.author.displayName ?? message.author.username,
|
||||
role: matchedRole.name,
|
||||
channel: 'name' in message.channel ? (message.channel.name ?? 'unknown') : 'DM',
|
||||
role: match.type === 'role' ? match.label : null,
|
||||
channel: channelName,
|
||||
content: message.content,
|
||||
attachments,
|
||||
messageLink,
|
||||
})
|
||||
console.log(`Forwarded message from ${message.author.tag} (${matchedRole.name}) in ${serverConfig.name}`)
|
||||
console.log(`Forwarded message from ${message.author.tag} (${match.type}: ${match.label}) in ${serverConfig.name}`)
|
||||
}
|
||||
catch (err) {
|
||||
console.error('Failed to forward message:', err)
|
||||
}
|
||||
}
|
||||
|
||||
function findMatchingChannel(
|
||||
channelId: string,
|
||||
configChannels: ChannelConfig[],
|
||||
): ChannelConfig | null {
|
||||
return configChannels.find(ch => ch.id === channelId) ?? null
|
||||
}
|
||||
|
||||
function findHighestPriorityRole(
|
||||
memberRoles: Map<string, unknown>,
|
||||
configRoles: RoleConfig[],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue