-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (152 loc) · 5.7 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
require('dotenv').config();
const { Client, Events, IntentsBitField, Collection, Partials, ActivityType, REST, Routes } = require('discord.js');
const fs = require('fs');
const database = require('./database');
const filter = require('./util/filter');
const transfers = require('./util/transfers');
console.log('Working with', Object.keys(database).join(', '));
console.log(filter.clean('The filter is working.'));
const client = new Client({
intents: new IntentsBitField(3276799),
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
// settings the modals
client.modals = new Collection();
fs.readdirSync(`${__dirname}/modals`)
.map((i) => {
const m = require(`./modals/${i}`);
client.modals.set(m.id, m.execute);
});
// settings the commands
client.commands = new Collection();
const commands = fs.readdirSync(`${__dirname}/commands`)
.map((i) => {
const c = require(`./commands/${i}`);
if (c.disabled) return;
client.commands.set(c.command.name, c.execute);
return c.command.toJSON();
}).filter(Boolean);
// creating the events
const events = fs.readdirSync(`${__dirname}/events`).map((i) => require(`./events/${i}`));
// const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN);
function copyMessage(message) {
const files = [];
for (let file of message.attachments) {
files.push(file[1]);
}
return {
content: message.content,
components: message.components,
files: files,
embeds: message.embeds
};
}
client.once(Events.ClientReady, (bot) => {
console.log(`Ready! Logged in as ${bot.user.tag}`);
client.user.setActivity({
type: ActivityType.Watching,
name: 'you'
});
// for messages
bot.on(Events.MessageCreate, async (message) => {
// if the author is a bot, or is a reply to an interaction we don't transfer the message
if (message.author.bot || message.interaction) {
return;
}
try {
// if it's a dm
if (message.channel.isDMBased()) {
transfers.createJob({
type: 'from-dms',
user: {
username: message.author.username,
discriminator: message.author.discriminator,
id: message.author.id
},
message: copyMessage(message)
}, client);
}
// if it's in a response thread, we send the message to the user
if (message.channel.isThread() && message.channel.ownerId === bot.user.id) {
const userId = message.channel.name.split(' ').at(-1);
if (userId === bot.user.id) {
return;
}
if (await database.Dms.get(userId)) {
transfers.createJob({
type: 'to-dms',
message: copyMessage(message),
user: {
id: userId,
}
}, client);
}
}
} catch (err) {
console.log('Unable to transfer message.\nReason:', err);
}
});
bot.on(Events.ThreadUpdate, async (oldThread, newThread) => {
const user = oldThread.name.split(' ').at(-1);
if (oldThread?.ownerId !== bot.user.id || !user) {
return;
}
if (newThread.locked && !oldThread.locked) {
await database.Dms.set(user, {
threadId: null
});
}
if (!newThread.locked && oldThread.locked) {
await database.Dms.set(user, {
threadId: newThread.id
});
}
});
// for slash commands
bot.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand() && !interaction.isContextMenuCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return interaction.reply('I can\'t find this command.');
try {
await command(client, interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
// for modals
bot.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isModalSubmit()) return;
const modal = client.modals.get(interaction.customId);
if (!modal) return;
try {
await modal(client, interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
events.forEach((i) => {
bot.on(i.event, i.callback.bind(null, client));
});
client.guilds.cache.forEach((guild) => {
guild.commands.set(commands);
});
// and deploy your commands!
/*
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(client.user.id),
{ body: [] || commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();*/
});
client.login(process.env.BOT_TOKEN);