-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroleAssignment.js
73 lines (58 loc) · 2.08 KB
/
roleAssignment.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
const { Client, Interaction, PermissionFlagsBits } = require('discord.js');
const Level = require('../../models/Level');
const saveErrorToDatabase = require('../../utils/saveErrorToDatabase');
module.exports = {
/**
*
* @param {Client} client
* @param {Interaction} interaction
*/
name: 'roleassignment',
description: "Will assign roles to members",
devOnly: false,
testOnly: false,
// options: [{}],
deleted: false,
botPermissions: [PermissionFlagsBits.ManageRoles],
callback: async (client, interaction) => {
try {
// start interaction
await interaction.deferReply({ ephemeral: true });
// got the role which participant selected
const role = interaction.guild.roles.cache.get(interaction.customId);
// check if role exist or not
if (!role) {
interaction.editReply({
content: "I couldn't find that role",
});
return;
}
// will check if user is registered participant or not
const query = {
username: interaction.user.username,
role: "member",
}
const level = await Level.findOne(query);
// console.log(level);
if(level){
await interaction.member.roles.add(role);
level.role = role.name; // changing the role in database
// saving the changes in database
await level.save().catch((e) => {
console.log(`Error saving updated level ${e}`);
return;
});
interaction.editReply({
content: `You got the role of ${role.name} succesfully`,
})
return;
}
interaction.editReply({
content: `You are not eligible for the role of ${role.name}`,
})
return;
} catch (error) {
saveErrorToDatabase(error);
}
},
};