Binato/server/objects/Channel.ts

105 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

2023-09-10 12:59:22 +01:00
import osu from "../../osuTyping";
import Bot from "../Bot";
import ChatHistory from "../ChatHistory";
import Shared from "../objects/Shared";
import DataStream from "./DataStream";
import User from "./User";
2022-11-19 01:06:03 +00:00
2023-09-10 12:59:22 +01:00
export default class Channel {
2022-11-19 01:06:03 +00:00
public name:string;
public description:string;
2022-11-19 15:06:03 +00:00
public stream:DataStream;
2023-08-20 13:03:01 +01:00
public isLocked:boolean = false;
2022-11-20 23:37:39 +00:00
private _isSpecial:boolean = false;
2022-11-19 01:06:03 +00:00
2023-08-20 13:03:01 +01:00
private readonly bot:Bot;
2022-11-23 00:48:28 +00:00
2023-08-20 13:03:01 +01:00
public constructor(shared:Shared, name:string, description:string, stream:DataStream, isSpecial:boolean = false) {
2022-11-19 01:06:03 +00:00
this.name = name;
this.description = description;
this.stream = stream;
2022-11-20 23:37:39 +00:00
this._isSpecial = isSpecial;
2022-11-23 00:48:28 +00:00
2023-08-20 13:03:01 +01:00
this.bot = shared.bot;
2022-11-20 23:37:39 +00:00
}
2023-08-20 13:03:01 +01:00
public get isSpecial() : boolean {
2022-11-20 23:37:39 +00:00
return this._isSpecial;
2022-11-19 01:06:03 +00:00
}
2023-08-20 13:03:01 +01:00
public get userCount() : number {
2022-11-19 15:06:03 +00:00
return this.stream.userCount;
}
public SendMessage(sender:User, message:string) {
2023-08-20 13:03:01 +01:00
if (!this.isLocked) {
const osuPacketWriter = osu.Bancho.Writer();
osuPacketWriter.SendMessage({
sendingClient: sender.username,
message: message,
target: this.name,
senderId: sender.id
});
this.stream.SendWithExclusion(osuPacketWriter.toBuffer, sender);
2023-09-10 12:59:22 +01:00
if (this.name === "#osu") {
ChatHistory.AddMessage(`${sender.username}: ${message}`);
}
2022-11-19 15:06:03 +00:00
}
2023-08-20 13:03:01 +01:00
if (message[0] === "!") {
this.bot.OnMessage(this, sender, message);
} else if (this.isLocked) {
return this.SendSystemMessage("This channel is currently locked", sender);
2022-11-19 15:06:03 +00:00
}
}
2022-11-23 00:48:28 +00:00
public SendBotMessage(message:string, sendTo?:User) {
2023-08-20 13:03:01 +01:00
const osuPacketWriter = osu.Bancho.Writer();
2022-11-23 00:48:28 +00:00
osuPacketWriter.SendMessage({
2023-08-20 13:03:01 +01:00
sendingClient: this.bot.user.username,
2022-11-23 00:48:28 +00:00
message: message,
target: this.name,
2023-08-20 13:03:01 +01:00
senderId: this.bot.user.id
2022-11-23 00:48:28 +00:00
});
if (sendTo instanceof User) {
sendTo.addActionToQueue(osuPacketWriter.toBuffer);
} else {
this.stream.Send(osuPacketWriter.toBuffer);
}
2023-09-10 12:59:22 +01:00
if (this.name === "#osu") {
ChatHistory.AddMessage(`${this.bot.user.username}: ${message}`);
}
2022-11-23 00:48:28 +00:00
}
2022-11-19 15:06:03 +00:00
public SendSystemMessage(message:string, sendTo?:User) {
2023-08-20 13:03:01 +01:00
const osuPacketWriter = osu.Bancho.Writer();
2022-11-19 15:06:03 +00:00
osuPacketWriter.SendMessage({
sendingClient: "System",
message: message,
target: this.name,
senderId: 1
});
if (sendTo instanceof User) {
sendTo.addActionToQueue(osuPacketWriter.toBuffer);
} else {
this.stream.Send(osuPacketWriter.toBuffer);
}
}
public Join(user:User) {
this.stream.AddUser(user);
2023-08-20 13:03:01 +01:00
const osuPacketWriter = osu.Bancho.Writer();
2022-11-19 15:06:03 +00:00
osuPacketWriter.ChannelJoinSuccess(this.name);
user.addActionToQueue(osuPacketWriter.toBuffer);
}
2022-11-19 01:06:03 +00:00
2022-11-19 15:06:03 +00:00
public Leave(user:User) {
this.stream.RemoveUser(user);
2023-08-20 13:03:01 +01:00
const osuPacketWriter = osu.Bancho.Writer();
osuPacketWriter.ChannelRevoked(this.name);
user.addActionToQueue(osuPacketWriter.toBuffer);
2022-11-19 01:06:03 +00:00
}
}