Skip to content

Commit

Permalink
Merge pull request #28 from TTTaevas/chat
Browse files Browse the repository at this point in the history
Introduce functions related to chatting
  • Loading branch information
TTTaevas authored Nov 27, 2023
2 parents 8ed659b + 67005b9 commit 9729c41
Show file tree
Hide file tree
Showing 97 changed files with 802 additions and 442 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ Your `refresh_token` can actually also expire at a (purposefully) unknown time,
- [ ] Lookup Changelog Build // likely won't implement unless I get convinced it's worth the confusion with Get Changelog Build

### Chat
- [ ] Chat Keepalive
- [ ] Create New PM
- [ ] Get Channel Messages
- [ ] Send Message to Channel
- [ ] Join Channel
- [ ] Leave Channel
- [ ] Mark Channel as Read
- [ ] Get Channel List
- [ ] Create Channel
- [ ] Get Channel
- [x] Chat Keepalive
- [x] Create New PM
- [x] Get Channel Messages
- [x] Send Message to Channel
- [x] Join Channel
- [x] Leave Channel
- [x] Mark Channel as Read
- [x] Get Channel List
- [x] Create Channel // split between createChatPrivateChannel() and createChatAnnouncementChannel()
- [x] Get Channel // removing `users` because `channel` would already have this property

### Comments
- [ ] Get Comments
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

218 changes: 166 additions & 52 deletions docs/classes/API.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/classes/APIError.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/enums/RankStatus.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/enums/Rulesets.html

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/functions/generateAuthorizationURL.html

Large diffs are not rendered by default.

