Merge branch 'fp-attempt-9001' of https://github.com/discordeno/discordeno into no-caching

This commit is contained in:
Skillz4Killz
2021-11-09 19:17:38 +00:00
committed by GitHub
18 changed files with 149 additions and 82 deletions

View File

@@ -128,7 +128,7 @@ type CacheOptions =
export function createBot<C extends CacheOptions = CacheOptions>(
options: CreateBotOptions<C>
): Bot<C extends { isAsync: true } ? AsyncCache : Cache> {
return {
const bot = {
id: options.botId,
applicationId: options.applicationId || options.botId,
token: `Bot ${options.token}`,
@@ -139,9 +139,12 @@ export function createBot<C extends CacheOptions = CacheOptions>(
activeGuildIds: new Set<bigint>(),
constants: createBotConstants(),
handlers: createBotGatewayHandlers({}),
// @ts-ignore b quiet
cache: createCache(options?.cache?.isAsync ?? false, options?.cache?.customTableCreator),
} as unknown as Bot<C extends { isAsync: true } ? AsyncCache : Cache>;
};
// @ts-ignore itoh cache types plz
bot.cache = createCache(bot as Bot, options.cache);
return bot as unknown as Bot<C extends { isAsync: true } ? AsyncCache : Cache>;
}
export function createEventHandlers(events: Partial<EventHandlers>): EventHandlers {
@@ -226,6 +229,7 @@ export function createRestManager(options: CreateRestManagerOptions) {
token: `${options.token.startsWith("Bot ") ? "" : "Bot "}${options.token}`,
maxRetryCount: options.maxRetryCount || 10,
secretKey: options.secretKey || "discordeno_best_lib_ever",
customUrl: options.customUrl || "",
pathQueues: new Map<
string,
{
@@ -261,12 +265,18 @@ export function createRestManager(options: CreateRestManagerOptions) {
};
}
export async function startBot(bot: Bot) {
// SETUP
export function setupBot(bot: Bot) {
bot.utils = createUtils({});
bot.transformers = createTransformers(bot.transformers || {});
bot.helpers = createHelpers(bot);
return bot;
}
export async function startBot(bot: Bot) {
// SETUP BOT
bot = setupBot(bot);
// START REST
bot.rest = createRestManager({ token: bot.token, debug: bot.events.debug });
if (!bot.botGatewayData) bot.botGatewayData = await bot.helpers.getGatewayBot();
@@ -643,8 +653,8 @@ export interface Helpers {
leaveThread: typeof helpers.leaveThread;
lockThread: typeof helpers.lockThread;
removeThreadMember: typeof helpers.removeThreadMember;
startPrivateThread: typeof helpers.startPrivateThread;
startThread: typeof helpers.startThread;
startThreadWithoutMessage: typeof helpers.startThreadWithoutMessage;
startThreadWithMessage: typeof helpers.startThreadWithMessage;
unarchiveThread: typeof helpers.unarchiveThread;
unlockThread: typeof helpers.unlockThread;
suppressEmbeds: typeof helpers.suppressEmbeds;
@@ -818,8 +828,8 @@ export function createBaseHelpers(options: Partial<Helpers>) {
leaveThread: options.leaveThread || helpers.leaveThread,
lockThread: options.lockThread || helpers.lockThread,
removeThreadMember: options.removeThreadMember || helpers.removeThreadMember,
startPrivateThread: options.startPrivateThread || helpers.startPrivateThread,
startThread: options.startThread || helpers.startThread,
startThreadWithoutMessage: options.startThreadWithoutMessage || helpers.startThreadWithoutMessage,
startThreadWithMessage: options.startThreadWithMessage || helpers.startThreadWithMessage,
unarchiveThread: options.unarchiveThread || helpers.unarchiveThread,
unlockThread: options.unlockThread || helpers.unlockThread,
suppressEmbeds: options.suppressEmbeds || helpers.suppressEmbeds,
@@ -849,7 +859,7 @@ export interface Transformers {
thread: typeof transformThread;
webhook: typeof transformWebhook;
auditlogEntry: typeof transformAuditlogEntry;
applicationCommandPermission: typeof transformApplicationCommandPermission
applicationCommandPermission: typeof transformApplicationCommandPermission;
}
export function createTransformers(options: Partial<Transformers>) {
@@ -876,7 +886,7 @@ export function createTransformers(options: Partial<Transformers>) {
snowflake: options.snowflake || snowflakeToBigint,
webhook: options.webhook || transformWebhook,
auditlogEntry: options.auditlogEntry || transformAuditlogEntry,
applicationCommandPermission: transformApplicationCommandPermission
applicationCommandPermission: transformApplicationCommandPermission,
};
}

View File

@@ -48,36 +48,40 @@ function channelSweeper(bot: Bot<Cache>, channel: DiscordenoChannel, key: bigint
}
export function createCache(
isAsync: true,
// deno-lint-ignore no-explicit-any
tableCreator: (tableName: TableNames) => AsyncCacheHandler<any>
bot: Bot,
options: {
isAsync: true;
tableCreator: (bot: Bot, tableName: TableNames) => AsyncCacheHandler<any>;
}
): AsyncCache;
export function createCache(
isAsync: false,
// deno-lint-ignore no-explicit-any
tableCreator?: (tableName: TableNames) => CacheHandler<any>
bot: Bot,
options: {
isAsync: false;
tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler<any>;
}
): Cache;
export function createCache(
isAsync: boolean,
tableCreator?: (
tableName: TableNames
// deno-lint-ignore no-explicit-any
) => CacheHandler<any> | AsyncCacheHandler<any>
bot: Bot,
options: {
isAsync: boolean;
tableCreator?: (bot: Bot, tableName: TableNames) => CacheHandler<any> | AsyncCacheHandler<any>;
}
): Omit<Cache, "execute"> | Omit<AsyncCache, "execute"> {
if (isAsync) {
if (!tableCreator) {
if (options.isAsync) {
if (!options.tableCreator) {
throw new Error("Async cache requires a tableCreator to be passed.");
}
const cache = {
guilds: tableCreator("guilds"),
users: tableCreator("users"),
members: tableCreator("members"),
channels: tableCreator("channels"),
messages: tableCreator("messages"),
presences: tableCreator("presences"),
// threads: tableCreator("threads"),
unavailableGuilds: tableCreator("unavailableGuilds"),
guilds: options.tableCreator(bot, "guilds"),
users: options.tableCreator(bot, "users"),
members: options.tableCreator(bot, "members"),
channels: options.tableCreator(bot, "channels"),
messages: options.tableCreator(bot, "messages"),
presences: options.tableCreator(bot, "presences"),
// threads: options.tableCreator(bot, "threads"),
unavailableGuilds: options.tableCreator(bot, "unavailableGuilds"),
executedSlashCommands: new Set(),
fetchAllMembersProcessingRequests: new Map(),
} as AsyncCache;
@@ -88,17 +92,17 @@ export function createCache(
return cache;
}
if (!tableCreator) tableCreator = createTable;
if (!options.tableCreator) options.tableCreator = createTable;
const cache = {
guilds: tableCreator("guilds"),
users: tableCreator("users"),
members: tableCreator("members"),
channels: tableCreator("channels"),
messages: tableCreator("messages"),
presences: tableCreator("presences"),
// threads: tableCreator("threads"),
unavailableGuilds: tableCreator("unavailableGuilds"),
guilds: options.tableCreator(bot, "guilds"),
users: options.tableCreator(bot, "users"),
members: options.tableCreator(bot, "members"),
channels: options.tableCreator(bot, "channels"),
messages: options.tableCreator(bot, "messages"),
presences: options.tableCreator(bot, "presences"),
// threads: options.tableCreator(bot, "threads"),
unavailableGuilds: options.tableCreator(bot, "unavailableGuilds"),
executedSlashCommands: new Set(),
fetchAllMembersProcessingRequests: new Map(),
} as Cache;
@@ -149,18 +153,18 @@ export interface AsyncCache {
execute: CacheExecutor;
}
function createTable<T>(_table: TableNames): CacheHandler<T> {
function createTable<T>(bot: Bot, _table: TableNames): CacheHandler<T> {
const table = new Collection<bigint, T>();
// @ts-ignore TODO: fix type error itoh pwease
if (_table === "guilds") table.startSweeper({ filter: guildSweeper, interval: 3660000 });
if (_table === "guilds") table.startSweeper({ filter: guildSweeper, interval: 3660000, bot });
// @ts-ignore TODO: fix type error itoh pwease
if (_table === "channels") table.startSweeper({ filter: channelSweeper, interval: 3660000 });
if (_table === "channels") table.startSweeper({ filter: channelSweeper, interval: 3660000, bot });
// @ts-ignore TODO: fix type error itoh pwease
if (_table === "messages") table.startSweeper({ filter: messageSweeper, interval: 300000 });
if (_table === "messages") table.startSweeper({ filter: messageSweeper, interval: 300000, bot });
// @ts-ignore TODO: fix type error itoh pwease
if (_table === "members") table.startSweeper({ filter: memberSweeper, interval: 300000 });
if (_table === "presences") table.startSweeper({ filter: () => true, interval: 300000 });
if (_table === "members") table.startSweeper({ filter: memberSweeper, interval: 300000, bot });
if (_table === "presences") table.startSweeper({ filter: () => true, interval: 300000, bot });
return {
clear: () => table.clear(),

View File

@@ -29,8 +29,6 @@ export async function cloneChannel(bot: Bot, channelId: bigint, reason?: string)
}),
};
//Create the channel (also handles permissions)
return await bot.helpers.createChannel(channelToClone.guildId!, createChannelOptions, reason);
}

View File

@@ -4,7 +4,7 @@ import { Collection } from "../../../util/collection.ts";
// import { channelToThread } from "../../../util/transformers/channel_to_thread.ts";
/** Returns all active threads in the guild, including public and private threads. Threads are ordered by their `id`, in descending order. */
export async function getActiveThreads(bot: Bot, guildId: bigint) {
export async function getActiveThreads(bot: Bot, guildId: bigint) {
// const result = (await bot.rest.runMethod(
// bot.rest,
// "get",

View File

@@ -3,7 +3,12 @@ import type { StartThreadWithMessage } from "../../../types/channels/threads/sta
import type { Bot } from "../../../bot.ts";
/** Creates a new public thread from an existing message. Returns a thread channel. */
export async function startThreadWithMessage(bot: Bot, channelId: bigint, messageId: bigint, options: StartThreadWithMessage) {
export async function startThreadWithMessage(
bot: Bot,
channelId: bigint,
messageId: bigint,
options: StartThreadWithMessage
) {
// const channel = await bot.cache.channels.get(channelId);
// if (channel) {
// if (!channel.isGuildTextBasedChannel) {

View File

@@ -88,10 +88,20 @@ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint
})),
allowed_mentions: {
parse: content.allowedMentions?.parse,
roles: content.allowedMentions?.roles,
users: content.allowedMentions?.users,
roles: content.allowedMentions?.roles?.map((id) => id.toString()),
users: content.allowedMentions?.users?.map((id) => id.toString()),
replied_user: content.allowedMentions?.repliedUser,
},
attachments: content.attachments?.map((attachment) => ({
id: attachment.id.toString(),
filename: attachment.filename,
content_type: attachment.contentType,
size: attachment.size,
url: attachment.url,
proxy_url: attachment.proxyUrl,
height: attachment.height,
width: attachment.width,
})),
file: content.file,
components: content.components?.map((component) => ({
type: component.type,

View File

@@ -132,8 +132,8 @@ export async function sendMessage(bot: Bot, channelId: bigint, content: string |
allowed_mentions: content.allowedMentions
? {
parse: content.allowedMentions?.parse,
roles: content.allowedMentions?.roles,
users: content.allowedMentions?.users,
roles: content.allowedMentions?.roles?.map((id) => id.toString()),
users: content.allowedMentions?.users?.map((id) => id.toString()),
replied_user: content.allowedMentions?.repliedUser,
}
: undefined,
@@ -183,9 +183,9 @@ export async function sendMessage(bot: Bot, channelId: bigint, content: string |
...(content.messageReference?.messageId
? {
message_reference: {
message_id: content.messageReference.messageId,
channel_id: content.messageReference.channelId,
guild_id: content.messageReference.guildId,
message_id: content.messageReference.messageId.toString(),
channel_id: content.messageReference.channelId?.toString(),
guild_id: content.messageReference.guildId?.toString(),
fail_if_not_exists: content.messageReference.failIfNotExists === true,
},
}

View File

@@ -149,8 +149,8 @@ import { joinThread } from "./channels/threads/join_thread.ts";
import { leaveThread } from "./channels/threads/leave_thread.ts";
import { lockThread } from "./channels/threads/lock_thread.ts";
import { removeThreadMember } from "./channels/threads/remove_thread_member.ts";
import { startPrivateThread } from "./channels/threads/start_private_thread.ts";
import { startThread } from "./channels/threads/start_thread.ts";
import { startThreadWithMessage } from "./channels/threads/startThreadWithMessage.ts";
import { startThreadWithoutMessage } from "./channels/threads/startThreadWithoutMessage.ts";
import { unarchiveThread } from "./channels/threads/unarchive_thread.ts";
import { unlockThread } from "./channels/threads/unlock_thread.ts";
import { cloneChannel } from "./channels/clone_channel.ts";
@@ -309,8 +309,8 @@ export {
leaveThread,
lockThread,
removeThreadMember,
startPrivateThread,
startThread,
startThreadWithMessage,
startThreadWithoutMessage,
unarchiveThread,
unlockThread,
suppressEmbeds,

View File

@@ -9,7 +9,7 @@ export function simplifyUrl(url: string, method: string) {
.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, function (match, p) {
return ["channels", "guilds"].includes(p) ? match : `/${p}/skillzPrefersID`;
})
.replace(/\/reactions\/[^/]+/g, "/reactions/skillzPrefersID")
.replace(/\/reactions\/[^/]+/g, "/reactions/skillzPrefersID");
// GENERAL /reactions and /reactions/emoji/@me share the buckets
if (route.includes("/reactions")) {

View File

@@ -21,11 +21,11 @@ export function transformAuditlogEntry(
key: change.key,
new: {
id: bot.transformers.snowflake(change.new_value.id!),
name: change.new_value.name
name: change.new_value.name,
},
old: {
id: bot.transformers.snowflake(change.old_value.id!),
name: change.old_value.name
name: change.old_value.name,
},
};
case "discovery_splash_hash":

View File

@@ -26,9 +26,9 @@ function unpack64(v: bigint, shift: number) {
return (v >> BigInt(shift * 64)) & Mask;
}
function pack64(v: string | number, shift: number) {
const b = BigInt(v);
if(b < 0 || b > Mask) throw new Error("should have been a 64 bit unsigned integer: " + v);
return b << BigInt(shift * 64)
const b = BigInt(v);
if (b < 0 || b > Mask) throw new Error("should have been a 64 bit unsigned integer: " + v);
return b << BigInt(shift * 64);
}
export function separate(v: bigint) {
return [Number(unpack64(v, 3)), unpack64(v, 2), unpack64(v, 0), unpack64(v, 1)] as [number, bigint, bigint, bigint];

View File

@@ -66,7 +66,17 @@ export type AuditLogChange =
| {
newValue: boolean;
oldValue: boolean;
key: "widget_enabled" | "nsfw" | "hoist" | "mentionable" | "temporary" | "deaf" | "mute" | "enable_emoticons" | "archived" | "locked";
key:
| "widget_enabled"
| "nsfw"
| "hoist"
| "mentionable"
| "temporary"
| "deaf"
| "mute"
| "enable_emoticons"
| "archived"
| "locked";
}
| {
newValue: Overwrite[];

View File

@@ -17,7 +17,10 @@ export interface StartThreadWithMessage extends StartThreadBase {
export interface StartThreadWithoutMessage extends StartThreadBase {
/** the type of thread to create */
type: DiscordChannelTypes.GuildNewsThread | DiscordChannelTypes.GuildPublicThread | DiscordChannelTypes.GuildPrivateThread;
type:
| DiscordChannelTypes.GuildNewsThread
| DiscordChannelTypes.GuildPublicThread
| DiscordChannelTypes.GuildPrivateThread;
/** whether non-moderators can add other non-moderators to a thread; only available when creating a private thread */
invitable?: boolean;
}
}

View File

@@ -29,4 +29,3 @@ export interface ThreadMembersUpdateModified extends ThreadMembersUpdateBase {
// TODO: verify this
// \* In this gateway event, the thread member objects will also include the [guild member](#DOCS_RESOURCES_GUILD/guild-member-object) and [presence](#DOCS_TOPICS_GATEWAY/presence) objects for each added thread member.
}

View File

@@ -13,9 +13,26 @@ export interface CreateMessage {
/** Embedded `rich` content (up to 6000 characters) */
embeds?: Embed[];
/** Allowed mentions for the message */
allowedMentions?: AllowedMentions;
allowedMentions?: Omit<AllowedMentions, "users" | "roles"> & {
/** Array of role_ids to mention (Max size of 100) */
roles?: bigint[];
/** Array of user_ids to mention (Max size of 100) */
users?: bigint[];
};
/** Include to make your message a reply */
messageReference?: MessageReference;
messageReference?: {
/** id of the originating message */
messageId?: bigint;
/**
* id of the originating message's channel
* Note: `channel_id` is optional when creating a reply, but will always be present when receiving an event/response that includes this data model.
*/
channelId?: bigint;
/** id of the originating message's guild */
guildId?: bigint;
/** When sending, whether to error if the referenced message doesn't exist instead of sending as a normal (non-reply) message, default true */
failIfNotExists: boolean;
};
/** The contents of the file being sent */
file?: FileContent | FileContent[];
/** The components you would like to have sent in this message */

View File

@@ -15,9 +15,16 @@ export interface EditMessage {
/** The contents of the file being sent/edited */
file?: FileContent | FileContent[] | null;
/** Allowed mentions for the message */
allowedMentions?: AllowedMentions | null;
allowedMentions?:
| (Omit<AllowedMentions, "users" | "roles"> & {
/** Array of role_ids to mention (Max size of 100) */
roles?: bigint[];
/** Array of user_ids to mention (Max size of 100) */
users?: bigint[];
})
| null;
/** Attached files to keep */
attachments?: Attachment | null;
attachments?: Attachment[];
/** The components you would like to have sent in this message */
components?: MessageComponents;
}

View File

@@ -35,8 +35,12 @@ export async function categoryChildrenTest(bot: Bot, guildId: bigint, t: Deno.Te
const ids = await bot.helpers.categoryChildren(category.id);
if (ids.size !== channels.length || !channels.every((c) => ids.has(c.id))) {
console.log('cccc 1', ids.size, channels.length);
console.log('cccc 2', channels.every((c) => ids.has(c.id)), ids);
console.log("cccc 1", ids.size, channels.length);
console.log(
"cccc 2",
channels.every((c) => ids.has(c.id)),
ids
);
throw new Error("The category channel ids did not match with the category channels.");
}
}

View File

@@ -575,18 +575,18 @@ Deno.test({
name: "[channel] delete a channel overwrite",
async fn() {
await deleteChannelOverwriteTests(bot, guild.id, t);
}
},
}),
t.step({
name: "[channel] edit a channel w/o a reason",
async fn() {
await editChannelTests(bot, guild.id, {}, t);
}
},
}),
t.step({
name: "[channel] edit a channel w/ a reason",
async fn() {
await editChannelTests(bot, guild.id, { reason: "Blame wolf"}, t);
await editChannelTests(bot, guild.id, { reason: "Blame wolf" }, t);
},
}),
]);