This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
/
whisper.ts
54 lines (49 loc) · 1.99 KB
/
whisper.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
import { awaitResponse } from "../await/await-response";
import { SingleConnection } from "../client/connection";
import { MessageError } from "../client/errors";
import { NoticeMessage } from "../message/twitch-types/notice";
import { validateChannelName } from "../validation/channel";
import { sendPrivmsg } from "./privmsg";
export class WhisperError extends MessageError {
public targetUsername: string;
public failedMessage: string;
public constructor(
targetUsername: string,
failedMessage: string,
message?: string,
cause?: Error
) {
super(message, cause);
this.targetUsername = targetUsername;
this.failedMessage = failedMessage;
}
}
const badNoticeIDs = [
"whisper_banned", // You have been banned from sending whispers.
"whisper_banned_recipient", // That user has been banned from receiving whispers.
"whisper_invalid_args", // Usage: <login> <message>
"whisper_invalid_login", // No user matching that login.
"whisper_invalid_self", // You cannot whisper to yourself.
"whisper_limit_per_min", // You are sending whispers too fast. Try again in a minute.
"whisper_limit_per_sec", // You are sending whispers too fast. Try again in a second.
"whisper_restricted", // Your settings prevent you from sending this whisper.
"whisper_restricted_recipient", // That user's settings prevent them from receiving this whisper.
];
export async function whisper(
conn: SingleConnection,
username: string,
message: string
): Promise<void> {
validateChannelName(username);
sendPrivmsg(conn, conn.configuration.username, `/w ${username} ${message}`);
return awaitResponse(conn, {
failure: (msg) =>
msg instanceof NoticeMessage &&
msg.channelName === conn.configuration.username &&
badNoticeIDs.includes(msg.messageID!),
noResponseAction: "success",
timeout: 1000,
errorType: (msg, cause) => new WhisperError(username, message, msg, cause),
errorMessage: `Failed to whisper [${username}]: ${message}`,
}) as Promise<void>;
}