Skip to content

Commit

Permalink
Parsed Guilds into Map for Client.guilds property
Browse files Browse the repository at this point in the history
  • Loading branch information
stuyy committed May 18, 2020
1 parent 4f50272 commit 7f50571
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 77 deletions.
30 changes: 0 additions & 30 deletions .denon.json

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.denon.json
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# What is Katana?

Katana is an unofficial Discord library that allows you to build Discord Bots.

# Usage

```TS
import { Client } from 'https://deno.land/x/katana/mod.ts'

const client = new Client();

client.on('ready', () => {
console.log('Bot has logged in!');
});
```

# Features

# Contributing

2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Client } from './src/client/Client.ts';

27 changes: 26 additions & 1 deletion src/client/Client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import EventEmitter from 'https://deno.land/[email protected]/node/events.ts';
import WebSocketManager from '../ws/Websocket.ts';
import ClientUser from './ClientUser.ts';
import Guild from '../models/Guild.ts';

export default class Client extends EventEmitter {
export class Client extends EventEmitter {

private _user!: ClientUser;
private _token!: string;
private _guilds: Map<string, Guild> = new Map();

private socket: WebSocketManager = new WebSocketManager(this);
async login(token: string): Promise<void> {
try {
this._token = token;
await this.socket.connect(token);
console.log(this.token);
} catch (err) {
console.log(err);
}
}

get user() {
return this._user;
}

set user(user: ClientUser) {
this._user = user;
}

get token() {
return this._token;
}

get guilds() {
return this._guilds;
}

}
2 changes: 1 addition & 1 deletion src/client/ClientUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default class ClientUser {
private bot: boolean,
private avatar: string,
) {

}
}
7 changes: 7 additions & 0 deletions src/constants/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum Constants {
GATEWAY = 'wss://gateway.discord.gg/?v=6&encoding=json',
API = 'https://discord.com/api/v6',
}

export enum OPCODE {
Expand All @@ -17,3 +18,9 @@ export enum OPCODE {
ELEVEN = 11,
TWELVE = 12,
}

export enum ENDPOINTS {
USERS = 'users',
USER_GUILDS = 'users/@me/guilds',
GUILDS = 'guilds'
}
76 changes: 38 additions & 38 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_SESSION',
CHANNEL_CREATE = 'CHANNEL_CREATE',
CHANNEL_UPDATE = 'CHANNEL_UPDATE',
CHANNEL_DELETE = 'CHANNEL_DELETE',
CHANNEL_PINS_UPDATE = 'CHANNEL_PINS_UPDATE',
GUILD_CREATE = 'GUILD_CREATE',
GUILD_UPDATE = 'GUILD_UPDATE',
GUILD_DELETE = 'GUILD_DELETE',
GUILD_BAN_ADD = 'GUILD_BAN_ADD',
GUILD_BAN_REMOVE = 'GUILD_BAN_REMOVE',
GUILD_EMOJIS_UPDATE = 'GUILD_EMOJIS_UPDATE',
GUILD_INTEGRATIONS_UPDATE = 'GUILD_INTEGRATIONS_UPDATE',
GUILD_MEMBER_ADD = 'GUILD_MEMBER_ADD',
GUILD_MEMBER_REMOVE = 'GUILD_MEMBER_REMOVE',
GUILD_MEMBER_UPDATE = 'GUILD_MEMBER_UPDATE',
GUILD_MEMBERS_CHUNK = 'GUILD_MEMBERS_CHUNK',
GUILD_ROLE_CREATE = 'GUILD_ROLE_CREATE',
GUILD_ROLE_UPDATE = 'GUILD_ROLE_UPDATE',
GUILD_ROLE_DELETE = 'GUILD_ROLE_DELETE',
INVITE_CREATE = 'INVITE_CREATE',
INVITE_DELETE = 'INVITE_DELETE',
MESSAGE_CREATE = 'MESSAGE_CREATE',
MESSAGE_UPDATE = 'MESSAGE_UPDATE',
MESSAGE_DELETE = 'MESSAGE_DELETE',
MESSAGE_DELETE_BULK = 'MESSAGE_DELETE_BULK',
MESSAGE_REACTION_ADD = 'MESSAGE_REACTION_ADD',
MESSAGE_REACTION_REMOVE = 'MESSAGE_REACTION_REMOVE',
MESSAGE_REACTION_REMOVE_ALL = 'MESSAGE_REACTION_REMOVE_ALL',
MESSAGE_REACTION_REMOVE_EMOJI = 'MESSAGE_REACTION_REMOVE_EMOJI',
PRESENCE_UPDATE = 'PRESENCE_UPDATE',
TYPING_START = 'TYPING_START',
USER_UPDATE = 'USER_UPDATE',
VOICE_STATE_UPDATE = 'VOICE_STATE_UPDATE',
VOICE_SERVER_UPDATE = 'VOICE_SERVER_UPDATE',
WEBHOOKS_UPDATE = 'WEBHOOKS_UPDATE',
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 = 'messageCreate',
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',
}
5 changes: 5 additions & 0 deletions src/constants/Payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export const Identify = {
}
}
}

