-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (91 loc) · 3.98 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
const fs = require('fs');
const {Client, Collection, Intents} = require('discord.js');
const client = new Client({intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES]});
const utils = require('./utils');
const config = require('./config.json');
if (utils.validateConfigurationSchema(config)) {
console.log('Configuration schema validated.');
} else {
console.log('Invalid configuration schema.');
process.exit(9);
}
const TOKEN = config['bot']['token'];
const PHOTOGRAPHY_CHANNEL = config['guild']['photography'];
client.once('ready', () => {
console.log(`${client.user.username} is ready!\n`);
console.log('Invite your bot to your server with this link: ');
console.log(`https://discord.com/api/oauth2/authorize?client_id=${client.application.id}&permissions=8&scope=bot%20applications.commands\n`);
});
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
/**
* Handle interactions (slash commands)
*/
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) {
return;
}
const command = client.commands.get(interaction.commandName);
if (!command) {
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({content: 'An unexpected error has occurred!', ephemeral: true});
}
});
/**
* Upon a new user joining the server, assign them a role based upon whether they are a bot or human.
*/
client.on('guildMemberAdd', async member => {
try {
if (member.user.bot) {
console.log('A bot has joined the server.');
await member.roles.add(await member.guild.roles.cache.find(role => role.name === "Bots"), `Auto-assigned with ${client.user.username} upon join`);
} else {
console.log('A human has joined the server.');
await member.roles.add(await member.guild.roles.cache.find(role => role.name === "Humans"), `Auto-assigned with ${client.user.username} upon join`);
}
} catch (error) {
console.error(error);
}
});
/**
* Upon a new message created, triage and respond to it if applicable.
*/
client.on('messageCreate', message => {
try {
if (PHOTOGRAPHY_CHANNEL !== undefined && message.channel.name === PHOTOGRAPHY_CHANNEL) { // Photography is enabled and a message was sent to the channel
if (message.attachments.size > 0) {
let messageContent = message.content;
let firstAttachment = message.attachments.first(); // TODO: Properly iterate for all attachments
if (utils.isImageAttachment(firstAttachment)) {
message.startThread({
name: messageContent.length > 0 ? messageContent : message.author.username + '\'s Photo',
reason: `Auto-generated by ${client.user.username} upon attachment upload`
}).then(thread => {
let camera;
utils.saveImage(firstAttachment).then(imagePath => {
// TODO: Extract camera metadata
camera = utils.getCameraFromImage(imagePath);
utils.deleteFile(imagePath); // Clean the temp folder
}).catch(() => {
camera = 'Unknown';
}).finally(async () => {
await thread.send(utils.generatePhotographyEmbed(firstAttachment.name, firstAttachment.url, camera, firstAttachment.width, firstAttachment.height));
});
});
}
}
}
} catch (error) {
console.error(error);
}
});
client.login(TOKEN).then(() => console.log('Login successful!'));