-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Enumerable } from '@d-fischer/shared-utils'; | ||
import type { HelixGame } from '../Game/HelixGame'; | ||
import type { ApiClient } from '../../../ApiClient'; | ||
|
||
/** @private */ | ||
export interface HelixChannelData { | ||
broadcaster_id: string; | ||
broadcaster_name: string; | ||
broadcaster_language: string; | ||
game_id: string; | ||
game_name: string; | ||
title: string; | ||
} | ||
|
||
/** | ||
* A Twitch channel. | ||
*/ | ||
export class HelixChannel { | ||
/** @private */ | ||
@Enumerable(false) protected readonly _client: ApiClient; | ||
|
||
/** @private */ | ||
constructor(/** @private */ protected _data: HelixChannelData, client: ApiClient) { | ||
this._client = client; | ||
} | ||
|
||
/** | ||
* The ID of the channel. | ||
*/ | ||
get id(): string { | ||
return this._data.broadcaster_id; | ||
} | ||
|
||
/** | ||
* The display name of the channel. | ||
*/ | ||
get displayName(): string { | ||
return this._data.broadcaster_name; | ||
} | ||
|
||
/** | ||
* The language of the channel. | ||
*/ | ||
get language(): string { | ||
return this._data.broadcaster_language; | ||
} | ||
|
||
/** | ||
* The ID of the game currently played on the channel. | ||
*/ | ||
get gameId(): string { | ||
return this._data.game_id; | ||
} | ||
|
||
/** | ||
* The name of the game currently played on the channel. | ||
*/ | ||
get gameName(): string { | ||
return this._data.game_name; | ||
} | ||
|
||
/** | ||
* Retrieves information about the game that is being played on this stream. | ||
*/ | ||
async getGame(): Promise<HelixGame | null> { | ||
return this._client.helix.games.getGameById(this._data.game_id); | ||
} | ||
|
||
/** | ||
* The title of the channel. | ||
*/ | ||
get title(): string { | ||
return this._data.title; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { TwitchApiCallType } from 'twitch-api-call'; | ||
import type { UserIdResolvable } from '../../../Toolkit/UserTools'; | ||
import { extractUserId } from '../../../Toolkit/UserTools'; | ||
import { BaseApi } from '../../BaseApi'; | ||
import type { HelixPaginatedResponse } from '../HelixResponse'; | ||
import type { HelixChannelData } from './HelixChannel'; | ||
import { HelixChannel } from './HelixChannel'; | ||
|
||
/** | ||
* Channel data to update using {@HelixChannelApi#updateChannel}. | ||
*/ | ||
export interface HelixChannelUpdate { | ||
/** | ||
* The language of the stream. | ||
*/ | ||
language?: string; | ||
|
||
/** | ||
* The ID of the game you're playing. | ||
*/ | ||
gameId?: string; | ||
|
||
/** | ||
* The title of the stream. | ||
*/ | ||
title?: string; | ||
} | ||
|
||
/** | ||
* The Helix API methods that deal with channels. | ||
* | ||
* Can be accessed using `client.helix.channels` on an {@ApiClient} instance. | ||
* | ||
* ## Example | ||
* ```ts | ||
* const api = new ApiClient(new StaticAuthProvider(clientId, accessToken)); | ||
* const channel = await api.helix.channels.getChannelInfoByUserId('125328655'); | ||
* ``` | ||
*/ | ||
export class HelixChannelApi extends BaseApi { | ||
/** | ||
* Retrieves the channel data for the given user. | ||
* | ||
* @param user The user you want to get channel info for. | ||
*/ | ||
async getChannelInfo(user: UserIdResolvable): Promise<HelixChannel | null> { | ||
const userId = extractUserId(user); | ||
const result = await this._client.callApi<HelixPaginatedResponse<HelixChannelData>>({ | ||
type: TwitchApiCallType.Helix, | ||
url: 'channels', | ||
query: { | ||
broadcaster_id: userId | ||
} | ||
}); | ||
|
||
return result.data.length ? new HelixChannel(result.data[0], this._client) : null; | ||
} | ||
|
||
/** | ||
* Updates the given user's channel data. | ||
* | ||
* @param user The user you want to update channel info for. | ||
* @param data The channel info to set. | ||
*/ | ||
async updateChannelInfo(user: UserIdResolvable, data: HelixChannelUpdate): Promise<void> { | ||
const userId = extractUserId(user); | ||
await this._client.callApi({ | ||
type: TwitchApiCallType.Helix, | ||
url: 'channels', | ||
method: 'PATCH', | ||
scope: 'user:edit:broadcast', | ||
query: { | ||
broadcaster_id: userId | ||
}, | ||
jsonBody: { | ||
game_id: data.gameId, | ||
broadcaster_language: data.language, | ||
title: data.title | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters