-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
99 lines (88 loc) · 2.71 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
const Discord = require("discord.js");
require("dotenv").config();
const { MongoClient } = require("mongodb");
const { isRegExp } = require("util");
const client = new Discord.Client();
const database = new MongoClient(process.env.DB_URI, {
useUnifiedTopology: true,
});
async function addJoinSound(document) {
try {
await database.connect();
const db = database.db("discord-bot-database");
// Use the collection "people"
const col = db.collection("join-sounds");
if ((await col.findOne({ user: document.user })) == null) {
await col.insertOne(document);
return "added";
} else {
await col.updateOne(
{ user: document.user },
{ $set: { url: document.url } }
);
return "updated";
}
// await console.log("Connected correctly to server");
} catch (err) {
console.log(err.stack);
}
}
async function findJoinSound(id) {
try {
await database.connect();
const db = database.db("discord-bot-database");
// Use the collection "people"
const col = db.collection("join-sounds");
const p = await col.findOne({ user: id });
return p;
// await console.log("Connected correctly to server");
} catch (err) {
console.log(err.stack);
}
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", async (msg) => {
if (msg.channel.name == "join-sound-upload") {
153946;
if (msg.attachments.array()[0].size < 166667) {
let joinSoundDocument = {
user: msg.author.id,
url: msg.attachments.array()[0].url,
};
const message = await addJoinSound(joinSoundDocument);
// console.log(msg.author.id);
// console.log(msg.attachments.array()[0].url);
const reply = await msg.reply(`Succussful ${message} join sound`);
reply.delete({ timeout: 5000 });
} else {
const reply = await msg.reply(`Join sound too long`);
reply.delete({ timeout: 5000 });
}
msg.delete({ timeout: 5000 });
}
// if (msg.content === "ping") {
// msg.reply("pong");
});
client.on("voiceStateUpdate", async (oldState, newState) => {
let channel = newState.channel;
if (oldState.channel == null && newState.channel != null) {
try {
const sound = await findJoinSound(newState.id);
const temp = sound.url.slice(sound.url.length - 3, sound.url.length);
console.log(temp);
if (temp == "mp3") {
const connection = await newState.channel.join();
const dispatcher = connection.play(sound.url);
console.log("hey");
dispatcher.on("finish", () => {
channel.leave();
});
}
} catch (err) {
console.log(err.stack);
}
}
});
client.login(process.env.TOKEN);