Skip to content

Commit

Permalink
add kraken/users/chat resource
Browse files Browse the repository at this point in the history
  • Loading branch information
d-fischer committed Jan 6, 2020
1 parent 6ea750d commit 67c942c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/twitch/src/API/Kraken/User/UserAPI.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import HellFreezesOverError from '../../../Errors/HellFreezesOverError';
import HTTPStatusCodeError from '../../../Errors/HTTPStatusCodeError';
import NoSubscriptionProgramError from '../../../Errors/NoSubscriptionProgramError';
import { Cacheable, Cached, CacheEntry, ClearsCache } from '../../../Toolkit/Decorators/Cache';
import BaseAPI from '../../BaseAPI';
import PrivilegedUser from './PrivilegedUser';
import User, { UserData } from './User';
import { entriesToObject, indexBy, mapObject } from '../../../Toolkit/ObjectTools';
import { extractUserId, UserIdResolvable } from '../../../Toolkit/UserTools';
import BaseAPI from '../../BaseAPI';
import EmoteSetList from '../Channel/EmoteSetList';
import UserSubscription from './UserSubscription';
import NoSubscriptionProgramError from '../../../Errors/NoSubscriptionProgramError';
import UserFollow, { UserFollowData } from './UserFollow';
import PrivilegedUser from './PrivilegedUser';
import User, { UserData } from './User';
import UserBlock, { UserBlockData } from './UserBlock';
import HellFreezesOverError from '../../../Errors/HellFreezesOverError';
import HTTPStatusCodeError from '../../../Errors/HTTPStatusCodeError';
import UserChatInfo, { UserChatInfoData } from './UserChatInfo';
import UserFollow, { UserFollowData } from './UserFollow';
import UserSubscription from './UserSubscription';

/**
* The API methods that deal with users.
Expand Down Expand Up @@ -100,6 +101,19 @@ export default class UserAPI extends BaseAPI {
return { ...cachedUsers, ...users };
}

/**
* Retrieves information about the user's chat appearance and privileges.
*
* @param user The user you want to get chat info for.
*/
@Cached(3600)
async getChatInfo(user: UserIdResolvable) {
const userId = extractUserId(user);

const data = await this._client.callAPI<UserChatInfoData>({ url: `users/${userId}/chat` });
return new UserChatInfo(data, this._client);
}

/**
* Retrieves the emotes a given user can use.
*
Expand Down
62 changes: 62 additions & 0 deletions packages/twitch/src/API/Kraken/User/UserChatInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { NonEnumerable } from '../../../Toolkit/Decorators/NonEnumerable';
import TwitchClient from '../../../TwitchClient';

interface UserChatInfoGlobalBadgeData {
id: string;
version: string;
}

export interface UserChatInfoData {
_id: string;
login: string;
display_name: string;
color: string;
is_verified_bot: boolean;
is_known_bot: boolean;
badges: UserChatInfoGlobalBadgeData[];
}

export default class UserChatInfo {
@NonEnumerable private readonly _client: TwitchClient;

/** @private */
constructor(private readonly _data: UserChatInfoData, client: TwitchClient) {
this._client = client;
}

get id() {
return this._data._id;
}

async getUser() {
return this._client.kraken.users.getUser(this._data._id);
}

get userName() {
return this._data.login;
}

get displayName() {
return this._data.display_name;
}

get color() {
return this._data.color;
}

get isKnownBot() {
return this._data.is_known_bot;
}

get isVerifiedBot() {
return this._data.is_verified_bot;
}

get isAtLeastKnownBot() {
return this._data.is_known_bot || this._data.is_verified_bot;
}

hasBadge(id: string) {
return this._data.badges.some(badge => badge.id === id);
}
}

0 comments on commit 67c942c

Please sign in to comment.