export const headers = {
'Content-Type' : 'application/json',
'Authorization' : '',
}
1 change: 1 addition & 0 deletions src/handlers/GUILD_CREATE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { Payload } from "../interfaces/Payload.ts";
import { Events } from '../constants/Events.ts';

export default async function (client: Client, payload: Payload) {
console.log('Guild Created');
client.emit(Events.GUILD_CREATE);
}
7 changes: 7 additions & 0 deletions src/handlers/MESSAGE_CREATE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Client from "../client/Client.ts";
import { Payload } from "../interfaces/Payload.ts";
import { Events } from '../constants/Events.ts';

export default function(client: Client, payload: Payload) {
client.emit(Events.MESSAGE_CREATE);
}
78 changes: 78 additions & 0 deletions src/handlers/READY.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
import Client from "../client/Client.ts";
import { Payload } from "../interfaces/Payload.ts";
import { Events } from '../constants/Events.ts';
import RestAPIHandler from '../rest/RestAPIHandler.ts';
import ClientUser from "../client/ClientUser.ts";
import Guild from "../models/Guild.ts";
import Role from "../models/Role.ts";

export default async function (client: Client, payload: Payload) {

const { user, guilds } = payload.d;

client.user = new ClientUser(
user.username,
user.discriminator,
user.verified,
user.id,
user.flags,
user.email,
user.bot,
user.avatar
);

const now = performance.now();

for (const g of guilds) {
const guild: any = await RestAPIHandler.fetchGuild(client.token, g.id);
const rolesArray = guild.roles;
const roles = new Map();
for (const role of rolesArray) {
roles.set(role.id, new Role(
role.id,
role.name,
role.color,
role.hoist,
role.position,
role.permissions,
role.managed,
role.mentionable
));
}

const newGuild = new Guild(
guild.id,
guild.name,
guild.icon,
guild.description,
guild.splash,
guild.discovery_splash,
guild.features,
guild.emojis,
guild.banner,
guild.owner_id,
guild.application_id,
guild.region,
guild.afk_channel_id,
guild.afk_timeout,
guild.system_channel_id,
guild.widget_enabled,
guild.widget_channel_id,
guild.verification_level,
roles,
guild.default_message_notifications,
guild.mfa_level,
guild.explicit_content_filter,
guild.max_presences,
guild.max_members,
guild.max_video_channel_users,
guild.vanity_url_code,
guild.premium_tier,
guild.premium_subscription_count,
guild.system_channel_flags,
guild.preferred_locale,
guild.rules_channel_id,
guild.public_updates_channel_id,
guild.embed_enabled,
guild.embed_channel_id,
);
client.guilds.set(newGuild.id, newGuild);
}
const end = performance.now();

console.log(`Duration: ${end-now}ms`)
client.emit(Events.READY);
}
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Client from '../src/client/Client.ts';
import EventQueue from './structures/EventQueue.ts';
import { Client } from '../src/client/Client.ts';
import "https://deno.land/x/dotenv/load.ts";

const client = new Client();
client.login(Deno.env.get('BOT_TOKEN')!.toString());

client.login('token');
client.on('ready', () => {
console.log('Bot has logged in.');
});
File renamed without changes.
51 changes: 51 additions & 0 deletions src/models/Guild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Role from "./Role.ts";

export default class Guild {

constructor(
private _id: string,
private name: string,
private icon: string,
private description: string,
private splash: string,
private discoverySplash: string,
private features: Array<any>,
private emojis: Array<any>,
private banner: string,
private ownerId: string,
private applicationId: string,
private region: string,
private afkChannelId: string,
private afkTimeout: string,
private systemChannelId: string,
private widgetEnabled: boolean,
private widgetChannelId: string,
private verificationLevel: number,
private _roles: Map<string, Role> = new Map(),
private defaultMessageNotifications: number,
private mfaLevel: number,
private explicitContentFilter: number,
private maxPresences: number,
private maxMembers: number,
private maxVideoChannelUsers: number,
private vanityUrl: string,
private premiumTier: number,
private premiumSubscriptionCount: number,
private systemChannelFlags: number,
private preferredLocale: string,
private rulesChannelId: string,
private publicUpdatesChannelId: string,
private embedEnabled: boolean,
private embedChannelId: string
) {

}

public get id(): string {
return this._id;
}

public get roles(): Map<string, Role> {
return this._roles;
}
}
Loading

0 comments on commit 7f50571

Please sign in to comment.