-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
123 lines (111 loc) · 3.72 KB
/
bot.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
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const dotenv = require("dotenv");
const mongoose = require("mongoose");
// Model created instead client.db ad client .collection could be used
const User = require("./model/user.model.js");
dotenv.config();
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log("Connected TO MONGODB");
startBot();
})
.catch((err) => console.error("Error occurred:", err));
// Random Color proider function to the role being assigned
const colors = [5763719, 2123412, 2123412, 10038562, 16776960];
const pickColor = () => {
return colors[Math.floor(Math.random() * colors.length)];
};
// Initially create a role and assign
// As a follow-up, store roles in DB, fetch and assign them matching username
const roleCreation = async (guild, roles) => {
// Promise.all takes an array of promises and returns a single promise that resolves
//when all the promises in the array have resolved.
const newRoles = await Promise.all(
roles.map(async (item) => {
let newRole = guild.roles.cache.find((role) => role.name === item);
if (!newRole) {
newRole = await guild.roles.create({
name: item,
color: pickColor(),
permissions: [],
});
}
return newRole;
})
);
return newRoles;
};
// Preapproving if they are already present in the DB
const preApproveUser = async (user) => {
const res = await User.findOne({ username: user });
if (!res) return null;
const result = res.toObject();
return {
username: result.username,
roles: result.roles,
};
};
const startBot = async () => {
try {
const res = await User.find({});
// On starting up, the event is fired
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
});
// When a member enters the server, it is triggered immediately
client.on("guildMemberAdd", async (member) => {
const username = `${member.user.username}`;
const verified = await preApproveUser(username);
if (verified) {
const role = await roleCreation(member.guild, verified.roles);
role.forEach(async (sprint) => {
await member.roles.add(sprint);
});
} else {
try {
await member.kick("User not found in the database.");
console.log(`Kicked ${username} as they are not in the database.`);
} catch (error) {
console.error(`Failed to kick ${username}:`, error);
}
}
});
// client.on("messageCreate", async (msg) => {
// if (msg.author.bot) return;
// console.log(msg.guild.members);
// if (verifiedUsers.has(msg.author.id)) {
// msg.channel.send("You are already verified.");
// return;
// }
// const userEmail = data.find((entry) => entry.Email === msg.content);
// if (userEmail) {
// msg.channel.send("User is Verified ✔️😀");
// verifiedUsers.add(msg.author.id);
// } else {
// if (msg.content.includes("@")) {
// msg.channel.send("You are not verified in server😞");
// }
// try {
// await msg.guild.members.ban(msg.author.id, {
// reason: "Unverified email provided",
// });
// console.log(`Banned ${msg.author.tag} for unverified email`);
// } catch (error) {
// console.error(`Failed to ban ${msg.author.tag}:`, error);
// }
// }
// });
client.login(process.env.TOKEN);
} catch (err) {
console.error("Error occurred:", err);
}
};