-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
76 lines (64 loc) · 2.09 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
require("dotenv/config");
const { Client, GatewayIntentBits, Events } = require("discord.js");
const {
joinVoiceChannel,
createAudioPlayer,
NoSubscriberBehavior,
createAudioResource,
} = require("@discordjs/voice");
const fs = require("fs");
const ms = require("ms");
const path = require("path");
const client = new Client({
intents: [Object.keys(GatewayIntentBits)],
});
client.on(Events.ClientReady, () => {
console.log("Bot is ready");
});
const prefix = "!";
client.on(Events.MessageCreate, async (message) => {
const cmd = message.content.split(" ")[0];
if (cmd === prefix + "join") {
//join room
const channel = message.guild.channels.cache.get(
message.member.voice.channelId
);
if (channel) {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const audioFiles = fs.readdirSync("./audios");
const randomFile =
audioFiles[Math.floor(Math.random() * audioFiles.length)];
const filePath = path.join("./audios", randomFile);
const resource = createAudioResource(filePath);
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause,
},
});
message.reply("I have successfully connected to the channel!");
player.play(resource);
connection.subscribe(player);
setInterval(async () => {
const audioFiles = fs.readdirSync("./audios");
const randomFile =
audioFiles[Math.floor(Math.random() * audioFiles.length)];
const filePath = path.join("./audios", randomFile);
const resource = createAudioResource(filePath);
await player.play(resource);
connection.subscribe(player);
}, ms("20s"));
player.on("error", (error) => {
console.error(
`Error: ${error.message} with resource ${error.resource.metadata.title}`
);
});
} else {
message.reply("You need to join a voice channel first!");
}
}
});
client.login(process.env.TOKEN);