Skip to content

Commit

Permalink
feat: standardize manager method names
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Jan 8, 2023
1 parent 45829a1 commit 6dddd7c
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 243 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-rats-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"guilded.js": minor
---

feat: standardize manager method names
83 changes: 43 additions & 40 deletions packages/guilded.js/lib/managers/global/ChannelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,52 @@ import { CacheableStructManager } from "./CacheableStructManager";
import type { ChannelType as APIChannelType } from "@guildedjs/guilded-api-typings";

export class GlobalChannelManager extends CacheableStructManager<string, Channel> {
get shouldCacheChannel() {
return this.client.options?.cache?.cacheChannels !== false;
}

create(options: RESTPostChannelsBody): Promise<Channel> {
return this.client.rest.router.createChannel(options).then((data) => {
const newChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
return newChannel;
});
}
fetch(channelId: string, force?: boolean): Promise<Channel> {
if (!force) {
const existingChannel = this.client.channels.cache.get(channelId);
if (existingChannel) return Promise.resolve(existingChannel);
}
return this.client.rest.router.getChannel(channelId).then((data) => {
const fetchedChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
if (this.shouldCacheChannel) this.cache.set(fetchedChannel.id, fetchedChannel);
return fetchedChannel;
});
}
update(channelId: string, options: RESTPatchChannelBody): Promise<Channel> {
return this.client.rest.router.updateChannel(channelId, options).then((data) => {
const existingChannel = this.cache.get(channelId);
if (existingChannel) return existingChannel._update(data.channel);

const newChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
if (this.shouldCacheChannel) this.cache.set(newChannel.id, newChannel);
return newChannel;
});
}
delete(channelId: string): Promise<Channel | void> {
return this.client.rest.router.deleteChannel(channelId).then((data) => {
const cachedChannel = this.cache.get(channelId);
return cachedChannel ?? void 0;
});
}
get shouldCacheChannel() {
return this.client.options?.cache?.cacheChannels !== false;
}

create(options: RESTPostChannelsBody): Promise<Channel> {
return this.client.rest.router.createChannel(options).then((data) => {
const newChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
return newChannel;
});
}

fetch(channelId: string, force?: boolean): Promise<Channel> {
if (!force) {
const existingChannel = this.client.channels.cache.get(channelId);
if (existingChannel) return Promise.resolve(existingChannel);
}
return this.client.rest.router.getChannel(channelId).then((data) => {
const fetchedChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
if (this.shouldCacheChannel) this.cache.set(fetchedChannel.id, fetchedChannel);
return fetchedChannel;
});
}

update(channelId: string, options: RESTPatchChannelBody): Promise<Channel> {
return this.client.rest.router.updateChannel(channelId, options).then((data) => {
const existingChannel = this.cache.get(channelId);
if (existingChannel) return existingChannel._update(data.channel);

const newChannel = new (transformTypeToChannel(data.channel.type))(this.client, data.channel);
if (this.shouldCacheChannel) this.cache.set(newChannel.id, newChannel);
return newChannel;
});
}

delete(channelId: string): Promise<Channel | void> {
return this.client.rest.router.deleteChannel(channelId).then((data) => {
const cachedChannel = this.cache.get(channelId);
return cachedChannel ?? void 0;
});
}
}

export const transformTypeToChannel = (str: APIChannelType) => typeToChannel[str as "forums" | "docs" | "list"] ?? Channel;

export const typeToChannel = {
forums: ForumChannel,
docs: DocChannel,
list: ListChannel,
forums: ForumChannel,
docs: DocChannel,
list: ListChannel,
};
138 changes: 69 additions & 69 deletions packages/guilded.js/lib/managers/global/MessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,82 @@ import type { MessageContent } from "../../typings";
import { CollectorOptions, MessageCollector } from "../../structures";

