Skip to content

Commit 0f9d0e2

Browse files
committed
feat: add chat api
1 parent c4c7794 commit 0f9d0e2

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

example/send-chat.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { env } from 'bun'
2+
import { Kient } from 'kient'
3+
4+
const kient = new Kient()
5+
kient.setAuthToken(env.KICK_TOKEN as string)
6+
7+
const bot = await kient.api.chat.send({
8+
type: 'bot',
9+
message: 'Message will be send to authenticated user channel',
10+
})
11+
console.log(bot.raw)
12+
13+
const user = await kient.api.chat.send({
14+
type: 'user',
15+
message: 'Message will be send to specified user id',
16+
userId: 0,
17+
})
18+
console.log(user.raw)

src/api/chat/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { APIBase } from '../api-base'
2+
import { sendChat, type SendChatParams } from './send-chat'
3+
4+
/**
5+
* Description placeholder
6+
*
7+
* @group APIs
8+
*/
9+
export class ChatAPI extends APIBase {
10+
/**
11+
* Send a chat to a channel and returns the status of the message
12+
*
13+
* @param query Accepts and object of the message and how if it's being sent from a bot account
14+
*/
15+
send(params: SendChatParams) {
16+
return sendChat(this.kient, params)
17+
}
18+
}

src/api/chat/send-chat.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Kient } from 'kient'
2+
import type { APIResponse } from '../../util/api-response'
3+
import { Chat, type ChatData } from '../../structures/chat'
4+
5+
interface SendBotChatParams {
6+
message: string
7+
type: 'bot'
8+
}
9+
10+
interface SendUserChatParams {
11+
message: string
12+
type: 'user'
13+
/**
14+
* The target channel's user ID to send the message to
15+
*/
16+
userId: number
17+
}
18+
19+
export type SendChatParams = SendUserChatParams | SendBotChatParams
20+
21+
export async function sendChat(kient: Kient, params: SendChatParams) {
22+
const requestParams: {
23+
content: string
24+
type: 'user' | 'bot'
25+
broadcaster_user_id?: number
26+
} = {
27+
content: params.message,
28+
type: params.type,
29+
}
30+
31+
if (params.type === 'user') {
32+
requestParams.broadcaster_user_id = params.userId
33+
}
34+
35+
const response = await kient._apiClient.fetch<APIResponse<ChatData>>('/chat', {
36+
method: 'POST',
37+
body: JSON.stringify(requestParams),
38+
})
39+
40+
const chat = new Chat(kient, response.data)
41+
return chat
42+
}

src/kient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { WSClient, type WSClientOptions } from './ws.client'
66
import { CategoryAPI } from './api/category'
77
import { UserAPI } from './api/user'
88
import { ChannelAPI } from './api/channel'
9+
import { ChatAPI } from './api/chat'
910

1011
type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T
1112

@@ -58,5 +59,6 @@ export class Kient extends EventEmitter<KientEventEmitters> {
5859
category: new CategoryAPI(this),
5960
user: new UserAPI(this),
6061
channel: new ChannelAPI(this),
62+
chat: new ChatAPI(this),
6163
}
6264
}

src/structures/chat.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Kient } from '../kient'
2+
import { Base } from './base'
3+
4+
export interface ChatData {
5+
is_sent: boolean
6+
message_id: string
7+
}
8+
9+
/**
10+
* Data structure of a chat message
11+
*
12+
* @group API Structures
13+
*/
14+
export class Chat extends Base<ChatData> {
15+
/**
16+
* The id of the sent message
17+
*/
18+
id: string
19+
20+
/**
21+
* A boolean indicating if the message has been sent
22+
*/
23+
isSent: boolean
24+
25+
/** @internal */
26+
constructor(kient: Kient, data: ChatData) {
27+
super(kient, data)
28+
29+
this.id = data.message_id
30+
this.isSent = data.is_sent
31+
}
32+
}

0 commit comments

Comments
 (0)