59 changes: 37 additions & 22 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/Beatmap.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapDifficultyAttributes.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapDifficultyAttributesFruits.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/BeatmapDifficultyAttributesMania.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapDifficultyAttributesOsu.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapDifficultyAttributesTaiko.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapExtended.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapExtendedWithFailtimes.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/BeatmapPack.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/BeatmapPlaycount.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapUserScore.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapWithBeatmapset.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/BeatmapWithBeatmapsetChecksumMaxcombo.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/Beatmapset.html

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/interfaces/BeatmapsetExtended.html

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions docs/interfaces/BeatmapsetExtendedPlus.html

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/interfaces/BeatmapsetExtendedWithBeatmapExtended.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/ChangelogBuildWithChangelogentriesVersions.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/ChangelogBuildWithUpdatestreams.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/interfaces/ChatChannel.html

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions docs/interfaces/ChatChannelWithDetails.html

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions docs/interfaces/ChatMessage.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/Event.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventAchievement.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/EventBeatmap.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/EventBeatmapPlaycount.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/EventBeatmapset.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/EventBeatmapsetApprove.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/EventBeatmapsetDelete.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/EventBeatmapsetRevive.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/EventBeatmapsetUpdate.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/EventBeatmapsetUpload.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/EventRank.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/EventRankLost.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventUser.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventUserSupportAgain.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventUserSupportFirst.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventUserSupportGift.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/EventUsernameChange.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions docs/interfaces/ForumPost.html

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/interfaces/ForumTopic.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/KudosuHistory.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/Leader.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/Match.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/MatchInfo.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/MultiplayerScore.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/MultiplayerScores.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions docs/interfaces/NewsPost.html

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/interfaces/NewsPostWithContentNavigation.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/PlaylistItem.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions docs/interfaces/PollConfig.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/Rankings.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/RankingsCountry.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/RankingsSpotlight.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/Room.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/Score.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/ScoreWithMatch.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/ScoreWithUser.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/ScoreWithUserBeatmap.html

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions docs/interfaces/ScoreWithUserBeatmapBeatmapset.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/SearchResultUser.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/SearchResultWiki.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/Spotlight.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/interfaces/SpotlightWithParticipantcount.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/UpdateStream.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/User.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/UserExtended.html

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions docs/interfaces/UserExtendedWithStatisticsrulesets.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/interfaces/UserSilence.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/UserStatistics.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/UserStatisticsWithCountryrank.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/interfaces/UserStatisticsWithUser.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/UserWithCountry.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/UserWithCountryCover.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/interfaces/UserWithKudosu.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/interfaces/WikiPage.html

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<a href="interfaces/ChangelogBuildWithChangelogentriesVersions.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Changelog<wbr/>Build<wbr/>With<wbr/>Changelogentries<wbr/>Versions</span></a>
<a href="interfaces/ChangelogBuildWithUpdatestreams.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Changelog<wbr/>Build<wbr/>With<wbr/>Updatestreams</span></a>
<a href="interfaces/ChangelogBuildWithUpdatestreamsChangelogentries.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Changelog<wbr/>Build<wbr/>With<wbr/>Updatestreams<wbr/>Changelogentries</span></a>
<a href="interfaces/ChatChannel.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Chat<wbr/>Channel</span></a>
<a href="interfaces/ChatChannelWithDetails.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Chat<wbr/>Channel<wbr/>With<wbr/>Details</span></a>
<a href="interfaces/ChatMessage.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Chat<wbr/>Message</span></a>
<a href="interfaces/Event.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event</span></a>
<a href="interfaces/EventAchievement.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event<wbr/>Achievement</span></a>
<a href="interfaces/EventBeatmap.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event<wbr/>Beatmap</span></a>
Expand All @@ -40,13 +43,18 @@
<a href="interfaces/EventUserSupportFirst.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event<wbr/>User<wbr/>Support<wbr/>First</span></a>
<a href="interfaces/EventUserSupportGift.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event<wbr/>User<wbr/>Support<wbr/>Gift</span></a>
<a href="interfaces/EventUsernameChange.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Event<wbr/>Username<wbr/>Change</span></a>
<a href="interfaces/ForumPost.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Forum<wbr/>Post</span></a>
<a href="interfaces/ForumTopic.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Forum<wbr/>Topic</span></a>
<a href="interfaces/KudosuHistory.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Kudosu<wbr/>History</span></a>
<a href="interfaces/Leader.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Leader</span></a>
<a href="interfaces/Match.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Match</span></a>
<a href="interfaces/MatchInfo.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Match<wbr/>Info</span></a>
<a href="interfaces/MultiplayerScore.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Multiplayer<wbr/>Score</span></a>
<a href="interfaces/MultiplayerScores.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Multiplayer<wbr/>Scores</span></a>
<a href="interfaces/NewsPost.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>News<wbr/>Post</span></a>
<a href="interfaces/NewsPostWithContentNavigation.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>News<wbr/>Post<wbr/>With<wbr/>Content<wbr/>Navigation</span></a>
<a href="interfaces/PlaylistItem.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Playlist<wbr/>Item</span></a>
<a href="interfaces/PollConfig.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Poll<wbr/>Config</span></a>
<a href="interfaces/Rankings.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Rankings</span></a>
<a href="interfaces/RankingsCountry.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Rankings<wbr/>Country</span></a>
<a href="interfaces/RankingsSpotlight.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>Rankings<wbr/>Spotlight</span></a>
Expand All @@ -64,6 +72,7 @@
<a href="interfaces/User.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User</span></a>
<a href="interfaces/UserExtended.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Extended</span></a>
<a href="interfaces/UserExtendedWithStatisticsrulesets.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Extended<wbr/>With<wbr/>Statisticsrulesets</span></a>
<a href="interfaces/UserSilence.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Silence</span></a>
<a href="interfaces/UserStatistics.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Statistics</span></a>
<a href="interfaces/UserStatisticsWithCountryrank.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Statistics<wbr/>With<wbr/>Countryrank</span></a>
<a href="interfaces/UserStatisticsWithUser.html" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-256"></use></svg><span>User<wbr/>Statistics<wbr/>With<wbr/>User</span></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/types/Mod.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/ProfileBanner.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Scope.html

Large diffs are not rendered by default.

58 changes: 40 additions & 18 deletions lib/chat.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,64 @@
import { CurrentUserAttributes, User } from "./user.js";

export type ChannelType =
"PUBLIC" |
"PRIVATE" |
"MULTIPLAYER" |
"SPECTATOR" |
"TEMPORARY" |
"PRIVATE" |
"PM" |
"GROUP" |
"ANNOUNCE";
import { User } from "./user.js";

/**
* Expected from api.keepChatAlive()
*/
export interface UserSilence {
id: number
user_id: number
}

