Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stuyy committed May 21, 2020
1 parent b6727ea commit f776bdd
Show file tree
Hide file tree
Showing 31 changed files with 580 additions and 377 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ client.on('message', (message) => {
});
}
});

client.login('token');
```

# Features
Expand Down
5 changes: 2 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

export { GuildChannel } from './src/models/channels/GuildChannel.ts';
export { Client } from './src/client/Client.ts';
export { GuildChannel } from "./src/models/channels/GuildChannel.ts";
export { Client } from "./src/client/Client.ts";
37 changes: 21 additions & 16 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import EventEmitter from 'https://deno.land/[email protected]/node/events.ts';
import WebSocketManager from '../client/ws/WebSocket.ts';
import ClientUser from './ClientUser.ts';
import Guild from '../models/Guild.ts';
import RestAPIHandler from '../client/rest/RestAPIHandler.ts';
import Collection from '../models/Collection.ts';
import { BaseChannel } from '../models/channels/BaseChannel.ts';
import { GuildChannel } from '../models/channels/GuildChannel.ts';
import { TextChannel } from '../models/channels/TextChannel.ts';
import Message from '../models/Message.ts';
import User from '../models/User.ts';
import EventEmitter from "https://deno.land/[email protected]/node/events.ts";
import WebSocketManager from "../client/ws/WebSocket.ts";
import ClientUser from "./ClientUser.ts";
import Guild from "../models/Guild.ts";
import RestAPIHandler from "../client/rest/RestAPIHandler.ts";
import Collection from "../models/Collection.ts";
import { BaseChannel } from "../models/channels/BaseChannel.ts";
import { GuildChannel } from "../models/channels/GuildChannel.ts";
import { TextChannel } from "../models/channels/TextChannel.ts";
import Message from "../models/Message.ts";
import User from "../models/User.ts";

interface ClientEvents {
channelCreate: (channel: GuildChannel) => void;
Expand All @@ -25,12 +25,17 @@ interface ClientEvents {
}

export declare interface Client {
on<Event extends keyof ClientEvents>(event: Event, listener: ClientEvents[Event]): this;
emit<Event extends keyof ClientEvents>(event: Event, ...args: Parameters<ClientEvents[Event]>): boolean;
on<Event extends keyof ClientEvents>(
event: Event,
listener: ClientEvents[Event],
): this;
emit<Event extends keyof ClientEvents>(
event: Event,
...args: Parameters<ClientEvents[Event]>
): boolean;
}

export class Client extends EventEmitter {

private _user!: ClientUser;
private _guilds: Collection<string, Guild> = new Collection();
private _channels: Collection<string, BaseChannel> = new Collection();
Expand Down Expand Up @@ -67,10 +72,10 @@ export class Client extends EventEmitter {
get channels(): Collection<string, BaseChannel> {
return this._channels;
}

get users(): Collection<string, User> {
return this._users;
}
}

export default Client;
export default Client;
10 changes: 5 additions & 5 deletions src/client/ClientEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GuildChannel } from '../models/channels/GuildChannel.ts';
import { TextChannel } from '../models/channels/TextChannel.ts';
import Guild from '../models/Guild.ts';
import Message from '../models/Message.ts';
import { GuildChannel } from "../models/channels/GuildChannel.ts";
import { TextChannel } from "../models/channels/TextChannel.ts";
import Guild from "../models/Guild.ts";
import Message from "../models/Message.ts";

export interface Client {
channelCreate: (channel: GuildChannel) => void;
Expand All @@ -14,4 +14,4 @@ export interface Client {
ready: () => void;
resumed: () => void;
message: (message: Message) => void;
}
}
7 changes: 3 additions & 4 deletions src/client/ClientUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default class ClientUser {
private email: string,
private bot: boolean,
private avatar: string,
) {

}
}
) {
}
}
71 changes: 46 additions & 25 deletions src/client/rest/RestAPIHandler.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,88 @@
import Client from '../Client.ts';
import { headers } from '../../constants/Payloads.ts';
import { Constants, ENDPOINTS } from '../../constants/Constants.ts';
import Message from '../../models/Message.ts';
import { MessageOptions } from '../../typedefs/MessageOptions.ts';
import Client from "../Client.ts";
import { headers } from "../../constants/Payloads.ts";
import { Constants, ENDPOINTS } from "../../constants/Constants.ts";
import Message from "../../models/Message.ts";
import { MessageOptions } from "../../typedefs/MessageOptions.ts";

export default class RestAPIHandler {

private _token: string = '';
private _token: string = "";

constructor(private client: Client) {
Object.defineProperty(this, '_token', {
enumerable: false
})
Object.defineProperty(this, "_token", {
enumerable: false,
});
}

async fetchGuilds() {
const response = await fetch(`${Constants.API}/${ENDPOINTS.USER_GUILDS}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.USER_GUILDS}`,
{ headers },
);
return response.json();
}

async fetchGuild(id: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.GUILDS}/${id}?with_counts=true`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.GUILDS}/${id}?with_counts=true`,
{ headers },
);
return response.json();
}

async fetchChannels(id: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.GUILDS}/${id}/${ENDPOINTS.CHANNELS}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.GUILDS}/${id}/${ENDPOINTS.CHANNELS}`,
{ headers },
);
return response.json();
}

