-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordHelper.js
127 lines (121 loc) · 3.75 KB
/
discordHelper.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import PromisE from './PromisE'
import { isObjArr } from './utils'
// import { Client, Message } from 'discord.js'
// import { BehaviorSubject } from 'rxjs'
// const token = process.env.Discord_Bot_Token
// // Create an instance of a Discord client
// const client = new Client()
// // WIP
// class DiscordBot {
// constructor(token) {
// this.client = new Client()
// this.token = token
// this.rxIsReady = new BehaviorSubject(false)
// }
// login() {
// return new Promise(async (resolve, reject) => {
// const loginResult = await this.client.login(this.token)
// console.log({ loginResult })
// const onResult = this.client.on('ready', (x) => {
// console.log('Bot ready', x)
// this.rxIsReady.next(true)
// })
// console.log({ onResult })
// })
// }
// async sendMessage() {
// await this.login()
// new Message(this.client, {}, '')
// }
// }
// export default new DiscordBot(token)
/**
* @name sendMessage
* @summary send message to Discord channel using webhook
*
* @param {String|{
* color: String,
* description: String,
* timestamp: String,
* title: String,
* url: String,
* }[]} content content or embeds
* @param {*} tag
* @param {*} username
* @param {*} webhookUrl
* @param {*} avatar_url
* @param {*} timeout
* @param {*} contentRedacted
* @param {*} split
* @param {*} limit
*
* @returns
*/
export const sendMessage = async (
content = '',
tag,
split = false, // whether to split content greater that limit (default: 2000 characters) into multiple messages
limit = 2000,
username = process.env.DISCORD_WEBHOOK_USERNAME || 'Logger',
webhookUrl = process.env.DISCORD_WEBHOOK_URL,
avatar_url = process.env.DISCORD_WEBHOOK_AVATAR_URL,
timeout = 60000,
) => {
const embeds = isObjArr(content) && content.length > 0
? content
: undefined
content = embeds
? ''
: `${content || ''}`
if (!content && embeds.length === 0) throw new Error('Empty content')
const contentRedacted = sendMessage.redactRegex
? content.replace(sendMessage.redactRegex, '')
: content
if (contentRedacted.length > limit) {
if (split) {
const embedMarkup = '>>> '
const isEmbed = contentRedacted.startsWith(embedMarkup)
limit = isEmbed
? limit - 4
: limit
const numMessages = Math.ceil(contentRedacted.length / limit)
for (let i = 0;i < numMessages;i++) {
const startIndex = i * limit
const part = contentRedacted.slice(startIndex, startIndex + limit)
await sendMessage(
(isEmbed ? embedMarkup : '') + part,
tag,
username,
webhookUrl,
avatar_url,
timeout,
)
}
return
}
contentRedacted = contentRedacted.slice(0, limit)
}
return await PromisE
.post(
webhookUrl + '?wait=true',
{
avatar_url,
...!!contentRedacted && { content: contentRedacted },
...!!embeds && { embeds },
username
},
{},
timeout,
false,// `true` will throw error
)
.catch(err =>
console.error(
tag,
'[DiscordWebhook]: failed to send message.',
{ content: contentRedacted, embeds, tag },
err
)
// ToDo: save as JSON and re-attempt later??
)
}
sendMessage.redactRegex = null