mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
allow structures to be overriden
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import { cache } from "../utils/cache.ts";
|
import { cache } from "../utils/cache.ts";
|
||||||
import { ChannelCreatePayload, ChannelTypes } from "../types/channel.ts";
|
import { ChannelCreatePayload, ChannelTypes } from "../types/channel.ts";
|
||||||
import { createChannel } from "../structures/channel.ts";
|
|
||||||
import { eventHandlers } from "../module/client.ts";
|
import { eventHandlers } from "../module/client.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
export const handleInternalChannelCreate = (data: ChannelCreatePayload) => {
|
export const handleInternalChannelCreate = (data: ChannelCreatePayload) => {
|
||||||
const channel = createChannel(data);
|
const channel = structures.createChannel(data);
|
||||||
cache.channels.set(channel.id, channel);
|
cache.channels.set(channel.id, channel);
|
||||||
if (channel.guildID) {
|
if (channel.guildID) {
|
||||||
const guild = cache.guilds.get(channel.guildID);
|
const guild = cache.guilds.get(channel.guildID);
|
||||||
@@ -15,7 +15,7 @@ export const handleInternalChannelCreate = (data: ChannelCreatePayload) => {
|
|||||||
|
|
||||||
export const handleInternalChannelUpdate = (data: ChannelCreatePayload) => {
|
export const handleInternalChannelUpdate = (data: ChannelCreatePayload) => {
|
||||||
const cachedChannel = cache.channels.get(data.id);
|
const cachedChannel = cache.channels.get(data.id);
|
||||||
const channel = createChannel(data);
|
const channel = structures.createChannel(data);
|
||||||
cache.channels.set(channel.id, channel);
|
cache.channels.set(channel.id, channel);
|
||||||
if (!cachedChannel) return;
|
if (!cachedChannel) return;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { Errors } from "../types/errors.ts";
|
|||||||
import { RequestManager } from "../module/requestManager.ts";
|
import { RequestManager } from "../module/requestManager.ts";
|
||||||
import { endpoints } from "../constants/discord.ts";
|
import { endpoints } from "../constants/discord.ts";
|
||||||
import { MessageCreateOptions } from "../types/message.ts";
|
import { MessageCreateOptions } from "../types/message.ts";
|
||||||
import { createMessage } from "../structures/message.ts";
|
|
||||||
import {
|
import {
|
||||||
GetMessagesAfter,
|
GetMessagesAfter,
|
||||||
GetMessagesBefore,
|
GetMessagesBefore,
|
||||||
@@ -20,6 +19,7 @@ import {
|
|||||||
FollowedChannelPayload,
|
FollowedChannelPayload,
|
||||||
} from "../types/channel.ts";
|
} from "../types/channel.ts";
|
||||||
import { logYellow } from "../utils/logger.ts";
|
import { logYellow } from "../utils/logger.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
/** Checks if a user id or a role id has permission in this channel */
|
/** Checks if a user id or a role id has permission in this channel */
|
||||||
export function hasChannelPermission(
|
export function hasChannelPermission(
|
||||||
@@ -63,7 +63,7 @@ export async function getMessage(channel: Channel, id: string) {
|
|||||||
const result = await RequestManager.get(
|
const result = await RequestManager.get(
|
||||||
endpoints.CHANNEL_MESSAGE(channel.id, id),
|
endpoints.CHANNEL_MESSAGE(channel.id, id),
|
||||||
) as MessageCreateOptions;
|
) as MessageCreateOptions;
|
||||||
return createMessage(result);
|
return structures.createMessage(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
||||||
@@ -97,7 +97,7 @@ export async function getMessages(
|
|||||||
endpoints.CHANNEL_MESSAGES(channel.id),
|
endpoints.CHANNEL_MESSAGES(channel.id),
|
||||||
options,
|
options,
|
||||||
)) as MessageCreateOptions[];
|
)) as MessageCreateOptions[];
|
||||||
return result.map((res) => createMessage(res));
|
return result.map((res) => structures.createMessage(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get pinned messages in this channel. */
|
/** Get pinned messages in this channel. */
|
||||||
@@ -105,7 +105,7 @@ export async function getPins(channelID: string) {
|
|||||||
const result = (await RequestManager.get(
|
const result = (await RequestManager.get(
|
||||||
endpoints.CHANNEL_PINS(channelID),
|
endpoints.CHANNEL_PINS(channelID),
|
||||||
)) as MessageCreateOptions[];
|
)) as MessageCreateOptions[];
|
||||||
return result.map((res) => createMessage(res));
|
return result.map((res) => structures.createMessage(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
||||||
@@ -177,7 +177,7 @@ export async function sendMessage(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return createMessage(result as MessageCreateOptions);
|
return structures.createMessage(result as MessageCreateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
|
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
|
||||||
|
|||||||
+7
-12
@@ -1,13 +1,8 @@
|
|||||||
import { Guild } from "../structures/guild.ts";
|
import { Guild } from "../structures/guild.ts";
|
||||||
import { createChannel } from "../structures/channel.ts";
|
|
||||||
|
|
||||||
import { formatImageURL } from "../utils/cdn.ts";
|
import { formatImageURL } from "../utils/cdn.ts";
|
||||||
import { botHasPermission } from "../utils/permissions.ts";
|
import { botHasPermission } from "../utils/permissions.ts";
|
||||||
|
|
||||||
import { RequestManager } from "../module/requestManager.ts";
|
import { RequestManager } from "../module/requestManager.ts";
|
||||||
|
|
||||||
import { endpoints } from "../constants/discord.ts";
|
import { endpoints } from "../constants/discord.ts";
|
||||||
|
|
||||||
import { Errors } from "../types/errors.ts";
|
import { Errors } from "../types/errors.ts";
|
||||||
import { Permissions, Permission } from "../types/permission.ts";
|
import { Permissions, Permission } from "../types/permission.ts";
|
||||||
import {
|
import {
|
||||||
@@ -32,15 +27,15 @@ import {
|
|||||||
UserPayload,
|
UserPayload,
|
||||||
} from "../types/guild.ts";
|
} from "../types/guild.ts";
|
||||||
import { RoleData } from "../types/role.ts";
|
import { RoleData } from "../types/role.ts";
|
||||||
import { createRole } from "../structures/role.ts";
|
|
||||||
import { Intents } from "../types/options.ts";
|
import { Intents } from "../types/options.ts";
|
||||||
import { identifyPayload } from "../module/client.ts";
|
import { identifyPayload } from "../module/client.ts";
|
||||||
import { requestAllMembers } from "../module/shardingManager.ts";
|
import { requestAllMembers } from "../module/shardingManager.ts";
|
||||||
import { MemberCreatePayload } from "../types/member.ts";
|
import { MemberCreatePayload } from "../types/member.ts";
|
||||||
import { cache } from "../utils/cache.ts";
|
import { cache } from "../utils/cache.ts";
|
||||||
import { createMember, Member } from "../structures/member.ts";
|
import { Member } from "../structures/member.ts";
|
||||||
import { urlToBase64 } from "../utils/utils.ts";
|
import { urlToBase64 } from "../utils/utils.ts";
|
||||||
import { Collection } from "../utils/collection.ts";
|
import { Collection } from "../utils/collection.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
/** Gets an array of all the channels ids that are the children of this category. */
|
/** Gets an array of all the channels ids that are the children of this category. */
|
||||||
export function categoryChildrenIDs(guild: Guild, id: string) {
|
export function categoryChildrenIDs(guild: Guild, id: string) {
|
||||||
@@ -120,7 +115,7 @@ export async function createGuildChannel(
|
|||||||
type: options?.type || ChannelTypes.GUILD_TEXT,
|
type: options?.type || ChannelTypes.GUILD_TEXT,
|
||||||
})) as ChannelCreatePayload;
|
})) as ChannelCreatePayload;
|
||||||
|
|
||||||
const channel = createChannel(result);
|
const channel = structures.createChannel(result);
|
||||||
guild.channels.set(result.id, channel);
|
guild.channels.set(result.id, channel);
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
@@ -147,7 +142,7 @@ export async function getChannels(guildID: string, addToCache = true) {
|
|||||||
endpoints.GUILD_CHANNELS(guildID),
|
endpoints.GUILD_CHANNELS(guildID),
|
||||||
) as ChannelCreatePayload[];
|
) as ChannelCreatePayload[];
|
||||||
return result.map((res) => {
|
return result.map((res) => {
|
||||||
const channel = createChannel(res, guildID);
|
const channel = structures.createChannel(res, guildID);
|
||||||
if (addToCache) {
|
if (addToCache) {
|
||||||
cache.channels.set(channel.id, channel);
|
cache.channels.set(channel.id, channel);
|
||||||
}
|
}
|
||||||
@@ -163,7 +158,7 @@ export async function getChannel(channelID: string, addToCache = true) {
|
|||||||
const result = await RequestManager.get(
|
const result = await RequestManager.get(
|
||||||
endpoints.GUILD_CHANNEL(channelID),
|
endpoints.GUILD_CHANNEL(channelID),
|
||||||
) as ChannelCreatePayload;
|
) as ChannelCreatePayload;
|
||||||
const channel = createChannel(result, result.guild_id);
|
const channel = structures.createChannel(result, result.guild_id);
|
||||||
if (addToCache) cache.channels.set(channel.id, channel);
|
if (addToCache) cache.channels.set(channel.id, channel);
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
@@ -194,7 +189,7 @@ export async function getMember(guildID: string, id: string) {
|
|||||||
endpoints.GUILD_MEMBER(guildID, id),
|
endpoints.GUILD_MEMBER(guildID, id),
|
||||||
) as MemberCreatePayload;
|
) as MemberCreatePayload;
|
||||||
|
|
||||||
const member = createMember(data, guild);
|
const member = structures.createMember(data, guild);
|
||||||
guild.members.set(id, member);
|
guild.members.set(id, member);
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
@@ -300,7 +295,7 @@ export async function createGuildRole(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const roleData = result as RoleData;
|
const roleData = result as RoleData;
|
||||||
const role = createRole(roleData);
|
const role = structures.createRole(roleData);
|
||||||
const guild = cache.guilds.get(guildID);
|
const guild = cache.guilds.get(guildID);
|
||||||
guild?.roles.set(role.id, role);
|
guild?.roles.set(role.id, role);
|
||||||
return role;
|
return role;
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ import { Errors } from "../types/errors.ts";
|
|||||||
import { RequestManager } from "../module/requestManager.ts";
|
import { RequestManager } from "../module/requestManager.ts";
|
||||||
import { MessageContent, DMChannelCreatePayload } from "../types/channel.ts";
|
import { MessageContent, DMChannelCreatePayload } from "../types/channel.ts";
|
||||||
import { cache } from "../utils/cache.ts";
|
import { cache } from "../utils/cache.ts";
|
||||||
import { createChannel } from "../structures/channel.ts";
|
|
||||||
import { EditMemberOptions } from "../types/member.ts";
|
import { EditMemberOptions } from "../types/member.ts";
|
||||||
import { sendMessage } from "./channel.ts";
|
import { sendMessage } from "./channel.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
/** The users custom avatar or the default avatar if you don't have a member object. */
|
/** The users custom avatar or the default avatar if you don't have a member object. */
|
||||||
export function rawAvatarURL(
|
export function rawAvatarURL(
|
||||||
@@ -108,7 +108,7 @@ export async function sendDirectMessage(
|
|||||||
) as DMChannelCreatePayload;
|
) as DMChannelCreatePayload;
|
||||||
// Channel create event will have added this channel to the cache
|
// Channel create event will have added this channel to the cache
|
||||||
cache.channels.delete(dmChannelData.id);
|
cache.channels.delete(dmChannelData.id);
|
||||||
const channel = createChannel(dmChannelData);
|
const channel = structures.createChannel(dmChannelData);
|
||||||
// Recreate the channel and add it undert he users id
|
// Recreate the channel and add it undert he users id
|
||||||
cache.channels.set(memberID, channel);
|
cache.channels.set(memberID, channel);
|
||||||
dmChannel = channel;
|
dmChannel = channel;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Message, createMessage } from "../structures/message.ts";
|
import { Message } from "../structures/message.ts";
|
||||||
import { delay } from "https://deno.land/std@0.67.0/async/delay.ts";
|
import { delay } from "https://deno.land/std@0.67.0/async/delay.ts";
|
||||||
import { botID } from "../module/client.ts";
|
import { botID } from "../module/client.ts";
|
||||||
import { Permissions } from "../types/permission.ts";
|
import { Permissions } from "../types/permission.ts";
|
||||||
@@ -9,6 +9,7 @@ import { botHasChannelPermissions } from "../utils/permissions.ts";
|
|||||||
import { MessageContent } from "../types/channel.ts";
|
import { MessageContent } from "../types/channel.ts";
|
||||||
import { UserPayload } from "../types/guild.ts";
|
import { UserPayload } from "../types/guild.ts";
|
||||||
import { MessageCreateOptions } from "../types/message.ts";
|
import { MessageCreateOptions } from "../types/message.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
/** Delete a message */
|
/** Delete a message */
|
||||||
export async function deleteMessage(
|
export async function deleteMessage(
|
||||||
@@ -221,7 +222,7 @@ export async function editMessage(
|
|||||||
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
|
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
|
||||||
content,
|
content,
|
||||||
);
|
);
|
||||||
return createMessage(result as MessageCreateOptions);
|
return structures.createMessage(result as MessageCreateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function publishMessage(channelID: string, messageID: string) {
|
export async function publishMessage(channelID: string, messageID: string) {
|
||||||
@@ -229,5 +230,5 @@ export async function publishMessage(channelID: string, messageID: string) {
|
|||||||
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelID, messageID),
|
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelID, messageID),
|
||||||
) as MessageCreateOptions;
|
) as MessageCreateOptions;
|
||||||
|
|
||||||
return createMessage(data);
|
return structures.createMessage(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import { Permissions } from "../types/permission.ts";
|
|||||||
import { Errors } from "../types/errors.ts";
|
import { Errors } from "../types/errors.ts";
|
||||||
import { RequestManager } from "../module/requestManager.ts";
|
import { RequestManager } from "../module/requestManager.ts";
|
||||||
import { endpoints } from "../constants/discord.ts";
|
import { endpoints } from "../constants/discord.ts";
|
||||||
import { createMessage } from "../structures/message.ts";
|
|
||||||
import { MessageCreateOptions } from "../types/message.ts";
|
import { MessageCreateOptions } from "../types/message.ts";
|
||||||
import { urlToBase64 } from "../utils/utils.ts";
|
import { urlToBase64 } from "../utils/utils.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
/** Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
|
/** Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
|
||||||
*
|
*
|
||||||
@@ -98,7 +98,7 @@ export async function executeWebhook(
|
|||||||
);
|
);
|
||||||
if (!options.wait) return;
|
if (!options.wait) return;
|
||||||
|
|
||||||
return createMessage(result as MessageCreateOptions);
|
return structures.createMessage(result as MessageCreateOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getWebhook(webhookID: string) {
|
export function getWebhook(webhookID: string) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
handleInternalChannelDelete,
|
handleInternalChannelDelete,
|
||||||
} from "../events/channels.ts";
|
} from "../events/channels.ts";
|
||||||
import { ChannelCreatePayload } from "../types/channel.ts";
|
import { ChannelCreatePayload } from "../types/channel.ts";
|
||||||
import { createGuild, Guild } from "../structures/guild.ts";
|
import { Guild } from "../structures/guild.ts";
|
||||||
import {
|
import {
|
||||||
CreateGuildPayload,
|
CreateGuildPayload,
|
||||||
GuildDeletePayload,
|
GuildDeletePayload,
|
||||||
@@ -43,8 +43,6 @@ import {
|
|||||||
handleInternalGuildDelete,
|
handleInternalGuildDelete,
|
||||||
} from "../events/guilds.ts";
|
} from "../events/guilds.ts";
|
||||||
import { cache } from "../utils/cache.ts";
|
import { cache } from "../utils/cache.ts";
|
||||||
import { createMember } from "../structures/member.ts";
|
|
||||||
import { createRole } from "../structures/role.ts";
|
|
||||||
import {
|
import {
|
||||||
MessageCreateOptions,
|
MessageCreateOptions,
|
||||||
MessageDeletePayload,
|
MessageDeletePayload,
|
||||||
@@ -53,7 +51,6 @@ import {
|
|||||||
BaseMessageReactionPayload,
|
BaseMessageReactionPayload,
|
||||||
MessageReactionRemoveEmojiPayload,
|
MessageReactionRemoveEmojiPayload,
|
||||||
} from "../types/message.ts";
|
} from "../types/message.ts";
|
||||||
import { createMessage } from "../structures/message.ts";
|
|
||||||
import { GuildUpdateChange } from "../types/options.ts";
|
import { GuildUpdateChange } from "../types/options.ts";
|
||||||
import {
|
import {
|
||||||
createBasicShard,
|
createBasicShard,
|
||||||
@@ -61,6 +58,7 @@ import {
|
|||||||
botGatewayStatusRequest,
|
botGatewayStatusRequest,
|
||||||
} from "./basicShard.ts";
|
} from "./basicShard.ts";
|
||||||
import { BotStatusRequest } from "../utils/utils.ts";
|
import { BotStatusRequest } from "../utils/utils.ts";
|
||||||
|
import { structures } from "../structures/mod.ts";
|
||||||
|
|
||||||
let shardCounter = 0;
|
let shardCounter = 0;
|
||||||
let basicSharding = false;
|
let basicSharding = false;
|
||||||
@@ -176,7 +174,10 @@ export async function handleDiscordPayload(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const guild = createGuild(data.d as CreateGuildPayload, shardID);
|
const guild = structures.createGuild(
|
||||||
|
data.d as CreateGuildPayload,
|
||||||
|
shardID,
|
||||||
|
);
|
||||||
handleInternalGuildCreate(guild);
|
handleInternalGuildCreate(guild);
|
||||||
if (cache.unavailableGuilds.get(options.id)) {
|
if (cache.unavailableGuilds.get(options.id)) {
|
||||||
cache.unavailableGuilds.delete(options.id);
|
cache.unavailableGuilds.delete(options.id);
|
||||||
@@ -278,7 +279,7 @@ export async function handleDiscordPayload(
|
|||||||
|
|
||||||
const memberCount = guild.memberCount + 1;
|
const memberCount = guild.memberCount + 1;
|
||||||
guild.memberCount = memberCount;
|
guild.memberCount = memberCount;
|
||||||
const member = createMember(
|
const member = structures.createMember(
|
||||||
options,
|
options,
|
||||||
guild,
|
guild,
|
||||||
);
|
);
|
||||||
@@ -318,7 +319,7 @@ export async function handleDiscordPayload(
|
|||||||
deaf: cachedMember?.deaf || false,
|
deaf: cachedMember?.deaf || false,
|
||||||
mute: cachedMember?.mute || false,
|
mute: cachedMember?.mute || false,
|
||||||
};
|
};
|
||||||
const member = createMember(
|
const member = structures.createMember(
|
||||||
newMemberData,
|
newMemberData,
|
||||||
guild,
|
guild,
|
||||||
);
|
);
|
||||||
@@ -357,7 +358,7 @@ export async function handleDiscordPayload(
|
|||||||
options.members.forEach((member) => {
|
options.members.forEach((member) => {
|
||||||
guild.members.set(
|
guild.members.set(
|
||||||
member.user.id,
|
member.user.id,
|
||||||
createMember(
|
structures.createMember(
|
||||||
member,
|
member,
|
||||||
guild,
|
guild,
|
||||||
),
|
),
|
||||||
@@ -398,7 +399,7 @@ export async function handleDiscordPayload(
|
|||||||
if (!guild) return;
|
if (!guild) return;
|
||||||
|
|
||||||
if (data.t === "GUILD_ROLE_CREATE") {
|
if (data.t === "GUILD_ROLE_CREATE") {
|
||||||
const role = createRole(options.role);
|
const role = structures.createRole(options.role);
|
||||||
const roles = guild.roles.set(options.role.id, role);
|
const roles = guild.roles.set(options.role.id, role);
|
||||||
guild.roles = roles;
|
guild.roles = roles;
|
||||||
return eventHandlers.roleCreate?.(guild, role);
|
return eventHandlers.roleCreate?.(guild, role);
|
||||||
@@ -408,7 +409,7 @@ export async function handleDiscordPayload(
|
|||||||
if (!cachedRole) return;
|
if (!cachedRole) return;
|
||||||
|
|
||||||
if (data.t === "GUILD_ROLE_UPDATE") {
|
if (data.t === "GUILD_ROLE_UPDATE") {
|
||||||
const role = createRole(options.role);
|
const role = structures.createRole(options.role);
|
||||||
return eventHandlers.roleUpdate?.(guild, role, cachedRole);
|
return eventHandlers.roleUpdate?.(guild, role, cachedRole);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -418,7 +419,7 @@ export async function handleDiscordPayload(
|
|||||||
const channel = cache.channels.get(options.channel_id);
|
const channel = cache.channels.get(options.channel_id);
|
||||||
if (channel) channel.lastMessageID = options.id;
|
if (channel) channel.lastMessageID = options.id;
|
||||||
|
|
||||||
const message = createMessage(options);
|
const message = structures.createMessage(options);
|
||||||
// Cache the message
|
// Cache the message
|
||||||
cache.messages.set(options.id, message);
|
cache.messages.set(options.id, message);
|
||||||
const guild = options.guild_id
|
const guild = options.guild_id
|
||||||
@@ -429,7 +430,7 @@ export async function handleDiscordPayload(
|
|||||||
// If in a guild cache the author as a member
|
// If in a guild cache the author as a member
|
||||||
guild?.members.set(
|
guild?.members.set(
|
||||||
options.author.id,
|
options.author.id,
|
||||||
createMember(
|
structures.createMember(
|
||||||
{ ...options.member, user: options.author },
|
{ ...options.member, user: options.author },
|
||||||
guild,
|
guild,
|
||||||
),
|
),
|
||||||
@@ -441,7 +442,10 @@ export async function handleDiscordPayload(
|
|||||||
if (mention.member) {
|
if (mention.member) {
|
||||||
guild?.members.set(
|
guild?.members.set(
|
||||||
mention.id,
|
mention.id,
|
||||||
createMember({ ...mention.member, user: mention }, guild),
|
structures.createMember(
|
||||||
|
{ ...mention.member, user: mention },
|
||||||
|
guild,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -533,7 +537,7 @@ export async function handleDiscordPayload(
|
|||||||
const guild = cache.guilds.get(options.guild_id);
|
const guild = cache.guilds.get(options.guild_id);
|
||||||
guild?.members.set(
|
guild?.members.set(
|
||||||
options.member.user.id,
|
options.member.user.id,
|
||||||
createMember(
|
structures.createMember(
|
||||||
options.member,
|
options.member,
|
||||||
guild,
|
guild,
|
||||||
),
|
),
|
||||||
@@ -608,7 +612,9 @@ export async function handleDiscordPayload(
|
|||||||
if (!guild) return;
|
if (!guild) return;
|
||||||
|
|
||||||
const member = guild.members.get(payload.user_id) ||
|
const member = guild.members.get(payload.user_id) ||
|
||||||
(payload.member ? createMember(payload.member, guild) : undefined);
|
(payload.member
|
||||||
|
? structures.createMember(payload.member, guild)
|
||||||
|
: undefined);
|
||||||
if (!member) return;
|
if (!member) return;
|
||||||
|
|
||||||
// No cached state before so lets make one for em
|
// No cached state before so lets make one for em
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { CreateGuildPayload } from "../types/guild.ts";
|
import { CreateGuildPayload } from "../types/guild.ts";
|
||||||
import { Collection } from "../utils/collection.ts";
|
import { Collection } from "../utils/collection.ts";
|
||||||
import { createRole } from "./role.ts";
|
import { structures } from "./mod.ts";
|
||||||
import { createMember, Member } from "./member.ts";
|
import { Member } from "./member.ts";
|
||||||
import { createChannel } from "./channel.ts";
|
|
||||||
|
|
||||||
export const createGuild = (data: CreateGuildPayload, shardID: number) => {
|
export function createGuild(data: CreateGuildPayload, shardID: number) {
|
||||||
const {
|
const {
|
||||||
owner_id: ownerID,
|
owner_id: ownerID,
|
||||||
afk_channel_id: afkChannelID,
|
afk_channel_id: afkChannelID,
|
||||||
@@ -60,14 +59,16 @@ export const createGuild = (data: CreateGuildPayload, shardID: number) => {
|
|||||||
preferredLocale,
|
preferredLocale,
|
||||||
|
|
||||||
/** The roles in the guild */
|
/** The roles in the guild */
|
||||||
roles: new Collection(data.roles.map((r) => [r.id, createRole(r)])),
|
roles: new Collection(
|
||||||
|
data.roles.map((r) => [r.id, structures.createRole(r)]),
|
||||||
|
),
|
||||||
/** When this guild was joined at. */
|
/** When this guild was joined at. */
|
||||||
joinedAt: Date.parse(joinedAt),
|
joinedAt: Date.parse(joinedAt),
|
||||||
/** The users in this guild. */
|
/** The users in this guild. */
|
||||||
members: new Collection<string, Member>(),
|
members: new Collection<string, Member>(),
|
||||||
/** The channels in the guild */
|
/** The channels in the guild */
|
||||||
channels: new Collection(
|
channels: new Collection(
|
||||||
data.channels.map((c) => [c.id, createChannel(c, data.id)]),
|
data.channels.map((c) => [c.id, structures.createChannel(c, data.id)]),
|
||||||
),
|
),
|
||||||
/** The presences of all the users in the guild. */
|
/** The presences of all the users in the guild. */
|
||||||
presences: new Collection(data.presences.map((p) => [p.user.id, p])),
|
presences: new Collection(data.presences.map((p) => [p.user.id, p])),
|
||||||
@@ -87,10 +88,10 @@ export const createGuild = (data: CreateGuildPayload, shardID: number) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
data.members.forEach((m) =>
|
data.members.forEach((m) =>
|
||||||
guild.members.set(m.user.id, createMember(m, guild))
|
guild.members.set(m.user.id, structures.createMember(m, guild))
|
||||||
);
|
);
|
||||||
|
|
||||||
return guild;
|
return guild;
|
||||||
};
|
}
|
||||||
|
|
||||||
export interface Guild extends ReturnType<typeof createGuild> {}
|
export interface Guild extends ReturnType<typeof createGuild> {}
|
||||||
|
|||||||
+20
-18
@@ -1,16 +1,27 @@
|
|||||||
import { MemberCreatePayload } from "../types/member.ts";
|
import { MemberCreatePayload } from "../types/member.ts";
|
||||||
import { Guild } from "./guild.ts";
|
import { Guild } from "./guild.ts";
|
||||||
import { cache } from "../utils/cache.ts";
|
|
||||||
|
|
||||||
export const createMember = (data: MemberCreatePayload, guild: Guild) => {
|
export function createMember(data: MemberCreatePayload, guild: Guild) {
|
||||||
|
const {
|
||||||
|
joined_at: joinedAt,
|
||||||
|
premium_since: premiumSince,
|
||||||
|
...rest
|
||||||
|
} = data;
|
||||||
|
|
||||||
|
const {
|
||||||
|
mfa_enabled: mfaEnabled,
|
||||||
|
premium_type: premiumType,
|
||||||
|
...user
|
||||||
|
} = data.user;
|
||||||
|
|
||||||
const member = {
|
const member = {
|
||||||
...data,
|
...rest,
|
||||||
|
// Only use those that we have not removed above
|
||||||
|
user: user,
|
||||||
/** When the user joined the guild */
|
/** When the user joined the guild */
|
||||||
joinedAt: Date.parse(data.joined_at),
|
joinedAt: Date.parse(joinedAt),
|
||||||
/** When the user used their nitro boost on the server. */
|
/** When the user used their nitro boost on the server. */
|
||||||
premiumSince: data.premium_since
|
premiumSince: premiumSince ? Date.parse(premiumSince) : undefined,
|
||||||
? Date.parse(data.premium_since)
|
|
||||||
: undefined,
|
|
||||||
/** The full username#discriminator */
|
/** The full username#discriminator */
|
||||||
tag: `${data.user.username}#${data.user.discriminator}`,
|
tag: `${data.user.username}#${data.user.discriminator}`,
|
||||||
/** The user mention with nickname if possible */
|
/** The user mention with nickname if possible */
|
||||||
@@ -18,20 +29,11 @@ export const createMember = (data: MemberCreatePayload, guild: Guild) => {
|
|||||||
/** The guild id where this member exists */
|
/** The guild id where this member exists */
|
||||||
guildID: guild.id,
|
guildID: guild.id,
|
||||||
/** Whether or not this user has 2FA enabled. */
|
/** Whether or not this user has 2FA enabled. */
|
||||||
mfaEnabled: data.user.mfa_enabled,
|
mfaEnabled,
|
||||||
/** The premium type for this user */
|
/** The premium type for this user */
|
||||||
premiumType: data.user.premium_type,
|
premiumType,
|
||||||
|
|
||||||
/** Gets the guild object from cache for this member. This is a method instead of a prop to preserve memory. */
|
|
||||||
guild: () => cache.guilds.get(guild.id)!,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remove excess properties to preserve cache.
|
|
||||||
// delete member.joined_at;
|
|
||||||
// delete member.premium_since;
|
|
||||||
// delete member.user.mfa_enabled;
|
|
||||||
// delete member.user.premium_type;
|
|
||||||
|
|
||||||
return member;
|
return member;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { createChannel } from "./channel.ts";
|
||||||
|
import { createGuild } from "./guild.ts";
|
||||||
|
import { createMember } from "./member.ts";
|
||||||
|
import { createMessage } from "./message.ts";
|
||||||
|
import { createRole } from "./role.ts";
|
||||||
|
|
||||||
|
/** This is the placeholder where the structure creation functions are kept. */
|
||||||
|
export let structures = {
|
||||||
|
createChannel,
|
||||||
|
createGuild,
|
||||||
|
createMember,
|
||||||
|
createMessage,
|
||||||
|
createRole,
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Structures = typeof structures[keyof typeof structures];
|
||||||
|
|
||||||
|
/** This function is used to update/reload/customize the internal structure of Discordeno.
|
||||||
|
*
|
||||||
|
* ⚠️ **ADVANCED USE ONLY: If you customize this in a wrong way, you could potentially create many new errors/bugs. Please take caution when using this.
|
||||||
|
*/
|
||||||
|
export function updateStructures(newStructures: Structures) {
|
||||||
|
structures = {
|
||||||
|
...structures,
|
||||||
|
...newStructures,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
import { RoleData } from "../types/role.ts";
|
import { RoleData } from "../types/role.ts";
|
||||||
|
|
||||||
export const createRole = (data: RoleData) => ({
|
export function createRole(data: RoleData) {
|
||||||
...data,
|
return {
|
||||||
/** The @ mention of the role in a string. */
|
...data,
|
||||||
mention: `<@&${data.id}>`,
|
/** The @ mention of the role in a string. */
|
||||||
});
|
mention: `<@&${data.id}>`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export interface Role extends ReturnType<typeof createRole> {}
|
export interface Role extends ReturnType<typeof createRole> {}
|
||||||
|
|||||||
Reference in New Issue
Block a user