-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (149 loc) · 4.99 KB
/
index.js
File metadata and controls
159 lines (149 loc) · 4.99 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
// Require the necessary discord.js classes
const { Client, GatewayIntentBits } = require("discord.js");
const { enable } = require("./src/commands/enable");
const { disable } = require("./src/commands/disable");
const { list } = require("./src/commands/list");
const { ThreadChannel } = require("./src/db");
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
// When the client is ready, run this code (only once)
client.once("ready", () => {
console.log("Ready!");
});
// for "docker stop"
process.on("SIGTERM", () => {
client.destroy();
});
client.on("messageCreate", async (message) => {
console.log(message.channel.isThread());
if (message.channel.isThread() || message.author.bot || message.hasThread) {
console.log(
"Nothing to be done because no text/bot is author/already has thread"
);
return;
}
const channel = await ThreadChannel.findOne({
channelId: message.channelId,
guildId: message.guildId,
});
if (!channel) {
return;
}
const date = new Date(message.createdTimestamp).toString();
let threadName;
switch (channel.naming) {
case "nameanddate":
threadName = `${message.author.username}, ${date.slice(0, 15)}`;
break;
case "messagecontent":
threadName = message.content.slice(0, 100);
break;
default:
threadName = `${message.author.username}, ${date.slice(0, 15)}`;
break;
}
try {
const thread = await message.startThread({
name: threadName,
autoArchiveDuration: 1440,
});
if (channel.channelMessage) {
await thread.send(channel.channelMessage);
}
await thread.leave();
} catch (error) {
console.log(error.message);
}
});
// listener for slash commands
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
if (!interaction.member.permissions.has("MANAGE_ROLES")) {
interaction.reply(
`You don't have manage roles permissions that are required to execute this command.`
);
return;
}
// switch that runs the correct code for used command
switch (interaction.commandName) {
case "enable": {
// defers the reply so that the reply still works normally even if the backend is slower than 3 seconds
await interaction.deferReply({ ephemeral: true });
let channelMessage = "";
channelMessage = interaction.options.getString("message");
const naming = interaction.options.getString("naming");
const channelName = interaction.guild.channels.cache.get(
interaction.channelId
).name;
// the toplevel try/catch block that forwards every error to the user
try {
// call function
await enable(
channelName,
interaction.channelId,
interaction.guildId,
channelMessage,
naming
);
await interaction.editReply(
`You successfully enabled autothreading in this channel.`
);
} catch (error) {
// forwards errors to the user
await interaction.editReply(
`There was an error, here is the error message: ${error}`
);
}
break;
}
case "disable": {
// defers the reply so that the reply still works normally even if the backend is slower than 3 seconds
await interaction.deferReply({ ephemeral: true });
// the toplevel try/catch block that forwards every error to the user
try {
// calls the unsubscribe function
await disable(interaction.channelId, interaction.guildId);
// update the defered reply if the unsubscribe function doesnt error
await interaction.editReply(
`You successfully disabled autothreading in this channel.`
);
} catch (error) {
// forwards errors to the user
await interaction.editReply(
`There was an error, here is the error message: ${error}`
);
}
break;
}
case "listchannels": {
// defers the reply so that the reply still works normally even if the backend is slower than 3 seconds
await interaction.deferReply({ ephemeral: true });
// the toplevel try/catch block that forwards every error to the user
try {
// calls the list function
const commandList = await list(interaction.guildId);
// update the defered reply with the return value of the list function if the unsubscribe function doesnt error
interaction.editReply(
`Here is the list of channels with autothreading enabled: ${commandList}`
);
} catch (error) {
// forwards errors to the user
await interaction.editReply(
`There was an error, here is the error message: ${error}`
);
}
break;
}
// not really needed because discord only listens for deployed commands
default:
break;
}
});
// Login to Discord with your client's token
client.login(process.env.TOKEN);