-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (61 loc) · 1.95 KB
/
index.ts
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
import dotenv from "dotenv";
import { Client, GatewayIntentBits, Partials } from "discord.js";
import { DISCORD_TOKEN, startingChannel, guildID } from "./config";
import { wrongId } from "./config/message";
import { serverResponse } from "./helper/serverResponse";
import { newMembers } from "./helper/newMember";
import {db} from "./helper/firebase";
// load env file
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
partials: [Partials.Channel],
});
client.login(DISCORD_TOKEN).then();
client.on("ready", () => {
console.log(`Logged in as ${client.user?.tag}`);
});
// user verification based on nocodb id
client.on("messageCreate", async (message) => {
if (message.channel.id === startingChannel && !message.author.bot) {
// clearing user input
message.channel.messages
.fetch({ limit: 1 })
.then((messages) => {
const botMsg = messages.last();
setTimeout(() => {
botMsg.delete();
}, 10000);
})
.catch(console.error);
const guild = client.guilds.cache.get(guildID);
if (!guild) throw new Error("Could not find guild check your env");
await newMembers(guild, message, client);
} else if (
!message.author.bot &&
message.channel.id === `744627218743033887` &&
message.author.id !== "735045662672027718"
) {
await serverResponse(message, wrongId());
}
});
// when user leaves the server this event is triggered
// (for future usecase)
client.on("guildMemberRemove", async (member) => {
try {
const docRef = db.collection("users").where("discordUserId", "==", member.id);
const doc = (await docRef.get()).docs[0];
await doc.ref.update({
discordActive: false,
});
} catch {
console.log("NO USER FOUND IN DB WHEN LEAVING THE SERVER");
}
// You can also perform other actions here, such as sending a farewell message or updating a database
});