-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa1de8d
commit b09e6c5
Showing
12 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import type { Guild } from "../structures/guilds/guild.ts"; | ||
import type { Message } from "../structures/mod.ts"; | ||
|
||
// ...this is gonna be very painful to fix | ||
export type ClientEvents = { | ||
messageCreate: [Message]; | ||
ready: []; | ||
guildCreate: [Guild]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GatewayHandler } from "../../types/mod.ts"; | ||
|
||
const guildCreate: GatewayHandler<"GUILD_CREATE"> = async ( | ||
client, | ||
[_, guild], | ||
) => { | ||
client.guilds.set(guild.id, guild); | ||
|
||
const guildObj = client.guilds.get(guild.id)!; | ||
await guildObj.channels.fetchAll(); | ||
|
||
client.emit("guildCreate", guildObj); | ||
}; | ||
|
||
export default guildCreate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import type { GatewayHandler } from "../../types/gateway.ts"; | ||
import guildCreate from "./guildCreate.ts"; | ||
import messageCreate from "./messageCreate.ts"; | ||
import ready from "./ready.ts"; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
export const GatewayHandlers: { [K: string]: GatewayHandler<any> } = { | ||
MESSAGE_CREATE: messageCreate, | ||
READY: ready, | ||
GUILD_CREATE: guildCreate, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
ChannelType, | ||
type GuildChannelPayload, | ||
type GuildTextChannelPayload, | ||
} from "../../../types/mod.ts"; | ||
import type { Client } from "../client/mod.ts"; | ||
import { GuildChannel } from "../structures/channels/guildChannel.ts"; | ||
import { GuildTextChannel } from "../structures/channels/guildTextChannel.ts"; | ||
import { BaseManager } from "./base.ts"; | ||
import { BaseChildManager } from "./baseChild.ts"; | ||
import type { ChannelsManager } from "./channels.ts"; | ||
|
||
export class GuildChannelsManager | ||
extends BaseChildManager<GuildChannelPayload, GuildChannel> { | ||
guildID: string; | ||
|
||
constructor(client: Client, parent: ChannelsManager, guildID: string) { | ||
super( | ||
client, | ||
parent as unknown as BaseManager<GuildChannelPayload, GuildChannel>, | ||
); | ||
this.guildID = guildID; | ||
} | ||
|
||
private _createChannel<P extends GuildChannelPayload>(payload: P) { | ||
switch (payload.type) { | ||
case ChannelType.GUILD_TEXT: | ||
return new GuildTextChannel( | ||
this.client, | ||
payload as unknown as GuildTextChannelPayload, | ||
); | ||
default: | ||
return new GuildChannel(this.client, payload); | ||
} | ||
} | ||
|
||
_fill(payloads: GuildChannelPayload[]) { | ||
for (const payload of payloads) { | ||
this.set(payload.id!, payload); | ||
} | ||
} | ||
|
||
_get(key: string): GuildChannelPayload | undefined { | ||
const channel = this.parent._get(key); | ||
if (!channel) return; | ||
if (channel.guild_id !== this.guildID) return; | ||
return channel; | ||
} | ||
|
||
async _fetchAll(): Promise<GuildChannelPayload[] | undefined> { | ||
try { | ||
const resp: GuildChannelPayload[] | undefined = await this.client.rest | ||
.get( | ||
`/guilds/${this.guildID}/channels`, | ||
); | ||
if (!resp) return; | ||
const channels = resp; | ||
this._fill(channels); | ||
return channels; | ||
} catch (_err) { | ||
return; | ||
} | ||
} | ||
|
||
async _fetch(id: string): Promise<GuildChannelPayload | undefined> { | ||
try { | ||
const resp: GuildChannelPayload | undefined = await this.client.rest | ||
.get( | ||
`/channels/${id}`, | ||
); | ||
if (!resp) return; | ||
this.set(id, resp); | ||
return resp; | ||
} catch (_err) { | ||
return; | ||
} | ||
} | ||
|
||
get( | ||
id: string, | ||
) { | ||
const cached = this._get(id); | ||
if (!cached) return; | ||
return this._createChannel(cached); | ||
} | ||
|
||
async fetchAll() { | ||
try { | ||
const channels = await this._fetchAll(); | ||
if (!channels) return; | ||
return channels.map((r) => this._createChannel(r)); | ||
} catch (_err) { | ||
return; | ||
} | ||
} | ||
|
||
async fetch( | ||
id: string, | ||
) { | ||
try { | ||
const payload = await this._fetch(id); | ||
if (!payload) return; | ||
return this._createChannel(payload); | ||
} catch (_err) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters