-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.mjs
More file actions
166 lines (138 loc) · 5.72 KB
/
index.mjs
File metadata and controls
166 lines (138 loc) · 5.72 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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.
//Key: Discord message ID, Value: GtS message ID
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());
if (attachment.description){
data.append("description", attachment.description);
}
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 = {
// "local_only": true, //Uncomment this when testing to not federate.
"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(Events.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;
}
if (typeof announcement_type === "string"){
// Keeping old code just in case
/*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();*/
//Editing a status and not including the 'media_ids' array unfortunately removes all of the media a post might have...
//I have to check the original post to see if there's any media, and copy it over to the edited post.
const originalStatus = await (await fetch(`https://social.lexevo.net/api/v1/statuses/${posts.get(oldMessage.id)}`, {
method: "GET",
headers: {
"Authorization": `Bearer ${process.env.FEDI_TOKEN}`
}
})).json();
//This is the copy part
const originalMediaIDs = originalStatus.media_attachments.map(originalAttachmentDetails => originalAttachmentDetails.id);
const reqBody = {
"status": `Resonite ${announcement_type} post: ${newMessage.content}\n\n#resonite`,
"media_ids": originalMediaIDs
}
const res = await fetch(`https://social.lexevo.net/api/v1/statuses/${posts.get(oldMessage.id)}`, {
method: "PUT",
headers: {
"Authorization": `Bearer ${process.env.FEDI_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify(reqBody)
});
const resBody = await res.json();
//The edited discord message id changes, but the edited GtS status ID does not, so I only need to set/update the map.
posts.set(newMessage.id, resBody.id);
}
});
botClient.login(process.env.DISCORD_TOKEN);