/**
* Expected from api.sendChatPrivateMessage(), api.createChatPrivateChannel()
*/
export interface ChatChannel {
channel_id: number
name: string
description: string | null
icon: string | null
type: ChannelType
type: "PUBLIC" | "PRIVATE" | "MULTIPLAYER" | "SPECTATOR" | "TEMPORARY" | "PRIVATE" | "PM" | "GROUP" | "ANNOUNCE"
moderated: boolean
uuid: string | null
current_user_attributes?: CurrentUserAttributes | null
last_message_id?: number | null
users?: number[] | null
}

/**
* Expected from api.joinChatChannel(), api.getChatChannel()
*/
export interface ChatChannelWithDetails extends ChatChannel {
current_user_attributes: {
can_message: boolean
/**
* The reason why messages can't be sent in this channel
* @remarks Is null if messages can be sent
*/
can_message_error: string | null
/**
* @remarks Is null if no message has been read (I think)
*/
last_read_id: number | null
}
last_message_id: number
/**
* The ids of the users that are in the channel
* @remarks Is empty for public channels
*/
users: number[]
}

/**
* Expected from api.sendChatPrivateMessage(), api.getChatMessages(), api.sendChatMessage()
*/
export interface ChatMessage {
channel_id: number
content: string
is_action: boolean
message_id: number
sender_id: number
timestamp: Date
/**
* Like "action", "markdown", "plain"
*/
type: string
uuid: string | null
sender?: User
uuid?: string | null
sender: User
}
136 changes: 134 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { WikiPage } from "./wiki.js"
import { NewsPost, NewsPostWithContentNavigation } from "./news.js"
import { SearchResultUser, SearchResultWiki } from "./home.js"
import { Rulesets, Mod, Scope } from "./misc.js"
import { ChatChannel, ChatChannelWithDetails, ChatMessage, UserSilence } from "./chat.js"


