Skip to content

Commit

Permalink
feat: add chat api
Browse files Browse the repository at this point in the history
  • Loading branch information
zSoulweaver committed Feb 21, 2025
1 parent c4c7794 commit 0f9d0e2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
18 changes: 18 additions & 0 deletions example/send-chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { env } from 'bun'
import { Kient } from 'kient'

const kient = new Kient()
kient.setAuthToken(env.KICK_TOKEN as string)

const bot = await kient.api.chat.send({
type: 'bot',
message: 'Message will be send to authenticated user channel',
})
console.log(bot.raw)

const user = await kient.api.chat.send({
type: 'user',
message: 'Message will be send to specified user id',
userId: 0,
})
console.log(user.raw)
18 changes: 18 additions & 0 deletions src/api/chat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { APIBase } from '../api-base'
import { sendChat, type SendChatParams } from './send-chat'

/**
* Description placeholder
*
* @group APIs
*/
export class ChatAPI extends APIBase {
/**
* Send a chat to a channel and returns the status of the message
*
* @param query Accepts and object of the message and how if it's being sent from a bot account
*/
send(params: SendChatParams) {
return sendChat(this.kient, params)
}
}
42 changes: 42 additions & 0 deletions src/api/chat/send-chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Kient } from 'kient'
import type { APIResponse } from '../../util/api-response'
import { Chat, type ChatData } from '../../structures/chat'

interface SendBotChatParams {
message: string
type: 'bot'
}

interface SendUserChatParams {
message: string
type: 'user'
/**
* The target channel's user ID to send the message to
*/
userId: number
}

export type SendChatParams = SendUserChatParams | SendBotChatParams

export async function sendChat(kient: Kient, params: SendChatParams) {
const requestParams: {
content: string
type: 'user' | 'bot'
broadcaster_user_id?: number
} = {
content: params.message,
type: params.type,
}

if (params.type === 'user') {
requestParams.broadcaster_user_id = params.userId
}

const response = await kient._apiClient.fetch<APIResponse<ChatData>>('/chat', {
method: 'POST',
body: JSON.stringify(requestParams),
})

const chat = new Chat(kient, response.data)
return chat
}
2 changes: 2 additions & 0 deletions src/kient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WSClient, type WSClientOptions } from './ws.client'
import { CategoryAPI } from './api/category'
import { UserAPI } from './api/user'
import { ChannelAPI } from './api/channel'
import { ChatAPI } from './api/chat'

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

Expand Down Expand Up @@ -58,5 +59,6 @@ export class Kient extends EventEmitter<KientEventEmitters> {
category: new CategoryAPI(this),
user: new UserAPI(this),
channel: new ChannelAPI(this),
chat: new ChatAPI(this),
}
}
32 changes: 32 additions & 0 deletions src/structures/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Kient } from '../kient'
import { Base } from './base'

export interface ChatData {
is_sent: boolean
message_id: string
}

/**
* Data structure of a chat message
*
* @group API Structures
*/
export class Chat extends Base<ChatData> {
/**
* The id of the sent message
*/
id: string

/**
* A boolean indicating if the message has been sent
*/
isSent: boolean

/** @internal */
constructor(kient: Kient, data: ChatData) {
super(kient, data)

this.id = data.message_id
this.isSent = data.is_sent
}
}

0 comments on commit 0f9d0e2

Please sign in to comment.