-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (55 loc) · 2.88 KB
/
index.js
File metadata and controls
66 lines (55 loc) · 2.88 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
const { Client, IntentsBitField, PermissionsBitField } = require("discord.js");
const UNARCHIVE_REASON = "Unarchived by Thread Unarchiver bot";
const UNARCHIVE_MESSAGE = "Thread has been unarchived. Remove me from this thread to stop unarchiving it.";
const ADDED_MESSAGE = "I will now prevent this thread from being archived.";
const NEED_SMIT_MESSAGE = "I require the permission `Send Messages in Threads` to work in";
const NEED_MT_MESSAGE = "I require the permission `Manage Threads` to unarchive";
const NEED_MT_ALT_MESSAGE = "Alternatively, you can enable `Anyone can unarchive` in the thread settings.";
const client = new Client({ intents: [IntentsBitField.Flags.Guilds] });
let ID = "";
// Start-up
client.once("ready", c => {
console.log("Logged in as", c.user.tag);
ID = c.user.id;
});
// When a thread is unarchived
client.on("threadUpdate", (oldT, T) => {
if(oldT.archived || !T.archived)
return;
if(!T.guildMembers.has(ID))
return;
let GM = T.guildMembers.get(ID);
// Check if bot can message in the thread. Required to unarchive
if(T.permissionsFor(GM).has(PermissionsBitField.Flags.SendMessagesInThreads)) { // different from T.sendable when thread is archived
if(T.unarchivable) {
T.setArchived(false, UNARCHIVE_REASON);
T.send(UNARCHIVE_MESSAGE);
console.log("Unarchived thread with ID:", T.id);
} else if(T.parent.permissionsFor(GM).has(PermissionsBitField.Flags.SendMessages)) {
T.parent.send(`${NEED_MT_MESSAGE} ${T.toString()}. ${NEED_MT_ALT_MESSAGE}`);
console.log("Ignored unarchive, since lacking permission in thread with ID:", T.id);
} else console.log("Ignored unarchive... I'm missing quite a few permissions in thread with ID:", T.id);
} else if(T.parent.permissionsFor(GM).has(PermissionsBitField.Flags.SendMessages)) {
T.parent.send(`${NEED_SMIT_MESSAGE} ${T.toString()}`); // Tell the user the bot can't message there
console.log("Ignored unarchive, since lacking permission SendMessagesInThreads in thread with ID:", T.id);
} else console.log("Ignored unarchive, since lacking permission to inform user of required permissions in thread with ID:", T.id);
});
// When the bot is added to a thread
client.on("threadMembersUpdate", (added, removed, T) => {
if(!added.has(client.user.id) || T.archived)
return;
let GM = added.get(ID).guildMember;
// Check if bot can message in the thread
if(T.sendable) {
T.send(ADDED_MESSAGE);
console.log("Added to thread with ID:", T.id);
} else if(T.parent.permissionsFor(GM).has(PermissionsBitField.Flags.SendMessages)) {
T.parent.send(`${NEED_SMIT_MESSAGE} ${T.toString()}`); // Tell the user the bot can't message there
console.log("Added, but no permission to send message in thread with ID:", T.id);
} else {
// Leave if the bot can't let the user know
T.leave();
console.log("Added, but no permission to inform user of required permissions in thread with ID:", T.id);
}
});
client.login("TOKEN HERE");