generated from HexaNona/kasumi-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.ts
114 lines (105 loc) · 3.86 KB
/
controller.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
import { client } from "init/client";
import Kasumi, { Card, MessageType } from "kasumi.js";
import { Streamer } from "../type";
import { LocalStreamer } from "./player";
import { Controller } from "../type";
export class LocalController extends Controller {
private userStreamers: Map<string, Streamer[]> = new Map();
private guildStreamers: Map<string, Streamer[]> = new Map();
private streamerChannel: Map<string, string> = new Map();
private channelStreamer: Map<string, Streamer> = new Map();
private allActiveStreamers: Set<Streamer> = new Set();
constructor(client: Kasumi<any>) {
super(client);
}
get activeStreamersArray() {
return [...this.allActiveStreamers];
}
async returnStreamer(streamer: Streamer) {
let sessions =
(await this.client.config.getOne("arisa::session.ongoing")) || [];
sessions = sessions.filter((v) => {
return !(
v.targetGuildId == streamer.TARGET_GUILD_ID &&
v.targetChannelId == streamer.TARGET_CHANNEL_ID &&
v.invitationAuthorId == streamer.INVITATION_AUTHOR_ID
);
});
this.client.config.set("arisa::session.ongoing", sessions);
this.channelStreamer.delete(streamer.TARGET_CHANNEL_ID);
this.allActiveStreamers.delete(streamer);
{
const streamers = (
this.userStreamers.get(streamer.INVITATION_AUTHOR_ID) || []
).filter((v) => v != streamer);
this.userStreamers.set(streamer.INVITATION_AUTHOR_ID, streamers);
}
{
const streamers = (
this.guildStreamers.get(streamer.TARGET_GUILD_ID) || []
).filter((v) => v != streamer);
this.guildStreamers.set(streamer.TARGET_GUILD_ID, streamers);
}
return true;
}
/**
* Assign a streamer bot to a channel.
*
* Bot token is auto assigned.
* @param channelId ChannelID
*/
async joinChannel(
guildId: string,
channelId: string,
authorId: string,
textChannelId?: string
) {
await this.client.API.voice.leave(channelId);
const streamer: Streamer = new LocalStreamer(
guildId,
channelId,
authorId,
this
);
this.allActiveStreamers.add(streamer);
this.channelStreamer.set(channelId, streamer);
{
const streamers = this.userStreamers.get(authorId) || [];
streamers.push(streamer);
this.userStreamers.set(authorId, streamers);
}
{
const streamers = this.guildStreamers.get(guildId) || [];
streamers.push(streamer);
this.guildStreamers.set(guildId, streamers);
}
const sessions =
(await this.client.config.getOne("arisa::session.ongoing")) || [];
sessions.push({
targetChannelId: channelId,
targetGuildId: guildId,
invitationAuthorId: authorId,
invitationTextChannelId: textChannelId,
});
this.client.config.set("arisa::session.ongoing", sessions);
if (await streamer.connect()) return streamer;
}
async abortStream(channelId: string) {
const streamer = this.getChannelStreamer(channelId);
if (!streamer) return false;
await streamer.disconnect(null);
return true;
}
getGuildStreamers(guildId: string): Streamer[] | undefined {
return this.guildStreamers.get(guildId);
}
getChannelStreamer(channelId: string): Streamer | undefined {
return this.channelStreamer.get(channelId);
}
getUserStreamers(userId: string): Streamer[] | undefined {
return this.userStreamers.get(userId);
}
getStreamerChannel(token: string): string | undefined {
return this.streamerChannel.get(token);
}
}