mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-21 04:25:27 -04:00
chore(housekeeping): make linter happy
This commit is contained in:
+13
-14
@@ -158,7 +158,7 @@ export class Client extends EventEmitter {
|
||||
}
|
||||
|
||||
this.on("message", async (message) => {
|
||||
let channel = message.channel;
|
||||
const channel = message.channel;
|
||||
if (!channel) return;
|
||||
if (channel.channel_type === "DirectMessage") {
|
||||
channel.active = true;
|
||||
@@ -210,7 +210,7 @@ export class Client extends EventEmitter {
|
||||
url: T,
|
||||
data?: Route<M, T>["data"],
|
||||
): Promise<Route<M, T>["response"]> {
|
||||
let res = await this.Axios.request({
|
||||
const res = await this.Axios.request({
|
||||
method,
|
||||
data,
|
||||
url,
|
||||
@@ -231,7 +231,7 @@ export class Client extends EventEmitter {
|
||||
url: T,
|
||||
data: AxiosRequestConfig,
|
||||
): Promise<Route<M, T>["response"]> {
|
||||
let res = await this.Axios.request({
|
||||
const res = await this.Axios.request({
|
||||
...data,
|
||||
method,
|
||||
url,
|
||||
@@ -313,7 +313,7 @@ export class Client extends EventEmitter {
|
||||
// Check onboarding status and connect to notifications service.
|
||||
private async $connect() {
|
||||
this.Axios.defaults.headers = this.$generateHeaders();
|
||||
let { onboarding } = await this.req("GET", "/onboard/hello");
|
||||
const { onboarding } = await this.req("GET", "/onboard/hello");
|
||||
if (onboarding) {
|
||||
return (username: string, loginAfterSuccess?: boolean) =>
|
||||
this.completeOnboarding({ username }, loginAfterSuccess);
|
||||
@@ -385,14 +385,14 @@ export class Client extends EventEmitter {
|
||||
data: { [key: string]: object | string },
|
||||
timestamp?: number,
|
||||
) {
|
||||
let requestData: { [key: string]: string } = {};
|
||||
for (let key of Object.keys(data)) {
|
||||
let value = data[key];
|
||||
const requestData: { [key: string]: string } = {};
|
||||
for (const key of Object.keys(data)) {
|
||||
const value = data[key];
|
||||
requestData[key] =
|
||||
typeof value === "string" ? value : JSON.stringify(value);
|
||||
}
|
||||
|
||||
let query = timestamp ? `?timestamp=${timestamp}` : "";
|
||||
const query = timestamp ? `?timestamp=${timestamp}` : "";
|
||||
await this.req(
|
||||
"POST",
|
||||
`/sync/settings/set${query}` as "/sync/settings/set",
|
||||
@@ -453,9 +453,8 @@ export class Client extends EventEmitter {
|
||||
*/
|
||||
markdownToText(source: string) {
|
||||
return source
|
||||
.replace(RE_MENTIONS, (sub: string, ...args: any[]) => {
|
||||
const id = args[0],
|
||||
user = this.users.get(id);
|
||||
.replace(RE_MENTIONS, (sub: string, id: string) => {
|
||||
const user = this.users.get(id as string);
|
||||
|
||||
if (user) {
|
||||
return `@${user.username}`;
|
||||
@@ -493,11 +492,11 @@ export class Client extends EventEmitter {
|
||||
) {
|
||||
const [options, allowAnimation, fallback] = args;
|
||||
|
||||
let autumn = this.configuration?.features.autumn;
|
||||
const autumn = this.configuration?.features.autumn;
|
||||
if (!autumn?.enabled) return fallback;
|
||||
if (!attachment) return fallback;
|
||||
|
||||
let { tag, _id, content_type } = attachment;
|
||||
const { tag, _id, content_type } = attachment;
|
||||
|
||||
let query = "";
|
||||
if (options) {
|
||||
@@ -505,7 +504,7 @@ export class Client extends EventEmitter {
|
||||
query =
|
||||
"?" +
|
||||
Object.keys(options)
|
||||
.map((k) => `${k}=${(options as any)[k]}`)
|
||||
.map((k) => `${k}=${options[k as keyof SizeOptions]}`)
|
||||
.join("&");
|
||||
}
|
||||
}
|
||||
|
||||
+2
-7
@@ -301,14 +301,14 @@ type Routes =
|
||||
method: "GET";
|
||||
route: `/users/${Id}/avatar`;
|
||||
data: undefined;
|
||||
response: any;
|
||||
response: unknown;
|
||||
}
|
||||
| {
|
||||
// Fetch default avatar for user.
|
||||
method: "GET";
|
||||
route: `/users/${Id}/default_avatar`;
|
||||
data: undefined;
|
||||
response: any;
|
||||
response: unknown;
|
||||
}
|
||||
/**
|
||||
* Channels
|
||||
@@ -837,8 +837,3 @@ export type Route<
|
||||
M extends RouteMethod,
|
||||
T extends RoutePath,
|
||||
> = ExtractMethodParameters<ExtractRouteParameters<Routes, T>, M>;
|
||||
|
||||
declare function dispatch<T extends RoutePath>(
|
||||
type: T,
|
||||
args: ExtractRouteParameters<Routes, T>,
|
||||
): void;
|
||||
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
type Tail<T extends any[]> = T extends [infer A, ...infer R] ? R : never;
|
||||
type Tail<T extends unknown[]> = T extends [infer _A, ...infer R] ? R : never;
|
||||
|
||||
+5
-5
@@ -17,7 +17,7 @@ export default class Bots {
|
||||
* @returns Bot and User object
|
||||
*/
|
||||
async fetch(id: string) {
|
||||
let { bot, user } = await this.client.req(
|
||||
const { bot, user } = await this.client.req(
|
||||
"GET",
|
||||
`/bots/${id}` as "/bots/id",
|
||||
);
|
||||
@@ -74,7 +74,7 @@ export default class Bots {
|
||||
`/bots/@me`,
|
||||
);
|
||||
|
||||
let users = [];
|
||||
const users = [];
|
||||
for (const obj of userObjects) {
|
||||
users.push(await this.client.users.fetch(obj._id, obj));
|
||||
}
|
||||
@@ -91,7 +91,7 @@ export default class Bots {
|
||||
await this.client.req("PATCH", `/bots/${id}` as "/bots/id", data);
|
||||
|
||||
if (data.name) {
|
||||
let user = this.client.users.get(id);
|
||||
const user = this.client.users.get(id);
|
||||
if (user) {
|
||||
runInAction(() => {
|
||||
user!.username = data.name!;
|
||||
@@ -105,8 +105,8 @@ export default class Bots {
|
||||
* @param data Bot creation data
|
||||
*/
|
||||
async create(data: Route<"POST", "/bots/create">["data"]) {
|
||||
let bot = await this.client.req("POST", "/bots/create", data);
|
||||
let user = await this.client.users.fetch(bot._id, {
|
||||
const bot = await this.client.req("POST", "/bots/create", data);
|
||||
const user = await this.client.users.fetch(bot._id, {
|
||||
_id: bot._id,
|
||||
username: data.name,
|
||||
bot: {
|
||||
|
||||
+23
-24
@@ -220,14 +220,13 @@ export class Channel {
|
||||
|
||||
@action update(data: Partial<ChannelI>, clear?: RemoveChannelField) {
|
||||
const apply = (key: string, target?: string) => {
|
||||
// This code has been tested.
|
||||
if (
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
typeof data[key] !== "undefined" &&
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
!isEqual(this[target ?? key], data[key])
|
||||
) {
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
this[target ?? key] = data[key];
|
||||
}
|
||||
};
|
||||
@@ -278,7 +277,7 @@ export class Channel {
|
||||
* @returns An array of the channel's members.
|
||||
*/
|
||||
async fetchMembers() {
|
||||
let members = await this.client.req(
|
||||
const members = await this.client.req(
|
||||
"GET",
|
||||
`/channels/${this._id}/members` as "/channels/id/members",
|
||||
);
|
||||
@@ -319,7 +318,7 @@ export class Channel {
|
||||
this.channel_type === "TextChannel" ||
|
||||
this.channel_type === "VoiceChannel"
|
||||
) {
|
||||
let server = this.server;
|
||||
const server = this.server;
|
||||
if (server) {
|
||||
server.channel_ids = server.channel_ids.filter(
|
||||
(x) => x !== this._id,
|
||||
@@ -365,12 +364,12 @@ export class Channel {
|
||||
nonce?: string;
|
||||
}),
|
||||
) {
|
||||
let msg: Route<"POST", "/channels/id/messages">["data"] = {
|
||||
const msg: Route<"POST", "/channels/id/messages">["data"] = {
|
||||
nonce: ulid(),
|
||||
...(typeof data === "string" ? { content: data } : data),
|
||||
};
|
||||
|
||||
let message = await this.client.req(
|
||||
const message = await this.client.req(
|
||||
"POST",
|
||||
`/channels/${this._id}/messages` as "/channels/id/messages",
|
||||
msg,
|
||||
@@ -384,7 +383,7 @@ export class Channel {
|
||||
* @returns The message
|
||||
*/
|
||||
async fetchMessage(message_id: string) {
|
||||
let message = await this.client.req(
|
||||
const message = await this.client.req(
|
||||
"GET",
|
||||
`/channels/${this._id}/messages/${message_id}` as "/channels/id/messages/id",
|
||||
);
|
||||
@@ -402,7 +401,7 @@ export class Channel {
|
||||
"include_users"
|
||||
>,
|
||||
) {
|
||||
let messages = (await this.client.request(
|
||||
const messages = (await this.client.request(
|
||||
"GET",
|
||||
`/channels/${this._id}/messages` as "/channels/id/messages",
|
||||
{ params },
|
||||
@@ -421,7 +420,7 @@ export class Channel {
|
||||
"include_users"
|
||||
>,
|
||||
) {
|
||||
let data = (await this.client.request(
|
||||
const data = (await this.client.request(
|
||||
"GET",
|
||||
`/channels/${this._id}/messages` as "/channels/id/messages",
|
||||
{ params: { ...params, include_users: true } },
|
||||
@@ -446,7 +445,7 @@ export class Channel {
|
||||
"include_users"
|
||||
>,
|
||||
) {
|
||||
let messages = (await this.client.req(
|
||||
const messages = (await this.client.req(
|
||||
"POST",
|
||||
`/channels/${this._id}/search` as "/channels/id/search",
|
||||
params,
|
||||
@@ -465,7 +464,7 @@ export class Channel {
|
||||
"include_users"
|
||||
>,
|
||||
) {
|
||||
let data = (await this.client.req(
|
||||
const data = (await this.client.req(
|
||||
"POST",
|
||||
`/channels/${this._id}/search` as "/channels/id/search",
|
||||
{ ...params, include_users: true },
|
||||
@@ -485,7 +484,7 @@ export class Channel {
|
||||
* @returns The stale messages
|
||||
*/
|
||||
async fetchStale(ids: string[]) {
|
||||
let data = await this.client.req(
|
||||
const data = await this.client.req(
|
||||
"POST",
|
||||
`/channels/${this._id}/messages/stale` as "/channels/id/messages/stale",
|
||||
{ ids },
|
||||
@@ -506,7 +505,7 @@ export class Channel {
|
||||
* @returns Newly created invite code
|
||||
*/
|
||||
async createInvite() {
|
||||
let res = await this.client.req(
|
||||
const res = await this.client.req(
|
||||
"POST",
|
||||
`/channels/${this._id}/invites` as "/channels/id/invites",
|
||||
);
|
||||
@@ -566,7 +565,7 @@ export class Channel {
|
||||
* @param role_id Role Id, set to 'default' to affect all users
|
||||
* @param permissions Permission number, removes permission if unset
|
||||
*/
|
||||
async setPermissions(role_id: string = "default", permissions?: number) {
|
||||
async setPermissions(role_id = "default", permissions?: number) {
|
||||
return await this.client.req(
|
||||
"PUT",
|
||||
`/channels/${this._id}/permissions/${role_id}` as "/channels/id/permissions/id",
|
||||
@@ -597,7 +596,7 @@ export class Channel {
|
||||
case "SavedMessages":
|
||||
return U32_MAX;
|
||||
case "DirectMessage": {
|
||||
let user_permissions = this.recipient?.permission || 0;
|
||||
const user_permissions = this.recipient?.permission || 0;
|
||||
|
||||
if (user_permissions & UserPermission.SendMessage) {
|
||||
return DEFAULT_PERMISSION_DM;
|
||||
@@ -614,13 +613,13 @@ export class Channel {
|
||||
}
|
||||
case "TextChannel":
|
||||
case "VoiceChannel": {
|
||||
let server = this.server;
|
||||
const server = this.server;
|
||||
if (typeof server === "undefined") return 0;
|
||||
|
||||
if (server.owner === this.client.user?._id) {
|
||||
return U32_MAX;
|
||||
} else {
|
||||
let member = this.client.members.getKey({
|
||||
const member = this.client.members.getKey({
|
||||
user: this.client.user!._id,
|
||||
server: server._id,
|
||||
}) ?? { roles: null };
|
||||
@@ -632,7 +631,7 @@ export class Channel {
|
||||
server.default_permissions[1]) >>> 0;
|
||||
|
||||
if (member.roles) {
|
||||
for (let role of member.roles) {
|
||||
for (const role of member.roles) {
|
||||
perm |= (this.role_permissions?.[role] ?? 0) >>> 0;
|
||||
perm |=
|
||||
(server.roles?.[role].permissions[1] ?? 0) >>>
|
||||
@@ -654,7 +653,7 @@ export default class Channels extends Collection<string, Channel> {
|
||||
}
|
||||
|
||||
@action $get(id: string, data?: ChannelI) {
|
||||
let channel = this.get(id)!;
|
||||
const channel = this.get(id)!;
|
||||
if (data) channel.update(data);
|
||||
return channel;
|
||||
}
|
||||
@@ -666,7 +665,7 @@ export default class Channels extends Collection<string, Channel> {
|
||||
*/
|
||||
async fetch(id: string, data?: ChannelI) {
|
||||
if (this.has(id)) return this.$get(id);
|
||||
let res =
|
||||
const res =
|
||||
data ??
|
||||
(await this.client.req("GET", `/channels/${id}` as "/channels/id"));
|
||||
return this.createObj(res);
|
||||
@@ -681,7 +680,7 @@ export default class Channels extends Collection<string, Channel> {
|
||||
*/
|
||||
createObj(data: ChannelI, emit?: boolean | number) {
|
||||
if (this.has(data._id)) return this.$get(data._id);
|
||||
let channel = new Channel(this.client, data);
|
||||
const channel = new Channel(this.client, data);
|
||||
|
||||
runInAction(() => {
|
||||
this.set(data._id, channel);
|
||||
@@ -697,7 +696,7 @@ export default class Channels extends Collection<string, Channel> {
|
||||
* @returns The newly-created group
|
||||
*/
|
||||
async createGroup(data: Route<"POST", "/channels/create">["data"]) {
|
||||
let group = await this.client.req("POST", `/channels/create`, data);
|
||||
const group = await this.client.req("POST", `/channels/create`, data);
|
||||
return (await this.fetch(group._id, group))!;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -53,12 +53,12 @@ export class Member {
|
||||
const apply = (key: string) => {
|
||||
// This code has been tested.
|
||||
if (
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
typeof data[key] !== "undefined" &&
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
!isEqual(this[key], data[key])
|
||||
) {
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
this[key] = data[key];
|
||||
}
|
||||
};
|
||||
@@ -133,7 +133,7 @@ export default class Members extends Collection<string, Member> {
|
||||
}
|
||||
|
||||
@action $get(id: MemberCompositeKey, data?: MemberI) {
|
||||
let member = this.getKey(id)!;
|
||||
const member = this.getKey(id)!;
|
||||
if (data) member.update(data);
|
||||
return member;
|
||||
}
|
||||
@@ -148,7 +148,7 @@ export default class Members extends Collection<string, Member> {
|
||||
createObj(data: MemberI, emit?: boolean | number) {
|
||||
if (this.hasKey(data._id)) return this.$get(data._id, data);
|
||||
|
||||
let member = new Member(this.client, data);
|
||||
const member = new Member(this.client, data);
|
||||
|
||||
runInAction(() => {
|
||||
this.setKey(data._id, member);
|
||||
|
||||
@@ -125,16 +125,16 @@ export class Message {
|
||||
) => {
|
||||
// This code has been tested.
|
||||
if (
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
typeof data[key] !== "undefined" &&
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
!isEqual(this[target ?? key], data[key])
|
||||
) {
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
this[target ?? key] = transform
|
||||
? // @ts-expect-error
|
||||
? // @ts-expect-error TODO: clean up types here
|
||||
transform(data[key])
|
||||
: // @ts-expect-error
|
||||
: // @ts-expect-error TODO: clean up types here
|
||||
data[key];
|
||||
}
|
||||
};
|
||||
@@ -186,7 +186,7 @@ export class Message {
|
||||
}),
|
||||
mention = true,
|
||||
) {
|
||||
let obj = typeof data === "string" ? { content: data } : data;
|
||||
const obj = typeof data === "string" ? { content: data } : data;
|
||||
return this.channel?.sendMessage({
|
||||
...obj,
|
||||
replies: [{ id: this._id, mention }],
|
||||
@@ -201,7 +201,7 @@ export default class Messages extends Collection<string, Message> {
|
||||
}
|
||||
|
||||
@action $get(id: string, data?: MessageI) {
|
||||
let msg = this.get(id)!;
|
||||
const msg = this.get(id)!;
|
||||
if (data) msg.update(data);
|
||||
return msg;
|
||||
}
|
||||
@@ -215,7 +215,7 @@ export default class Messages extends Collection<string, Message> {
|
||||
*/
|
||||
createObj(data: MessageI, emit?: boolean | number) {
|
||||
if (this.has(data._id)) return this.$get(data._id);
|
||||
let message = new Message(this.client, data);
|
||||
const message = new Message(this.client, data);
|
||||
|
||||
runInAction(() => {
|
||||
this.set(data._id, message);
|
||||
|
||||
+12
-12
@@ -81,12 +81,12 @@ export class Server {
|
||||
const apply = (key: string, target?: string) => {
|
||||
// This code has been tested.
|
||||
if (
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
typeof data[key] !== "undefined" &&
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
!isEqual(this[target ?? key], data[key])
|
||||
) {
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
this[target ?? key] = data[key];
|
||||
}
|
||||
};
|
||||
@@ -221,7 +221,7 @@ export class Server {
|
||||
* @param permissions Permission number, removes permission if unset
|
||||
*/
|
||||
async setPermissions(
|
||||
role_id: string = "default",
|
||||
role_id = "default",
|
||||
permissions?: { server: number; channel: number },
|
||||
) {
|
||||
return await this.client.req(
|
||||
@@ -295,7 +295,7 @@ export class Server {
|
||||
* @returns An array of the server's members and their user objects.
|
||||
*/
|
||||
async fetchMembers() {
|
||||
let data = await this.client.req(
|
||||
const data = await this.client.req(
|
||||
"GET",
|
||||
`/servers/${this._id}/members` as "/servers/id/members",
|
||||
);
|
||||
@@ -319,7 +319,7 @@ export class Server {
|
||||
if (this.owner === this.client.user?._id) {
|
||||
return U32_MAX;
|
||||
} else {
|
||||
let member = this.client.members.getKey({
|
||||
const member = this.client.members.getKey({
|
||||
user: this.client.user!._id,
|
||||
server: this._id,
|
||||
}) ?? { roles: null };
|
||||
@@ -328,7 +328,7 @@ export class Server {
|
||||
|
||||
let perm = this.default_permissions[0] >>> 0;
|
||||
if (member.roles) {
|
||||
for (let role of member.roles) {
|
||||
for (const role of member.roles) {
|
||||
perm |= (this.roles?.[role].permissions[0] ?? 0) >>> 0;
|
||||
}
|
||||
}
|
||||
@@ -345,7 +345,7 @@ export default class Servers extends Collection<string, Server> {
|
||||
}
|
||||
|
||||
@action $get(id: string, data?: ServerI) {
|
||||
let server = this.get(id)!;
|
||||
const server = this.get(id)!;
|
||||
if (data) server.update(data);
|
||||
return server;
|
||||
}
|
||||
@@ -357,12 +357,12 @@ export default class Servers extends Collection<string, Server> {
|
||||
*/
|
||||
async fetch(id: string, data?: ServerI) {
|
||||
if (this.has(id)) return this.$get(id, data);
|
||||
let res =
|
||||
const res =
|
||||
data ??
|
||||
(await this.client.req("GET", `/servers/${id}` as "/servers/id"));
|
||||
|
||||
return runInAction(async () => {
|
||||
for (let channel of res.channels) {
|
||||
for (const channel of res.channels) {
|
||||
// ! FIXME: add route for fetching all channels
|
||||
// ! FIXME: OR the WHOLE server
|
||||
try {
|
||||
@@ -383,7 +383,7 @@ export default class Servers extends Collection<string, Server> {
|
||||
*/
|
||||
createObj(data: ServerI) {
|
||||
if (this.has(data._id)) return this.$get(data._id, data);
|
||||
let server = new Server(this.client, data);
|
||||
const server = new Server(this.client, data);
|
||||
|
||||
runInAction(() => {
|
||||
this.set(data._id, server);
|
||||
@@ -398,7 +398,7 @@ export default class Servers extends Collection<string, Server> {
|
||||
* @returns The newly-created server
|
||||
*/
|
||||
async createServer(data: Route<"POST", "/servers/create">["data"]) {
|
||||
let server = await this.client.req("POST", `/servers/create`, data);
|
||||
const server = await this.client.req("POST", `/servers/create`, data);
|
||||
return this.fetch(server._id, server);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -71,12 +71,12 @@ export class User {
|
||||
const apply = (key: string) => {
|
||||
// This code has been tested.
|
||||
if (
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
typeof data[key] !== "undefined" &&
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
!isEqual(this[key], data[key])
|
||||
) {
|
||||
// @ts-expect-error
|
||||
// @ts-expect-error TODO: clean up types here
|
||||
this[key] = data[key];
|
||||
|
||||
if (key === "relationship") {
|
||||
@@ -240,7 +240,7 @@ export default class Users extends Collection<string, User> {
|
||||
}
|
||||
|
||||
@action $get(id: string, data?: UserI) {
|
||||
let user = this.get(id)!;
|
||||
const user = this.get(id)!;
|
||||
if (data) user.update(data);
|
||||
return user;
|
||||
}
|
||||
@@ -252,7 +252,7 @@ export default class Users extends Collection<string, User> {
|
||||
*/
|
||||
async fetch(id: string, data?: UserI) {
|
||||
if (this.has(id)) return this.$get(id, data);
|
||||
let res =
|
||||
const res =
|
||||
data ??
|
||||
(await this.client.req("GET", `/users/${id}` as "/users/id"));
|
||||
return this.createObj(res);
|
||||
@@ -266,7 +266,7 @@ export default class Users extends Collection<string, User> {
|
||||
*/
|
||||
createObj(data: UserI) {
|
||||
if (this.has(data._id)) return this.$get(data._id, data);
|
||||
let user = new User(this.client, data);
|
||||
const user = new User(this.client, data);
|
||||
|
||||
runInAction(() => {
|
||||
this.set(data._id, user);
|
||||
|
||||
+3
-3
@@ -8,7 +8,7 @@ config();
|
||||
// Copy and paste `.env.example` to `.env` and edit accordingly.
|
||||
|
||||
function user() {
|
||||
let client = new Client({
|
||||
const client = new Client({
|
||||
apiURL: process.env.API_URL,
|
||||
debug: true,
|
||||
});
|
||||
@@ -21,7 +21,7 @@ function user() {
|
||||
if (message.content === "sus") {
|
||||
message.channel!.sendMessage("sus!");
|
||||
} else if (message.content === "bot") {
|
||||
let bot = await client.req("POST", "/bots/create", {
|
||||
const bot = await client.req("POST", "/bots/create", {
|
||||
name: "basedbot12",
|
||||
});
|
||||
message.channel!.sendMessage(JSON.stringify(bot));
|
||||
@@ -63,7 +63,7 @@ function user() {
|
||||
}
|
||||
|
||||
function bot() {
|
||||
let client = new Client({
|
||||
const client = new Client({
|
||||
apiURL: process.env.API_URL,
|
||||
});
|
||||
|
||||
|
||||
+29
-29
@@ -53,7 +53,7 @@ export class WebSocketClient {
|
||||
)
|
||||
return;
|
||||
|
||||
let data = JSON.stringify(notification);
|
||||
const data = JSON.stringify(notification);
|
||||
if (this.client.debug) console.debug("[<] PACKET", data);
|
||||
this.ws.send(data);
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export class WebSocketClient {
|
||||
|
||||
return new Promise((resolve, $reject) => {
|
||||
let thrown = false;
|
||||
const reject = (err: any) => {
|
||||
const reject = (err: unknown) => {
|
||||
if (!thrown) {
|
||||
thrown = true;
|
||||
$reject(err);
|
||||
@@ -88,7 +88,7 @@ export class WebSocketClient {
|
||||
);
|
||||
}
|
||||
|
||||
let ws = new WebSocket(this.client.configuration.ws);
|
||||
const ws = new WebSocket(this.client.configuration.ws);
|
||||
this.ws = ws;
|
||||
|
||||
ws.onopen = () => {
|
||||
@@ -105,13 +105,13 @@ export class WebSocketClient {
|
||||
}
|
||||
};
|
||||
|
||||
let timeouts: Record<string, any> = {};
|
||||
let handle = async (msg: WebSocket.MessageEvent) => {
|
||||
let data = msg.data;
|
||||
const timeouts: Record<string, number> = {};
|
||||
const handle = async (msg: WebSocket.MessageEvent) => {
|
||||
const data = msg.data;
|
||||
if (typeof data !== "string") return;
|
||||
|
||||
if (this.client.debug) console.debug("[>] PACKET", data);
|
||||
let packet = JSON.parse(data) as ClientboundNotification;
|
||||
const packet = JSON.parse(data) as ClientboundNotification;
|
||||
this.client.emit("packet", packet);
|
||||
switch (packet.type) {
|
||||
case "Error": {
|
||||
@@ -130,19 +130,19 @@ export class WebSocketClient {
|
||||
runInAction(() => {
|
||||
if (packet.type !== "Ready") throw 0;
|
||||
|
||||
for (let user of packet.users) {
|
||||
for (const user of packet.users) {
|
||||
this.client.users.createObj(user);
|
||||
}
|
||||
|
||||
for (let channel of packet.channels) {
|
||||
for (const channel of packet.channels) {
|
||||
this.client.channels.createObj(channel);
|
||||
}
|
||||
|
||||
for (let server of packet.servers) {
|
||||
for (const server of packet.servers) {
|
||||
this.client.servers.createObj(server);
|
||||
}
|
||||
|
||||
for (let member of packet.members) {
|
||||
for (const member of packet.members) {
|
||||
this.client.members.createObj(member);
|
||||
}
|
||||
});
|
||||
@@ -165,7 +165,7 @@ export class WebSocketClient {
|
||||
data: +new Date(),
|
||||
}),
|
||||
this.client.heartbeat * 1e3,
|
||||
) as any;
|
||||
) as unknown as number;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -209,11 +209,11 @@ export class WebSocketClient {
|
||||
await this.client.users.fetch(packet.author);
|
||||
}
|
||||
|
||||
let channel = await this.client.channels.fetch(
|
||||
const channel = await this.client.channels.fetch(
|
||||
packet.channel,
|
||||
);
|
||||
if (channel.channel_type === "TextChannel") {
|
||||
let server = await this.client.servers.fetch(
|
||||
const server = await this.client.servers.fetch(
|
||||
channel.server_id!,
|
||||
);
|
||||
if (
|
||||
@@ -229,7 +229,7 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "MessageUpdate": {
|
||||
let message = this.client.messages.get(packet.id);
|
||||
const message = this.client.messages.get(packet.id);
|
||||
if (message) {
|
||||
message.update(packet.data);
|
||||
this.client.emit("message/update", message);
|
||||
@@ -251,7 +251,7 @@ export class WebSocketClient {
|
||||
packet.channel_type === "TextChannel" ||
|
||||
packet.channel_type === "VoiceChannel"
|
||||
) {
|
||||
let server = await this.client.servers.fetch(
|
||||
const server = await this.client.servers.fetch(
|
||||
packet.server,
|
||||
);
|
||||
server.channel_ids.push(packet._id);
|
||||
@@ -263,7 +263,7 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ChannelUpdate": {
|
||||
let channel = this.client.channels.get(packet.id);
|
||||
const channel = this.client.channels.get(packet.id);
|
||||
if (channel) {
|
||||
channel.update(packet.data, packet.clear);
|
||||
this.client.emit("channel/update", channel);
|
||||
@@ -292,7 +292,7 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ServerUpdate": {
|
||||
let server = this.client.servers.get(packet.id);
|
||||
const server = this.client.servers.get(packet.id);
|
||||
if (server) {
|
||||
server.update(packet.data, packet.clear);
|
||||
this.client.emit("server/update", server);
|
||||
@@ -307,7 +307,7 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ServerMemberUpdate": {
|
||||
let member = this.client.members.getKey(packet.id);
|
||||
const member = this.client.members.getKey(packet.id);
|
||||
if (member) {
|
||||
member.update(packet.data, packet.clear);
|
||||
this.client.emit("member/update", member);
|
||||
@@ -368,9 +368,9 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ServerRoleUpdate": {
|
||||
let server = this.client.servers.get(packet.id);
|
||||
const server = this.client.servers.get(packet.id);
|
||||
if (server) {
|
||||
let role = {
|
||||
const role = {
|
||||
...server.roles?.[packet.role_id],
|
||||
...packet.data,
|
||||
} as Role;
|
||||
@@ -389,9 +389,9 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ServerRoleDelete": {
|
||||
let server = this.client.servers.get(packet.id);
|
||||
const server = this.client.servers.get(packet.id);
|
||||
if (server) {
|
||||
let { [packet.role_id]: _, ...roles } =
|
||||
const { [packet.role_id]: _, ...roles } =
|
||||
server.roles ?? {};
|
||||
server.roles = roles;
|
||||
this.client.emit(
|
||||
@@ -411,7 +411,7 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "UserRelationship": {
|
||||
let user = this.client.users.get(packet.user._id);
|
||||
const user = this.client.users.get(packet.user._id);
|
||||
if (user) {
|
||||
user.update({ relationship: packet.status });
|
||||
} else {
|
||||
@@ -422,8 +422,8 @@ export class WebSocketClient {
|
||||
}
|
||||
|
||||
case "ChannelStartTyping": {
|
||||
let channel = this.client.channels.get(packet.id);
|
||||
let user = packet.user;
|
||||
const channel = this.client.channels.get(packet.id);
|
||||
const user = packet.user;
|
||||
|
||||
if (channel) {
|
||||
channel.updateStartTyping(user);
|
||||
@@ -431,7 +431,7 @@ export class WebSocketClient {
|
||||
clearInterval(timeouts[packet.id + user]);
|
||||
timeouts[packet.id + user] = setTimeout(() => {
|
||||
channel!.updateStopTyping(user);
|
||||
}, 3000);
|
||||
}, 3000) as unknown as number;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -462,14 +462,14 @@ export class WebSocketClient {
|
||||
};
|
||||
|
||||
let processing = false;
|
||||
let queue: WebSocket.MessageEvent[] = [];
|
||||
const queue: WebSocket.MessageEvent[] = [];
|
||||
ws.onmessage = async (data) => {
|
||||
queue.push(data);
|
||||
|
||||
if (!processing) {
|
||||
processing = true;
|
||||
while (queue.length > 0) {
|
||||
await handle(queue.shift() as any);
|
||||
await handle(queue.shift()!);
|
||||
}
|
||||
processing = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user