-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex-g2d.js
80 lines (71 loc) · 2.43 KB
/
index-g2d.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
const discord = require('discord.js');
const express = require('express');
const bodyParser = require('body-parser')
const USERS = require('./USERS.json');
const {
PORT, EMOJI_URL, DISCORD_WEBHOOK_URL
} = process.env;
function getEmojiUrl(pack, index) {
let url = EMOJI_URL;
url = url.replace('%PACK%', pack.toString());
url = url.replace('%INDEX%', index.toString());
return url;
}
const webhookClient = new discord.WebhookClient({
url: DISCORD_WEBHOOK_URL,
});
const groupmeApp = express();
groupmeApp.use(bodyParser.json());
groupmeApp.use(express.static('assets'))
groupmeApp.post('/g2d', async (req, res) => {
const m = req.body;
if (m.sender_type === 'bot') {
console.log('***** Received bot message from GroupMe; it will NOT be forwarded to Discord');
res.end();
return;
}
console.log('***** Received message from GroupMe:', JSON.stringify(m,null,4));
let text = m.text;
const embeds = [];
for (let a of m.attachments) {
if (a.type === 'image') {
//text += ' ' + a.url;
const imageEmbed = new discord.EmbedBuilder().setImage(a.url);
embeds.push(imageEmbed);
}
else if (a.type === 'emoji') {
const ph = a.placeholder;
for (let cm of a.charmap) {
text = text.replace(ph, ''); // previously :capital_abcd:
const emojiEmbed = new discord.EmbedBuilder().setImage( getEmojiUrl(...cm) );
embeds.push(emojiEmbed);
}
}
else if (a.type === 'mentions') {
for (let i in a.user_ids) {
const uidg = a.user_ids[i];
const [start, len] = a.loci[i];
const needle = m.text.substring(start, start+len);
const uidd = USERS.filter(u => u.uidg===uidg)[0].uidd;
text = text.replace(needle, `<@${uidd}>`);
}
}
}
try {
await webhookClient.send({
content: text,
username: `${m.name} (from GroupMe)`,
avatarURL: m.avatar_url,
embeds,
});
} catch (err) {
if (err instanceof discord.DiscordAPIError) {
console.log(JSON.stringify(err.rawError));
}
}
res.end();
});
groupmeApp.post('/g2d-noop-*', (req, res) => res.end());
groupmeApp.listen(PORT, () => {
console.log(`***** GroupMe listener app listening on port ${PORT}`);
});