-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoice.ts
158 lines (153 loc) · 4.55 KB
/
voice.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
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
joinVoiceChannel,
getVoiceConnection,
AudioPlayer,
AudioPlayerState,
} from "@discordjs/voice";
import {
Collection,
GuildChannel,
GuildTextBasedChannel,
escapeInlineCode,
} from "discord.js";
import { Video, dl } from "./yt";
import { error, info } from "./logger";
function shuffle<T>(arr: T[]) {
arr = arr.slice();
let i = arr.length;
let j = 0;
let temp: T;
if (i === 0) {
return arr;
}
while (--i) {
j = Math.floor(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
export class Player {
private playing = false;
public looping = false;
public queuelooping = false;
constructor(
public player: AudioPlayer,
public queue: Video[] = [],
public channel: GuildTextBasedChannel,
) {}
async push(options: Video, playlist: boolean = false) {
this.queue.push(options);
if (!this.playing) {
this.playing = true;
await this.channel.send(
`[${options.title}](<https://youtube.com/watch?v=${options.id}>)を再生します。`,
);
this.play();
} else {
if (playlist) return;
await this.channel.send(
`[${options.title}](<https://youtube.com/watch?v=${options.id}>)をキューに追加しました。(${this.queue.length}曲目)`,
);
}
}
async skip() {
this.player.stop();
await this.channel.send("スキップしました。");
}
async pause() {
this.player.pause();
await this.channel.send("一時停止しました。");
}
async resume() {
this.player.unpause();
await this.channel.send("再開しました。");
}
async clear() {
this.queue = [];
this.player.stop();
await this.channel.send("停止しました。");
}
async shuffle() {
this.queue = shuffle(this.queue);
await this.channel.send("シャッフルしました。");
}
async play() {
if (!this.queue.length) {
this.playing = false;
return;
}
const options =
this.looping && !this.queuelooping
? this.queue[0]
: this.queue.shift()!;
try {
await this.channel.send(
`${escapeInlineCode(options.title)}を再生します。(${
this.looping ?? this.queuelooping
? "ループ中"
: `${this.queue.length}曲残っています`
})`,
);
const stream = await dl(options.id);
this.player.play(stream);
const onError = (e: any) => {
error(e);
this.player.off("stateChange", listener);
this.player.off("error", onError);
this.play();
};
const listener = (
state: AudioPlayerState,
newState: AudioPlayerState,
) => {
if (state.status === "playing" && newState.status === "idle") {
info(`Finished playing ${options.title}.`);
this.player.off("stateChange", listener);
this.player.off("error", onError);
if (this.queuelooping) {
this.queue.push(options);
}
this.play();
}
};
this.player.on("stateChange", listener);
this.player.on("error", onError);
} catch (e) {
console.error(e);
this.play();
}
}
}
const players = new Collection<string, Player>();
export async function connect(
channel: GuildChannel,
log: GuildTextBasedChannel,
) {
const conn = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: true,
});
const player = new AudioPlayer();
conn.subscribe(player);
players.set(channel.guild.id, new Player(player, [], log));
return conn;
}
export function get(guildId: string) {
const conn = getVoiceConnection(guildId);
if (!conn) {
if (players.has(guildId)) players.delete(guildId);
return {};
}
return {
player: players.get(guildId),
conn,
};
}
export function disconnect(guildId: string) {
const conn = getVoiceConnection(guildId);
if (conn) conn.destroy();
}