UNPKG

2.64 kBPlain TextView Raw
1import cleanTemporaryFiles from '../messenger/cleanTemporaryFiles'
2
3const log = logger.withScope('discord:listener')
4
5import { Message } from 'discord.js'
6import { fromDiscord as createMessage } from '../createMessage'
7import handleCommand from '../handleCommand'
8import { sendMessage as sendDiscordMessage } from './'
9import { sendMessage as sendMessengerMessage } from '../messenger'
10import { checkIgnoredSequences, checkMKeep } from '../utils'
11
12export default async (message: Message) => {
13 if (config.paused) {
14 log.info('Got a Discord messenger (paused)')
15 return
16 }
17
18 log.info('Got a Discord message')
19 log.trace('message', message, 1)
20
21 if (checkMKeep(message.cleanContent)) return log.debug('m!keep received, ignoring.')
22 if (config.discord.ignoreBots && message.author.bot) return log.debug('config.discord.ignoreBots enabled and author is a bot.')
23 if (Array.isArray(config.discord.ignoredUsers) && config.discord.ignoredUsers.includes(message.author.id)) return log.debug('author is in config.discord.ignoredUsers.')
24
25 // don't want to echo bot's messages
26 if (discord.webhooks.has(message.author.id) || message.author.username === discord.client.user.username) return log.debug('Message was sent by Miscord or its webhook')
27
28 if (Array.isArray(discord.channels.command) && discord.channels.command.some(channel => channel.id === message.channel.id)) {
29 if (message.channel.type === 'dm') return handleCommand(message)
30 if (message.mentions.users && message.mentions.users.has(discord.client.user.id)) {
31 message.content = message.content.replace(new RegExp(`<@!?${discord.client.user.id}>`), '')
32 return handleCommand(message)
33 }
34 }
35
36 if (checkIgnoredSequences(message.cleanContent)) return log.debug('found an ignored sequence, ignoring.')
37
38 // make sure this channel is meant for the bot
39 const connection = connections.getWith(message.channel.id)
40 if (!connection) return log.debug('Channel not found in bot\'s connections')
41
42 // send message to threads specified in the connections
43 {
44 if (message.type === 'PINS_ADD' && !config.messenger.sendPinned) {
45 log.debug(`"pinned" messages are disabled, ignoring`)
46 } else {
47 const threads = connection.getWritableThreads()
48 const data = await createMessage.toMessenger(message)
49 Promise.all(threads.map(thread => sendMessengerMessage(thread, data)))
50 .then(() => cleanTemporaryFiles(data))
51 }
52 }
53 {
54 const channels = connection.getOtherWritableChannels(message.channel.id)
55 const data = createMessage.toDiscord(message)
56 channels.forEach(endpoint => sendDiscordMessage(endpoint.id, data))
57 }
58}