fix tests and cache bugs

This commit is contained in:
Skillz4Killz
2021-10-29 15:46:36 +00:00
committed by GitHub
parent dfe37ef4ea
commit 6f6099abc6
16 changed files with 127 additions and 39 deletions
+29 -3
View File
@@ -253,11 +253,11 @@ export async function startBot(bot: Bot) {
// SETUP
bot.utils = createUtils({});
bot.transformers = createTransformers(bot.transformers || {});
bot.helpers = createHelpers(bot.helpers || {});
bot.helpers = createHelpers(bot);
// START REST
bot.rest = createRestManager({ token: bot.token, debug: bot.events.debug });
if (!bot.botGatewayData) bot.botGatewayData = await bot.helpers.getGatewayBot(bot);
if (!bot.botGatewayData) bot.botGatewayData = await bot.helpers.getGatewayBot();
// START WS
bot.gateway = createGatewayManager({
@@ -629,7 +629,24 @@ export interface Helpers {
suppressEmbeds: typeof helpers.suppressEmbeds;
}
export function createHelpers(options: Partial<Helpers>) {
export function createHelpers(
bot: Bot,
customHelpers?: Partial<Helpers>,
): FinalHelpers {
const converted = {} as FinalHelpers;
for (const [name, fun] of Object.entries({ ...createBaseHelpers(customHelpers || {}) })) {
// @ts-ignore - TODO: make the types better
converted[name as keyof FinalHelpers] = (
...args: RemoveFirstFromTuple<Parameters<typeof fun>>
) =>
// @ts-ignore - TODO: make the types better
fun(bot, ...args);
}
return converted;
}
export function createBaseHelpers(options: Partial<Helpers>) {
return {
addDiscoverySubcategory: options.addDiscoverySubcategory || helpers.addDiscoverySubcategory,
addReaction: options.addReaction || helpers.addReaction,
@@ -1236,3 +1253,12 @@ export function createBotGatewayHandlers(
INTEGRATION_DELETE: options.INTEGRATION_DELETE ?? handlers.handleIntegrationDelete,
};
}
export type RemoveFirstFromTuple<T extends any[]> = T["length"] extends 0 ? []
: ((...b: T) => void) extends (a: any, ...b: infer I) => void ? I
: [];
export type FinalHelpers = {
[K in keyof Helpers]: (
...args: RemoveFirstFromTuple<Parameters<Helpers[K]>>
) => ReturnType<Helpers[K]>;
};
+19 -8
View File
@@ -1,14 +1,9 @@
import { Bot } from "./bot.ts";
import type { DiscordenoChannel } from "./transformers/channel.ts";
import type { DiscordenoGuild } from "./transformers/guild.ts";
import type { DiscordenoMember, DiscordenoUser } from "./transformers/member.ts";
import type { DiscordenoMessage } from "./transformers/message.ts";
import { DiscordenoPresence } from "./transformers/presence.ts";
import { PresenceUpdate } from "./types/activity/presence_update.ts";
import { Channel } from "./types/channels/channel.ts";
import { GuildMember } from "./types/members/guild_member.ts";
import { Message } from "./types/messages/message.ts";
import { SnakeCasedPropertiesDeep } from "./types/util.ts";
import { Collection } from "./util/collection.ts";
export function createCache(
@@ -33,7 +28,7 @@ export function createCache(
throw new Error("Async cache requires a tableCreator to be passed.");
}
return {
const cache = {
guilds: tableCreator("guilds"),
users: tableCreator("users"),
members: tableCreator("members"),
@@ -44,10 +39,14 @@ export function createCache(
unavailableGuilds: tableCreator("unavailableGuilds"),
executedSlashCommands: new Set(),
} as AsyncCache;
cache.execute = createExecute(cache);
return cache;
}
if (!tableCreator) tableCreator = createTable;
return {
const cache = {
guilds: tableCreator("guilds"),
users: tableCreator("users"),
members: tableCreator("members"),
@@ -58,6 +57,10 @@ export function createCache(
unavailableGuilds: tableCreator("unavailableGuilds"),
executedSlashCommands: new Set(),
} as Cache;
cache.execute = createExecute(cache);
return cache;
}
export type CachedDiscordenoUser = DiscordenoUser & { guilds: Map<bigint, GuildMember> };
@@ -180,7 +183,15 @@ export function createExecute(cache: Cache | AsyncCache): CacheExecutor {
};
}
export type TableNames = "channels" | "users" | "guilds" | "messages" | "presences" | "threads" | "unavailableGuilds" | "members";
export type TableNames =
| "channels"
| "users"
| "guilds"
| "messages"
| "presences"
| "threads"
| "unavailableGuilds"
| "members";
// function messageSweeper(bot: Bot, message: DiscordenoMessage) {
// // DM messages aren't needed
+1 -1
View File
@@ -25,5 +25,5 @@ export async function cloneChannel(bot: Bot, channelId: bigint, reason?: string)
};
//Create the channel (also handles permissions)
return await bot.helpers.createChannel(bot, channelToClone.guildId!, createChannelOptions, reason);
return await bot.helpers.createChannel(channelToClone.guildId!, createChannelOptions, reason);
}
+2 -2
View File
@@ -98,14 +98,14 @@ function processEditChannelQueue(bot: Bot) {
if (!details) return;
await bot.helpers
.editChannel(bot, details.channelId, details.options)
.editChannel(details.channelId, details.options)
.then((result) => details.resolve(result))
.catch(details.reject);
const secondDetails = request.items.shift();
if (!secondDetails) return;
await bot.helpers
.editChannel(bot, secondDetails.channelId, secondDetails.options)
.editChannel(secondDetails.channelId, secondDetails.options)
.then((result) => secondDetails.resolve(result))
.catch(secondDetails.reject);
return;
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Sets a thread channel to be archived. */
export async function archiveThread(bot: Bot, threadId: bigint) {
return await bot.helpers.editThread(bot, threadId, { archived: true });
return await bot.helpers.editThread(threadId, { archived: true });
}
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Sets a thread channel to be unarchived. */
export async function unarchiveThread(bot: Bot, threadId: bigint) {
return await bot.helpers.editThread(bot, threadId, { archived: false });
return await bot.helpers.editThread(threadId, { archived: false });
}
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Sets a thread channel to be unlocked. */
export async function unlockThread(bot: Bot, threadId: bigint) {
return await bot.helpers.editThread(bot, threadId, { locked: false });
return await bot.helpers.editThread(threadId, { locked: false });
}
+1 -1
View File
@@ -22,7 +22,7 @@ export async function createGuild(bot: Bot, options: CreateGuild) {
// MANUALLY CACHE THE GUILD
await bot.cache.guilds.set(guild.id, guild);
// MANUALLY CACHE THE BOT
await bot.helpers.getMember(bot, guild.id, bot.id);
await bot.helpers.getMember(guild.id, bot.id);
return guild;
}
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../bot.ts";
/** Kicks a member from a voice channel */
export function disconnectMember(bot: Bot, guildId: bigint, memberId: bigint) {
return bot.helpers.editMember(bot, guildId, memberId, { channelId: null });
return bot.helpers.editMember(guildId, memberId, { channelId: null });
}
+1 -1
View File
@@ -8,5 +8,5 @@ import type { Bot } from "../../bot.ts";
* @param channelId id of channel to move user to (if they are connected to voice)
*/
export function moveMember(bot: Bot, guildId: bigint, memberId: bigint, channelId: bigint) {
return bot.helpers.editMember(bot, guildId, memberId, { channelId });
return bot.helpers.editMember(guildId, memberId, { channelId });
}
+1 -1
View File
@@ -25,5 +25,5 @@ export async function sendDirectMessage(bot: Bot, memberId: bigint, content: str
}
// If it does exist try sending a message to this user
return await bot.helpers.sendMessage(bot, dmChannel.id, content);
return await bot.helpers.sendMessage(dmChannel.id, content);
}
+2 -2
View File
@@ -9,11 +9,11 @@ export async function addReactions(
ordered = false
) {
if (!ordered) {
await Promise.all(reactions.map((reaction) => bot.helpers.addReaction(bot, channelId, messageId, reaction)));
await Promise.all(reactions.map((reaction) => bot.helpers.addReaction(channelId, messageId, reaction)));
} else {
for (const reaction of reactions) {
bot.events.debug("Running for of loop in addReactions function.");
await bot.helpers.addReaction(bot, channelId, messageId, reaction);
await bot.helpers.addReaction(channelId, messageId, reaction);
}
}
}
-3
View File
@@ -1,4 +1,3 @@
// import { cacheHandlers } from "../../cache.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
@@ -6,8 +5,6 @@ import type { CreateMessage } from "../../types/messages/create_message.ts";
import type { Message } from "../../types/messages/message.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import type { Bot } from "../../bot.ts";
import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
import { Embed } from "../../types/embeds/embed.ts";
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
export async function sendMessage(bot: Bot, channelId: bigint, content: string | CreateMessage) {
+3 -3
View File
@@ -49,7 +49,7 @@ export async function dispatchRequirements(bot: Bot, data: DiscordGatewayPayload
bot.events.debug(`[DISPATCH] New Guild ID has appeared: ${id} in ${data.t} event`);
const rawGuild = (await bot.helpers
.getGuild(bot, id, {
.getGuild(id, {
counts: true,
addToCache: false,
})
@@ -63,8 +63,8 @@ export async function dispatchRequirements(bot: Bot, data: DiscordGatewayPayload
bot.events.debug(`[DISPATCH] Guild ID ${id} has been found. ${rawGuild.name}`);
const [channels, botMember] = await Promise.all([
bot.helpers.getChannels(bot, id, false),
bot.helpers.getMember(bot, id, bot.id, { force: true }),
bot.helpers.getChannels(id, false),
bot.helpers.getMember(id, bot.id, { force: true }),
]).catch((error) => {
bot.events.debug(error);
return [];