Skip to content

Commit

Permalink
feat(user): added DM fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Sup3rFire committed Aug 4, 2021
1 parent be7d85c commit 0dd2758
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/client/ClientUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Room from "../room/Room";
import User from "../user/User";
import Client from "../client/Client";
import { Handling, Presence } from "..";
import { DirectMessage } from "..";

export default class ClientUser extends User {
constructor(data: any, client: Client) {
Expand Down Expand Up @@ -92,17 +93,7 @@ export default interface ClientUser {
/**
* Emitted when the ClientUser receives a message from another User
*/
on(
event: "message",
callback: (message: {
content: string;
content_safe: string;
author: User | undefined;
systemMessage: boolean;
id: string;
ts: string;
}) => void
): this;
on(event: "message", callback: (message: DirectMessage) => void): this;

/**
* Emitted when the ClientUser is invited to a room
Expand Down
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import { TetraChannel } from "./channel/Channel";

export { Client, ClientUser, Room, User, UserManager, TetraChannel };

export interface DirectMessage {
/** Contents of the message. */
content: string;
/** Contents of the message after being passed through TETR.IO's profanity filter. */
content_safe: string;

/** Whether this is a system message. */
system: boolean;

/** The ISO 8601-formatted timestamp of the message. */
ts: string;

/** The id of the message. */
id: string;

/**
* The author of the message.
*/
author?: User;
}

export interface CacheData {
cache: {
status: string;
Expand Down
46 changes: 46 additions & 0 deletions src/user/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import EventEmitter from "events";
import fetch from "node-fetch";
import { DirectMessage } from "..";
import Client from "../client/Client";

export default class User extends EventEmitter {
Expand Down Expand Up @@ -298,4 +300,48 @@ export default class User extends EventEmitter {
data: this._id,
});
}

/**
* Fetches the DMs between the bot and the user
*/
public async getDMs(): Promise<DirectMessage[]> {
const id = this._id;
const token = this.client.token;
const data: {
success: boolean;
error?: any;
dms: {
_id: string;
ts: string;
stream: string;
data: {
content: string;
content_safe: string;
system: boolean;
user: string;
userdata: any;
};
}[];
} = await (
await fetch(`https://tetr.io/api/dms/${encodeURIComponent(id)}`, {
headers: { Authorization: `Bearer ${token}` },
})
).json();
if (!data.success) throw new Error(data.error);
else {
const dms: DirectMessage[] = [];
data.dms.forEach(async (message) => {
dms.push({
content: message.data.content,
content_safe: message.data.content_safe,
system: message.data.system,
id: message._id,
ts: message.ts,
author:
message.data.user == this._id ? this : await this.client.users.fetch(message.data.user),
});
});
return dms;
}
}
}

0 comments on commit 0dd2758

Please sign in to comment.