async fetchChannel(id: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.CHANNELS}/${id}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.CHANNELS}/${id}`,
{ headers },
);
return response.json();
}

async fetchGuildMember(guildId: string, userId: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.GUILDS}/${guildId}/${ENDPOINTS.MEMBERS}/${userId}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.GUILDS}/${guildId}/${ENDPOINTS.MEMBERS}/${userId}`,
{ headers },
);
return response.json();
}

async fetchGuildMembers(guildId: string, count: number) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.GUILDS}/${guildId}/${ENDPOINTS.MEMBERS}?limit=${count}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.GUILDS}/${guildId}/${ENDPOINTS.MEMBERS}?limit=${count}`,
{ headers },
);
return response.json();
}

async fetchUser(userId: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.USERS}/${userId}`, { headers });
const response = await fetch(
`${Constants.API}/${ENDPOINTS.USERS}/${userId}`,
{ headers },
);
return response.json();
}

async createMessage(options: MessageOptions, id: string) {
const response = await fetch(`${Constants.API}/${ENDPOINTS.CHANNELS}/${id}/${ENDPOINTS.MESSAGES}`, {
method: 'POST',
headers,
body: JSON.stringify(options),
});
const response = await fetch(
`${Constants.API}/${ENDPOINTS.CHANNELS}/${id}/${ENDPOINTS.MESSAGES}`,
{
method: "POST",
headers,
body: JSON.stringify(options),
},
);
return response.json();
}


set token(token: string) {
this._token = token;
headers.Authorization = `Bot ${this._token}`;
}

}
}
18 changes: 9 additions & 9 deletions src/client/ws/WebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import { connectWebSocket, WebSocket } from "https://deno.land/std/ws/mod.ts";
import EventEmitter from 'https://deno.land/[email protected]/node/events.ts';
import { Constants, OPCODE } from '../../constants/Constants.ts';
import { Identify, Heartbeat } from '../../constants/Payloads.ts';
import { Payload } from '../../constants/Payloads.ts';
import EventEmitter from "https://deno.land/[email protected]/node/events.ts";
import { Constants, OPCODE } from "../../constants/Constants.ts";
import { Identify, Heartbeat } from "../../constants/Payloads.ts";
import { Payload } from "../../constants/Payloads.ts";
import Client from "../Client.ts";

