generated from Borodutch/backend-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendPostForModeration.ts
More file actions
74 lines (73 loc) · 2.08 KB
/
sendPostForModeration.ts
File metadata and controls
74 lines (73 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as translate from '@vitalets/google-translate-api'
import { Colors } from 'discord.js'
import { EmbedBuilder } from '@discordjs/builders'
import ModerationLevel from '@/models/ModerationLevel'
import PostingService from '@/models/PostingService'
import actionButtonBuilder from '@/helpers/actionButtonBuilder'
import channels from '@/helpers/channels'
import getSymbol from '@/helpers/getSymbol'
import logError from '@/helpers/logError'
import sendErrorOnDiscord from '@/helpers/sendErrorOnDiscord'
import typeToChannel from '@/helpers/typeToChannel'
export default async function ({
blockchainId,
contractAddress,
derivativeAddress,
moderationLevel,
postingService,
reasons,
text,
}: {
blockchainId: number
derivativeAddress: string
text: string
contractAddress: string
reasons: string
postingService: PostingService
moderationLevel: ModerationLevel
}) {
const channel = await typeToChannel[postingService]()
const row = actionButtonBuilder({
blockchainId,
contractAddress,
postingService,
})
const symbol = await getSymbol(derivativeAddress)
let english = true
let translationText = ''
reasons.split(', ').forEach((reason) => {
if (reason.includes('not English')) {
english = false
}
})
if (!english) {
const translation = await translate(text, { to: 'en' })
translationText = '\nTranslation: ' + translation?.text
}
const embed = new EmbedBuilder()
.setColor(Colors.Default)
.setTitle(`Blockchain post #${blockchainId} from ${symbol}`)
.setDescription(
`${text}${
moderationLevel === ModerationLevel.medium
? `\n\nModeration reasons: ${reasons}${translationText}`
: ''
}`
)
try {
await channel.send({
components: [row],
embeds: [embed],
})
} catch (error) {
logError('Sending post to Discord', error)
const errorChannel = await channels.error()
await sendErrorOnDiscord({
blockchainId,
channel: errorChannel,
derivativeAddress,
error,
extraTitle: `sending post to Discord for ${postingService}`,
})
}
}