-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjoin.js
More file actions
110 lines (107 loc) · 4.07 KB
/
join.js
File metadata and controls
110 lines (107 loc) · 4.07 KB
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
const { CommandBuilder } = require("../Commands.js");
const RevoltPlayer = require("../Player.js");
/* */
function joinChannel(message, cid, cb=()=>{}, ecb=()=>{}) {
if (!this.client.channels.has(cid)) {
ecb();
return message.replyEmbed("Couldn't find the channel `" + cid + "`\nUse the help command to learn more about this. (`%help join`)")
}
if (this.playerMap.has(cid)) {
cb(this.playerMap.get(cid));
return message.replyEmbed("Already joined <#" + cid + ">.");
}
this.channels.push(cid);
const settings = this.getSettings(message);
const pOff = this.freed.shift() || ++this.currPort; // reuse old ports
const p = new RevoltPlayer(this.config.token, {
voice: this.revoice,
portOffset: pOff,
client: this.client,
spotify: this.spotifyConfig,
uploader: this.uploader,
settings: settings,
spotifyClient: this.spotify,
geniusClient: this.geniusClient,
messageChannel: message.channel,
ytdlp: this.ytdlp,
innertube: this.innertube
});
p.on("autoleave", async () => {
message.channel.sendEmbed("Left channel <#" + cid + "> because of inactivity.");
const port = p.port - 3050;
this.playerMap.delete(cid);
p.destroy();
this.freed.push(port);
});
p.on("leave", () => {
p.connection.users.forEach(user => {
if (!this.observedVoiceUsers.has(user.id)) return;
const cbs = this.observedVoiceUsers.get(user.id);
p.emit("userupdate", user, "leave");
cbs.forEach(c => c.cb.call(this, "left", p));
});
});
p.on("message", (m) => {
if ((this.getSettings(message).get("songAnnouncements")) == "false") return;
message.channel.sendEmbed(m);
});
p.on("roomfetched", () => {
p.connection.users.forEach(user => {
if (!this.observedVoiceUsers.has(user.id)) return;
const cbs = this.observedVoiceUsers.get(user.id); // .id because this is a revoice user object
p.emit("userupdate", user, "join");
cbs.forEach(c => c.cb.call(this, "joined", p));
});
});
this.playerMap.set(cid, p);
message.replyEmbed("Joining Channel...").then((message) => {
p.join(cid).then(() => {
message.editEmbed(`✅ Successfully joined <#${cid}>`);
cb(p);
p.connection.on("userjoin", (user) => {
if (!this.observedVoiceUsers.has(user.id)) return;
const cbs = this.observedVoiceUsers.get(user.id); // .id because this is a revoice user object
p.emit("userupdate", user, "join");
cbs.forEach(c => c.cb.call(this, "joined", p));
});
p.connection.on("userleave", (user) => {
if (!this.observedVoiceUsers.has(user.id)) return;
const cbs = this.observedVoiceUsers.get(user.id);
p.emit("userupdate", user, "leave");
cbs.forEach(c => c.cb.call(this, "left"));
});
});
});
}
module.exports = {
command: new CommandBuilder()
.setName("join")
.setDescription("Make the bot join a specific voice channel.", "commands.join")
.setId("join")
.addChannelOption((option) =>
option.setName("Channel ID")
.setType("voiceChannel")
.setId("cid")
.setDescription("Specify the channel the bot should join. It will try to find it automatically, if not provided. If it doesn't find you though, it will fail and you will have to provide the channel. This is necessary due to (current) Stoat limitations.", "options.join.channel-deprecated")
.setDynamicDefault((_client, message) => {
if (!message) return null;
const user = message.authorId;
var id = null;
message.channel.server.channels.forEach((c) => {
if (!c.isVoice) return;
if (!c.voiceParticipants.has(user)) return;
id = c.id;
});
return id;
})
.setRequired(true)
),
run: function(message, data) {
const cid = data.getById("cid").value || this.checkVoiceChannels(message);
this.players.initPlayer(message, cid);
},
export: {
name: "joinChannel",
object: joinChannel
}
}