export default class WebSocketManager extends EventEmitter {

private interval: number = 0;
private socket!: WebSocket;
private ackReceived: boolean = false;
Expand All @@ -32,12 +30,14 @@ export default class WebSocketManager extends EventEmitter {
this.ackReceived = true;
break;
case OPCODE.NINE:
console.log('Invalid gateway session');
console.log("Invalid gateway session");
break;
}
if (event) {
try {
const { default: module } = await import(`../../handlers/${event}.ts`);
const { default: module } = await import(
`../../handlers/${event}.ts`
);
module(this.client, payload);
} catch (err) {
// console.log(err);
Expand All @@ -60,4 +60,4 @@ export default class WebSocketManager extends EventEmitter {
Identify.d.token = token;
this.socket.send(JSON.stringify(Identify));
}
}
}
16 changes: 8 additions & 8 deletions src/constants/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum Constants {
GATEWAY = 'wss://gateway.discord.gg/?v=6&encoding=json',
API = 'https://discord.com/api/v6',
GATEWAY = "wss://gateway.discord.gg/?v=6&encoding=json",
API = "https://discord.com/api/v6",
}

export enum OPCODE {
Expand All @@ -20,10 +20,10 @@ export enum OPCODE {
}

export enum ENDPOINTS {
USERS = 'users',
USER_GUILDS = 'users/@me/guilds',
GUILDS = 'guilds',
CHANNELS = 'channels',
MESSAGES = 'messages',
MEMBERS = 'members',
USERS = "users",
USER_GUILDS = "users/@me/guilds",
GUILDS = "guilds",
CHANNELS = "channels",
MESSAGES = "messages",
MEMBERS = "members",
}
78 changes: 39 additions & 39 deletions src/constants/Events.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
export enum Events {
READY = 'ready',
RESUMED = 'resumed',
RECONNECT = 'reconnect',
INVALID_SESSION = 'invalid',
CHANNEL_CREATE = 'channelCreate',
CHANNEL_UPDATE = 'channelUpdate',
CHANNEL_DELETE = 'channelDelete',
CHANNEL_PINS_UPDATE = 'channelPinsUpdate',
GUILD_CREATE = 'guildCreate',
GUILD_UPDATE = 'guildUpdate',
GUILD_DELETE = 'guildDelete',
GUILD_BAN_ADD = 'guildBanAdd',
GUILD_BAN_REMOVE = 'guildBanRemove',
GUILD_EMOJIS_UPDATE = 'guildEmojisUpdate',
GUILD_INTEGRATIONS_UPDATE = 'guildIntegrationsUpdate',
GUILD_MEMBER_ADD = 'guildMemberAdd',
GUILD_MEMBER_REMOVE = 'guildMemberRemove',
GUILD_MEMBER_UPDATE = 'guildMemberUpdate',
GUILD_MEMBERS_CHUNK = 'guildMemberAdd',
GUILD_ROLE_CREATE = 'guildRoleCreate',
GUILD_ROLE_UPDATE = 'guildRoleUpdate',
GUILD_ROLE_DELETE = 'guildRoleDelete',
INVITE_CREATE = 'inviteCreate',
INVITE_DELETE = 'inviteDelete',
MESSAGE_CREATE = 'message',
MESSAGE_UPDATE = 'messageUpdate',
MESSAGE_DELETE = 'messageDelete',
MESSAGE_DELETE_BULK = 'messageDeleteBulk',
MESSAGE_REACTION_ADD = 'messageReactionAdd',
MESSAGE_REACTION_REMOVE = 'messageReactionRemove',
MESSAGE_REACTION_REMOVE_ALL = 'messageReactionRemoveAll',
MESSAGE_REACTION_REMOVE_EMOJI = 'messageReactionRemoveEmoji',
PRESENCE_UPDATE = 'presenceUpdate',
TYPING_START = 'typingStart',
USER_UPDATE = 'userUpdate',
VOICE_STATE_UPDATE = 'voiceStateUpdate',
VOICE_SERVER_UPDATE = 'voiceStateUpdate',
WEBHOOKS_UPDATE = 'webhooksUpdate',
}
READY = "ready",
RESUMED = "resumed",
RECONNECT = "reconnect",
INVALID_SESSION = "invalid",
CHANNEL_CREATE = "channelCreate",
CHANNEL_UPDATE = "channelUpdate",
CHANNEL_DELETE = "channelDelete",
CHANNEL_PINS_UPDATE = "channelPinsUpdate",
GUILD_CREATE = "guildCreate",
GUILD_UPDATE = "guildUpdate",
GUILD_DELETE = "guildDelete",
GUILD_BAN_ADD = "guildBanAdd",
GUILD_BAN_REMOVE = "guildBanRemove",
GUILD_EMOJIS_UPDATE = "guildEmojisUpdate",
GUILD_INTEGRATIONS_UPDATE = "guildIntegrationsUpdate",
GUILD_MEMBER_ADD = "guildMemberAdd",
GUILD_MEMBER_REMOVE = "guildMemberRemove",
GUILD_MEMBER_UPDATE = "guildMemberUpdate",
GUILD_MEMBERS_CHUNK = "guildMemberAdd",
GUILD_ROLE_CREATE = "guildRoleCreate",
GUILD_ROLE_UPDATE = "guildRoleUpdate",
GUILD_ROLE_DELETE = "guildRoleDelete",
INVITE_CREATE = "inviteCreate",
INVITE_DELETE = "inviteDelete",
MESSAGE_CREATE = "message",
MESSAGE_UPDATE = "messageUpdate",
MESSAGE_DELETE = "messageDelete",
MESSAGE_DELETE_BULK = "messageDeleteBulk",
MESSAGE_REACTION_ADD = "messageReactionAdd",
MESSAGE_REACTION_REMOVE = "messageReactionRemove",
MESSAGE_REACTION_REMOVE_ALL = "messageReactionRemoveAll",
MESSAGE_REACTION_REMOVE_EMOJI = "messageReactionRemoveEmoji",
PRESENCE_UPDATE = "presenceUpdate",
TYPING_START = "typingStart",
USER_UPDATE = "userUpdate",
VOICE_STATE_UPDATE = "voiceStateUpdate",
VOICE_SERVER_UPDATE = "voiceStateUpdate",
WEBHOOKS_UPDATE = "webhooksUpdate",
}
Loading

0 comments on commit f776bdd

Please sign in to comment.