-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
193 lines (183 loc) · 6 KB
/
index.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const Discord = require("discord.js");
const Revolt = require("revolt.js");
const config = require("./config.json");
const disc = new Discord.Client({
intents: Object.values(Discord.Intents.FLAGS), // fuck you discord lmao
});
disc.login(config.token.discord);
const rev = new Revolt.Client();
rev.loginBot(config.token.revolt);
let ready = 0;
async function done() {
console.log("Ready to work.");
let discServer = await disc.guilds.fetch(config.server.discord);
let revServer = await rev.servers.fetch(config.server.revolt);
if (!discServer || !revServer) return console.log("Error fetching servers.");
console.log("Fetching data...");
await discServer.channels.fetch().catch(console.error);
await discServer.emojis.fetch().catch(console.error);
await revServer.fetchMembers();
console.log("Fetched data.");
function discFindChannel(id) {
// finds a discord channel given a revolt id
return discServer.channels.cache.filter((c) => c.isText()).find((c) => c.topic === id);
}
function discordEmbed(message) {
let embed = new Discord.MessageEmbed();
embed.setAuthor(message.author.username, message.author.generateAvatarURL());
if (message.content) embed.setDescription(parseRevoltMentions(message.content));
let file = message.attachments
?.filter((a) => a.content_type.startsWith("image"))
.map((a) => `https://autumn.revolt.chat/attachments/${a._id}/${a.filename}`);
if (file && file.length) embed.setImage(file[0]);
embed.setTimestamp();
embed.setFooter(message._id);
return embed;
}
function parseRevoltMentions(content) {
let newContent = "";
content.split("<").forEach((c) => {
c.split(">").forEach((cc) => {
if (!cc) return;
if (cc.length !== 27 || (!cc.startsWith("@") && !cc.startsWith("#"))) {
newContent += cc; // needs better imp
} else {
let modifier = [...cc][0];
let id = cc.substring(1);
switch (modifier) {
case "@":
let user = rev.users.get(id) || {};
newContent += `@${user.username || "unknownuser"}`;
break;
case "#":
let channel = revServer.channels.get(id) || {};
newContent += `#${channel.name || "unknown-channel"}`;
break;
default:
newContent += cc;
break;
}
}
});
});
return newContent;
}
function revoltTimestamp() {
// format timestamp for revolt
return new Date().toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
});
}
function sync() {
let revChan = revServer.channels.filter((c) => c.channel_type == "TextChannel");
console.log("Syncing channels...");
return new Promise((_res) => {
new Promise((res) => {
let done = 0;
revChan.forEach(async (c) => {
let channel = discFindChannel(c._id);
if (!channel) {
console.log(`Did not find channel ${c.name} in discord, creating it..."`);
await discServer.channels
.create(c.name, { type: "GUILD_TEXT", topic: c._id })
.catch(console.error);
}
done++;
if (revChan.length == done) {
console.log("Synced channels.");
res();
}
});
}).then(() => {
console.log("Cleaning up channels...");
new Promise((res) => {
let chan = discServer.channels.cache;
let through = 0;
chan.forEach(async (c) => {
if (!c.topic || !revChan.map((c) => c._id).includes(c.topic)) {
await c.delete().catch(console.error);
}
through++;
if (through == chan.size) {
console.log("Cleaned up channels.");
res();
}
});
}).then(() => {
console.log("Sycing server data...");
new Promise(async (res) => {
if (discServer.name !== revServer.name)
await discServer.setName(revServer.name).catch(console.error);
if (!discServer.icon)
await discServer.setIcon(revServer.generateIconURL()).catch(console.error); // needs better detection
console.log("Synced server data.");
_res();
});
});
});
});
}
sync().then(() => {
console.log("Syncing done.");
rev.on("message", async (message) => {
if (
message.channel.server_id !== revServer._id ||
message.author.bot ||
(!message.content && !message.attachments)
)
return;
let chan = discFindChannel(message.channel_id);
if (!chan) {
console.log("Failed to find message channel in discord!");
sync();
return;
}
chan
.send({
embeds: [discordEmbed(message)],
})
.catch(console.error);
});
disc.on("messageCreate", async (message) => {
if (
message.guild.id !== discServer.id ||
message.author.id == disc.user.id ||
(!message.content && !message.attachments.size)
)
return;
let chan = rev.channels.get(message.channel.topic);
if (!chan) {
console.log("Failed to find message channel in revolt!");
sync();
return;
}
let files = message.attachments.map((a) => a.proxyURL);
await chan
.sendMessage(
`**${message.author.tag}** [${revoltTimestamp()}]
> ${message.content}
${files.join(", ")}`
)
.catch(console.error);
await message.react("📩").catch(console.error);
});
let syncOn = ["ChannelCreate", "ServerUpdate", "ChannelDelete"];
rev.on("packet", (p) => {
if (syncOn.includes(p.type)) sync();
});
});
}
disc.on("ready", () => {
ready++;
console.log(`${disc.user.tag} is online!`);
if (ready == 2) done();
});
rev.on("connected", () => {
ready++;
console.log(`Revolt is online!`);
if (ready == 2) done();
});