-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
145 lines (111 loc) · 3.88 KB
/
test.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as discord from "https://deno.land/x/[email protected]/mod.ts";
import * as voice from "./mod.ts";
import ytsr from "https://deno.land/x/[email protected]/mod.ts";
import { getInfo } from "https://deno.land/x/[email protected]/mod.ts";
import { TOKEN } from "./config.ts";
const client = new discord.CommandClient({
prefix: ".",
token: TOKEN,
intents: ["GUILDS", "GUILD_VOICE_STATES", "GUILD_MESSAGES"],
});
const conns = new discord.Collection<string, voice.VoiceConnection>();
client.commands.add(
class extends discord.Command {
name = "join";
async execute(ctx: discord.CommandContext) {
if (!ctx.guild) return;
if (conns.has(client.user!.id)) {
return ctx.message.reply("I've already joined VC.");
}
const vs = await ctx.guild.voiceStates.get(ctx.author.id);
if (!vs || !vs.channel) {
return ctx.message.reply("You're not in a Voice Channel.");
}
const data = await vs.channel.join({ deaf: true });
const conn = new voice.VoiceConnection(client.user!.id, {
mode: "xsalsa20_poly1305",
receive: "opus",
});
conn.voiceStateUpdate({
guildID: data.guild.id,
channelID: vs.channel.id,
sessionID: data.sessionID,
});
conn.voiceServerUpdate({ endpoint: data.endpoint, token: data.token });
conn.connect();
conns.set(ctx.guild.id, conn);
ctx.message.reply("Joined Voice Channel!");
}
},
);
client.commands.add(
class extends discord.Command {
name = "play";
async execute(ctx: discord.CommandContext) {
if (!ctx.guild) return;
if (!conns.has(ctx.guild.id)) {
return ctx.message.reply(
"I have not even joined a Voice Channel here.",
);
}
const conn = conns.get(ctx.guild.id)!;
if (!conn.ready) return ctx.message.reply("Connection not ready.");
if (!ctx.argString.length) {
return ctx.message.reply("Give some query for search!");
}
const search = await ytsr.searchOne(ctx.argString);
if (!search || !search.id) return ctx.message.reply("Nothing found.");
const info = await getInfo(search.id);
const url = info.formats.find((e) => e.hasAudio && !e.hasVideo)!.url;
const player = conn.player();
const stream = new voice.PCMStream(url);
stream.pipeTo(player.writable);
ctx.message.reply("Playing now - " + search.title + "!");
}
},
);
client.commands.add(
class extends discord.Command {
name = "receive";
async execute(ctx: discord.CommandContext) {
if (!ctx.guild) return;
if (!conns.has(ctx.guild.id)) {
return ctx.message.reply(
"I have not even joined a Voice Channel here.",
);
}
const conn = conns.get(ctx.guild.id)!;
const user = ctx.message.mentions.users.first();
if (!user) {
return ctx.message.reply("Mention someone to receive audio for.");
}
ctx.message.reply("Receiving now.");
for await (const frame of conn.readable(user.id)) {
console.log(frame);
}
}
},
);
client.commands.add(
class extends discord.Command {
name = "leave";
async execute(ctx: discord.CommandContext) {
if (!ctx.guild) return;
if (!conns.has(ctx.guild.id)) {
return ctx.message.reply(
"I have not even joined a Voice Channel here.",
);
}
const conn = conns.get(ctx.guild.id);
conn?.close();
conns.delete(ctx.guild.id);
const vs = await ctx.guild.voiceStates.get(client.user!.id);
if (vs) {
await vs.channel?.leave();
}
ctx.message.reply("Left voice channel.");
}
},
);
client.on("commandError", console.error);
client.connect().then(() => console.log("Connected!"));