export { User, UserWithKudosu, UserWithCountry, UserWithCountryCover, UserWithCountryCoverGroupsStatisticsrulesets, UserWithCountryCoverGroupsStatisticsSupport,
Expand All @@ -47,6 +48,7 @@ export { WikiPage } from "./wiki.js"
export { NewsPost, NewsPostWithContentNavigation } from "./news.js"
export { SearchResultUser, SearchResultWiki } from "./home.js"
export { Rulesets, Mod, Scope } from "./misc.js"
export { ChatChannel, ChatChannelWithDetails, ChatMessage, UserSilence } from "./chat.js"

/**
* Some stuff doesn't have the right type to begin with, such as dates, which are being returned as strings, this fixes that
Expand Down Expand Up @@ -362,14 +364,15 @@ export class API {
this.log(true, "Will request again in a few instants...", `(Try #${number_try})`)
const to_wait = (Math.floor(Math.random() * (500 - 100 + 1)) + 100) * 10
await new Promise(res => setTimeout(res, to_wait))
return correctType(await this.request(method, endpoint, parameters, number_try + 1))
return await this.request(method, endpoint, parameters, number_try + 1)
}

throw new APIError(err, `${this.server}/api/v2`, endpoint, parameters)
}

this.log(false, response.statusText, response.status, {endpoint, parameters})
return correctType(await response.json())
// 204 means the request worked as intended and did not give us anything, so we can't `.json()` the response
return response.status !== 204 ? correctType(await response.json()) : undefined
}


Expand Down Expand Up @@ -894,4 +897,133 @@ export class API {
async editForumPost(post: {id: number} | ForumPost, new_text: string): Promise<ForumPost> {
return await this.request("put", `forums/posts/${post.id}`, {body: new_text})
}


// CHAT STUFF

/**
* Needs to be done periodically to reset chat activity timeout
* @remarks Every 30 seconds is a good idea
* @param since UserSilences that are before that will not be returned!
* @returns A list of recent silences
* @scope chat.read
*/
async keepChatAlive(since?: {user_silence?: {id: number} | UserSilence, message?: {message_id: number} | ChatMessage}): Promise<UserSilence[]> {
return await this.request("post", "chat/ack", {history_since: since?.user_silence?.id, since: since?.message?.message_id})
}

/**
* Send a private message to someone!
* @remarks You don't need to use `createChatPrivateChannel` before sending a message
* @param user_target The User you wanna send your message to!
* @param message The message you wanna send
* @param is_action (defaults to false) Is it a command? Like `/me dances`
* @param uuid A client-side message identifier
* @returns The message you sent
* @scope chat.write
*/
async sendChatPrivateMessage(user_target: {id: number} | User, message: string, is_action: boolean = false, uuid?: string):
Promise<{channel: ChatChannel, message: ChatMessage}> {
return await this.request("post", "chat/new", {target_id: user_target.id, message, is_action, uuid})
}

/**
* Get the recent messages of a specific ChatChannel!
* @param channel The Channel you wanna get the messages from
* @param limit (defaults to 20, max 50) The maximum amount of messages you want to get!
* @param since Get the messages sent after this message
* @param until Get the messages sent up to but not including this message
* @scope chat.read
*/
async getChatMessages(channel: {channel_id: number} | ChatChannel, limit: number = 20,
since?: {message_id: number} | ChatMessage, until?: {message_id: number} | ChatMessage): Promise<ChatMessage[]> {
return await this.request("get", `chat/channels/${channel.channel_id}/messages`, {limit, since: since?.message_id, until: until?.message_id})
}

/**
* Send a message in a ChatChannel!
* @param channel The channel in which you want to send your message
* @param message The message you wanna send
* @param is_action (defaults to false) Is it a command? Like `/me dances`
* @returns The newly sent ChatMessage!
* @scope chat.write
*/
async sendChatMessage(channel: {channel_id: number} | ChatChannel, message: string, is_action: boolean = false): Promise<ChatMessage> {
return await this.request("post", `chat/channels/${channel.channel_id}/messages`, {message, is_action})
}

/**
* Join a public or multiplayer ChatChannel, allowing you to interact with it!
* @param channel The channel you wanna join
* @param user (defaults to the presumed authorized user) The user joining the channel
* @scope chat.write_manage
*/
async joinChatChannel(channel: {channel_id: number} | ChatChannel, user?: {id: number} | User): Promise<ChatChannelWithDetails> {
return await this.request("put", `chat/channels/${channel.channel_id}/users/${user?.id || this.user}`)
}

/**
* Leave/Close a public ChatChannel!
* @param channel The channel you wanna join
* @param user (defaults to the presumed authorized user) The user joining the channel
* @scope chat.write_manage
*/
async leaveChatChannel(channel: {channel_id: number} | ChatChannel, user?: {id: number} | User): Promise<void> {
return await this.request("delete", `chat/channels/${channel.channel_id}/users/${user?.id || this.user}`)
}

/**
* Mark a certain channel as read up to a given message!
* @param channel The channel in question
* @param message You're marking this and all the messages before it as read!
* @scope chat.read
*/
async markChatChannelAsRead(channel: {channel_id: number} | ChatChannel, message: {message_id: number} | ChatMessage): Promise<void> {
return await this.request("put",
`chat/channels/${channel.channel_id}/mark-as-read/${message.message_id}`, {channel_id: channel.channel_id, message: message.message_id})
}

/**
* Get a list of all publicly joinable channels!
* @scope chat.read
*/
async getChatChannels(): Promise<ChatChannel[]> {
return await this.request("get", "chat/channels")
}

/**
* Create/Open/Join a private messages chat channel!
* @param user_target The other user able to read and send messages in this channel
* @returns The newly created channel!
* @scope chat.write_manage
*/
async createChatPrivateChannel(user_target: {id: number} | User): Promise<ChatChannel> {
return await this.request("post", "chat/channels", {type: "PM", target_id: user_target.id})
}

/**
* Create a new announcement!
* @remarks From my understanding, this WILL 403 unless the user is kinda special
* @param channel Details of the channel you're creating
* @param user_targets The people that will receive your message
* @param message The message to send with the announcement
* @returns The newly created channel!
* @scope chat.write_manage
*/
async createChatAnnouncementChannel(channel: {name: string, description: string}, user_targets: Array<{id: number} | User>, message: string):
Promise<ChatChannel> {
const target_ids = user_targets.map((u) => u.id)
return await this.request("post", "chat/channels", {type: "ANNOUNCE", channel, target_ids, message})
}

/**
* Get a ChatChannel, and the users in it if it is a private channel!
* @remarks Will 404 if the user has not joined the channel (use `joinChatChannel` for that)
* @param channel The channel in question
* @scope chat.read
*/
async getChatChannel(channel: {channel_id: number} | ChatChannel): Promise<ChatChannelWithDetails> {
const response = await this.request("get", `chat/channels/${channel.channel_id}`)
return response.channel
}
}
Loading

0 comments on commit 9729c41

Please sign in to comment.