-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
125 lines (106 loc) · 3.94 KB
/
index.mjs
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
import dotenv from 'dotenv';
import { Client, Events, GatewayIntentBits } from 'discord.js';
dotenv.config();
//An in-memory map to keep track of which discord message links to which fedi post.
//Used to find which fedi post to reply to when discord message is edited.
let posts = new Map();
const botClient = new Client({intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]});
botClient.on(Events.MessageCreate, async (message) => {
let announcement_type = null;
let attachment_ids = [];
switch (message.channelId){
case "1171348411589607434": //#resonite-announcements
announcement_type = "announcement";
break;
case "1171348449367687239": //#resonite-updates
announcement_type = "update";
break;
case "1171348477201088532": //#resonite-devlog
announcement_type = "devlog";
break;
case "1173566951319146536": //#test
announcement_type = "testing";
break;
default:
break;
}
//Do some things here
if (typeof announcement_type === "string"){
//Check to see if post has media attachment.
if (message.attachments){
for (const attachment of message.attachments.values()){
let data = new FormData();
let res = await fetch(attachment.url);
data.append("file", await res.blob());
let res2 = await fetch("https://social.lexevo.net/api/v2/media", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FEDI_TOKEN}`
},
body: data
});
if (res2.status === 200){
const resJson = await res2.json();
attachment_ids.push(resJson.id);
}
}
}
//TODO: Check for rapid succession of messages
const bodyBuilder = {
"status": `Resonite ${announcement_type} post: ${message.content}\n\n#resonite`,
"media_ids": attachment_ids,
"visibility": "public"
};
const res = await fetch("https://social.lexevo.net/api/v1/statuses", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FEDI_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify(bodyBuilder)
});
const resBody = await res.json();
posts.set(message.id, resBody.id);
}
});
botClient.on("messageUpdate", async (oldMessage, newMessage) => {
let announcement_type = null;
//Check to see if message was edited
switch (newMessage.channelId){
case "1171348411589607434": //#resonite-announcements
announcement_type = "announcement";
break;
case "1171348449367687239": //#resonite-updates
announcement_type = "update";
break;
case "1171348477201088532": //#resonite-devlog
announcement_type = "devlog";
break;
case "1173566951319146536": //#test
announcement_type = "testing";
break;
default:
break;
}
const bodyBuilder = {
"status": `Edited resonite ${announcement_type} post: ${newMessage.content}`,
"in_reply_to_id": posts.get(oldMessage.id),
"visibility": "unlisted"
};
const res = await fetch("https://social.lexevo.net/api/v1/statuses", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FEDI_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify(bodyBuilder)
});
const resBody = await res.json();
posts.set(newMessage.id, resBody.id);
});
botClient.login(process.env.DISCORD_TOKEN);