export class GlobalMessageManager extends CacheableStructManager<string, Message> {
get shouldCacheMessage() {
return this.client.options.cache?.cacheMessages !== false;
}
get shouldCacheMessage() {
return this.client.options.cache?.cacheMessages !== false;
}

/** Get a list of the latest 50 messages from a channel. */
fetchMany(channelId: string, options: RESTGetChannelMessagesQuery): Promise<Collection<string, Message>> {
return this.client.rest.router.getChannelMessages(channelId, options).then((data) => {
const messages = new Collection<string, Message>();
for (const message of data.messages) {
const newMessage = new Message(this.client, message);
messages.set(newMessage.id, newMessage);
}
return messages;
});
}
/** Get a list of the latest 50 messages from a channel. */
fetchMany(channelId: string, options: RESTGetChannelMessagesQuery): Promise<Collection<string, Message>> {
return this.client.rest.router.getChannelMessages(channelId, options).then((data) => {
const messages = new Collection<string, Message>();
for (const message of data.messages) {
const newMessage = new Message(this.client, message);
messages.set(newMessage.id, newMessage);
}
return messages;
});
}

/** Get details for a specific chat message from a chat channel. */
fetch(channelId: string, messageId: string, force?: boolean): Promise<Message> {
if (!force) {
const existingMessage = this.client.messages.cache.get(messageId);
if (existingMessage) return Promise.resolve(existingMessage);
}
return this.client.rest.router.getChannelMessage(channelId, messageId).then((data) => {
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}
/** Get details for a specific chat message from a chat channel. */
fetch(channelId: string, messageId: string, force?: boolean): Promise<Message> {
if (!force) {
const existingMessage = this.client.messages.cache.get(messageId);
if (existingMessage) return Promise.resolve(existingMessage);
}
return this.client.rest.router.getChannelMessage(channelId, messageId).then((data) => {
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}

/** Send a message in a channel */
send(channelId: string, content: MessageContent): Promise<Message> {
return this.client.rest.router.createChannelMessage(channelId, resolveContentToData(content)).then((data) => {
// This is in the case of which the WS gateway beats us to adding the message to the cache. If they haven't, then we do it ourselves.
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage;
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}
/** Send a message in a channel */
send(channelId: string, content: MessageContent): Promise<Message> {
return this.client.rest.router.createChannelMessage(channelId, resolveContentToData(content)).then((data) => {
// This is in the case of which the WS gateway beats us to adding the message to the cache. If they haven't, then we do it ourselves.
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage;
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}

/** Add a reaction emote */
addReaction(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.rest.router.addReactionEmote(channelId, contentId, emoteId).then(() => void 0);
}
/** Add a reaction emote */
addReaction(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.reactions.create(channelId, contentId, emoteId)
}

/** Delete a reaction emote */
deleteReaction(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.rest.router.deleteReactionEmote(channelId, contentId, emoteId).then(() => void 0);
}
/** Delete a reaction emote */
deleteReaction(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.reactions.delete(channelId, contentId, emoteId)
}

/** Update a channel message. */
update(channelId: string, messageId: string, content: RESTPostChannelMessagesBody | Embed | string): Promise<Message> {
return this.client.rest.router.updateChannelMessage(channelId, messageId, resolveContentToData(content)).then((data) => {
// This is in the case of which the WS gateway beats us to modifying the message in the cache. If they haven't, then we do it ourselves.
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage._update(data.message);
/** Update a channel message. */
update(channelId: string, messageId: string, content: RESTPostChannelMessagesBody | Embed | string): Promise<Message> {
return this.client.rest.router.updateChannelMessage(channelId, messageId, resolveContentToData(content)).then((data) => {
// This is in the case of which the WS gateway beats us to modifying the message in the cache. If they haven't, then we do it ourselves.
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage._update(data.message);

const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
});
}

/** Delete a channel message. */
delete(channelId: string, messageId: string): Promise<void> {
return this.client.rest.router.deleteChannelMessage(channelId, messageId).then(() => void 0);
}
/** Delete a channel message. */
delete(channelId: string, messageId: string): Promise<void> {
return this.client.rest.router.deleteChannelMessage(channelId, messageId).then(() => void 0);
}

async awaitMessages(channelId: string, options: CollectorOptions<Message>) {
return new MessageCollector(this.client, {
...options,
filter: (item) => {
if (item.channelId !== channelId) return false;
return options.filter?.(item) ?? true;
},
}).start();
}
async awaitMessages(channelId: string, options: CollectorOptions<Message>) {
return new MessageCollector(this.client, {
...options,
filter: (item) => {
if (item.channelId !== channelId) return false;
return options.filter?.(item) ?? true;
},
}).start();
}
}
16 changes: 13 additions & 3 deletions packages/guilded.js/lib/managers/global/ReactionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import type { EmotePayload } from "@guildedjs/guilded-api-typings";
import type { MessageReaction } from "../../structures";

export class GlobalReactionManager extends CacheableStructManager<string, MessageReaction> {
get shouldCacheReaction() {
return this.client.options.cache?.cacheMessageReactions !== false;
}
get shouldCacheReaction() {
return this.client.options.cache?.cacheMessageReactions !== false;
}

/** Add a reaction emote */
create(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.rest.router.addReactionEmote(channelId, contentId, emoteId).then(() => void 0);
}

/** Delete a reaction emote */
delete(channelId: string, contentId: string, emoteId: number): Promise<void> {
return this.client.rest.router.deleteReactionEmote(channelId, contentId, emoteId).then(() => void 0);
}
}
96 changes: 48 additions & 48 deletions packages/guilded.js/lib/managers/global/WebhookManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,57 @@ import { Webhook } from "../../structures/Webhook";
import { CacheableStructManager } from "./CacheableStructManager";

export class GlobalWebhookManager extends CacheableStructManager<string, Webhook> {
get _shouldCacheWebhook() {
return this.client.options.cache?.cacheWebhooks !== false;
}
get _shouldCacheWebhook() {
return this.client.options.cache?.cacheWebhooks !== false;
}

/** Create a webhook */
createWebhook(serverId: string, options: RESTPostServerWebhooksBody): Promise<Webhook> {
return this.client.rest.router.createWebhook(serverId, options).then((data) => {
// This is in the case of which the WS gateway beats us to adding the message to the cache. If they haven't, then we do it ourselves.
const existingWebhook = this.client.webhooks.cache.get(data.webhook.id);
if (existingWebhook) return existingWebhook;
const newWebhook = new Webhook(this.client, data.webhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
return newWebhook;
});
}
/** Create a webhook */
create(serverId: string, options: RESTPostServerWebhooksBody): Promise<Webhook> {
return this.client.rest.router.createWebhook(serverId, options).then((data) => {
// This is in the case of which the WS gateway beats us to adding the message to the cache. If they haven't, then we do it ourselves.
const existingWebhook = this.client.webhooks.cache.get(data.webhook.id);
if (existingWebhook) return existingWebhook;
const newWebhook = new Webhook(this.client, data.webhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
return newWebhook;
});
}

/** Get a server's webhooks */
getWebhooks(serverId: string, channelId?: string): Promise<Collection<string, Webhook>> {
return this.client.rest.router.getWebhooks(serverId, channelId).then((data) => {
const webhooks = new Collection<string, Webhook>();
for (const webhook of data.webhooks) {
const newWebhook = new Webhook(this.client, webhook);
webhooks.set(newWebhook.id, newWebhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
}
return webhooks;
});
}
/** Get a server's webhooks */
fetchMany(serverId: string, channelId?: string): Promise<Collection<string, Webhook>> {
return this.client.rest.router.getWebhooks(serverId, channelId).then((data) => {
const webhooks = new Collection<string, Webhook>();
for (const webhook of data.webhooks) {
const newWebhook = new Webhook(this.client, webhook);
webhooks.set(newWebhook.id, newWebhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
}
return webhooks;
});
}

/** Get a webhook */
getWebhook(serverId: string, webhookId: string, force?: boolean): Promise<Webhook> {
if (!force) {
const existingWebhook = this.client.webhooks.cache.get(webhookId);
if (existingWebhook) return Promise.resolve(existingWebhook);
}
return this.client.rest.router.getWebhook(serverId, webhookId).then((data) => {
const newWebhook = new Webhook(this.client, data.webhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
return newWebhook;
});
}
/** Get a webhook */
fetch(serverId: string, webhookId: string, force?: boolean): Promise<Webhook> {
if (!force) {
const existingWebhook = this.client.webhooks.cache.get(webhookId);
if (existingWebhook) return Promise.resolve(existingWebhook);
}
return this.client.rest.router.getWebhook(serverId, webhookId).then((data) => {
const newWebhook = new Webhook(this.client, data.webhook);
if (this._shouldCacheWebhook) this.cache.set(newWebhook.id, newWebhook);
return newWebhook;
});
}

/** Update a webhook */
updateWebhook(serverId: string, webhookId: string, options: RESTPutServerWebhookBody): Promise<Webhook> {
return this.client.rest.router.updateWebhook(serverId, webhookId, options).then((data) => {
return this.cache.get(data.webhook.id)?._update(data.webhook) ?? new Webhook(this.client, data.webhook);
});
}
/** Update a webhook */
update(serverId: string, webhookId: string, options: RESTPutServerWebhookBody): Promise<Webhook> {
return this.client.rest.router.updateWebhook(serverId, webhookId, options).then((data) => {
return this.cache.get(data.webhook.id)?._update(data.webhook) ?? new Webhook(this.client, data.webhook);
});
}

/** Delete a webhook */
deleteWebhook(serverId: string, webhookId: string): Promise<Webhook | null> {
return this.client.rest.router.deleteWebhook(serverId, webhookId);
}
/** Delete a webhook */
delete(serverId: string, webhookId: string): Promise<Webhook | null> {
return this.client.rest.router.deleteWebhook(serverId, webhookId);
}
}
Loading

0 comments on commit 6dddd7c

Please sign in to comment.