refactor: typings using ReturnType (#2105)

* fix: check new types idea

* fix: type errors

* fix: new style

* fix: more cleanup

* fix: more cleanup

* fix: cleanup audit logs

* fix: cleanup stickers

* fix: cleanup integrations

* fix: more cleanup

* fix: organize into 1 place

* fix: few errors

* fix: some broken import fixes

* fix: quite a lot of fixes across the board

* fix: more fixes for broken imports

* fix: more fixes for broken imports

* fix: handler imports

* fix: all remaining import errors

* fix: more errors needing fixes

* fix: clearing up transformers

* fix: few moer types

* fix: more cleanup of extra types

* fix: fmt

* fix: cleanup discordeno file

* Nuke Base Types (#2102)

* fix: cleanup snake stuff

* convert camelCase to snake_case (#2103)

* fix: add camelize

* fix: finalize remaining errors

* fix: imports in test

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
This commit is contained in:
Skillz4Killz
2022-03-14 22:11:22 -04:00
committed by GitHub
co-authored by LTS20050703
parent da29ff294d
commit a0a1554756
505 changed files with 5646 additions and 7105 deletions
+12 -12
View File
@@ -1,23 +1,23 @@
import {
Bot,
Collection,
DiscordenoChannel,
DiscordenoGuild,
DiscordenoMember,
DiscordenoMessage,
DiscordenoPresence,
DiscordenoUser,
Channel,
Guild,
Member,
Message,
PresenceUpdate,
User,
} from "../deps.ts";
export type BotWithCache<B extends Bot = Bot> = B & CacheProps;
export interface CacheProps extends Bot {
guilds: Collection<bigint, DiscordenoGuild>;
users: Collection<bigint, DiscordenoUser>;
members: Collection<bigint, DiscordenoMember>;
channels: Collection<bigint, DiscordenoChannel>;
messages: Collection<bigint, DiscordenoMessage>;
presences: Collection<bigint, DiscordenoPresence>;
guilds: Collection<bigint, Guild>;
users: Collection<bigint, User>;
members: Collection<bigint, Member>;
channels: Collection<bigint, Channel>;
messages: Collection<bigint, Message>;
presences: Collection<bigint, PresenceUpdate>;
dispatchedGuildIds: Set<bigint>;
dispatchedChannelIds: Set<bigint>;
activeGuildIds: Set<bigint>;
+2 -2
View File
@@ -1,11 +1,11 @@
import { Bot, GatewayPayload } from "../deps.ts";
import { Bot, DiscordGatewayPayload } from "../deps.ts";
import { BotWithCache } from "./addCacheCollections.ts";
const processing = new Set<bigint>();
export async function dispatchRequirements<B extends Bot>(
bot: BotWithCache<B>,
data: GatewayPayload,
data: DiscordGatewayPayload,
) {
// DELETE MEANS WE DONT NEED TO FETCH. CREATE SHOULD HAVE DATA TO CACHE
if (data.t && ["GUILD_CREATE", "GUILD_DELETE"].includes(data.t)) return;
+6 -14
View File
@@ -1,12 +1,4 @@
import type {
Bot,
GuildMemberAdd,
GuildMemberRemove,
MessageReactionAdd,
MessageReactionRemove,
MessageReactionRemoveAll,
SnakeCasedPropertiesDeep,
} from "../deps.ts";
import type { Bot, DiscordGuildMemberAdd, DiscordGuildMemberRemove, DiscordMessageReactionAdd, DiscordMessageReactionRemove, DiscordMessageReactionRemoveAll } from "../deps.ts";
import type { BotWithCache } from "./addCacheCollections.ts";
export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
@@ -19,7 +11,7 @@ export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
} = bot.handlers;
bot.handlers.GUILD_MEMBER_ADD = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildMemberAdd>;
const payload = data.d as DiscordGuildMemberAdd;
const guild = bot.guilds.get(bot.transformers.snowflake(payload.guild_id));
@@ -29,7 +21,7 @@ export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.GUILD_MEMBER_REMOVE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildMemberRemove>;
const payload = data.d as DiscordGuildMemberRemove;
const guild = bot.guilds.get(bot.transformers.snowflake(payload.guild_id));
@@ -39,7 +31,7 @@ export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.MESSAGE_REACTION_ADD = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<MessageReactionAdd>;
const payload = data.d as DiscordMessageReactionAdd;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = bot.messages.get(messageId);
@@ -74,7 +66,7 @@ export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.MESSAGE_REACTION_REMOVE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<MessageReactionRemove>;
const payload = data.d as DiscordMessageReactionRemove;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = bot.messages.get(messageId);
@@ -105,7 +97,7 @@ export function setupCacheEdits<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.MESSAGE_REACTION_REMOVE_ALL = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<MessageReactionRemoveAll>;
const payload = data.d as DiscordMessageReactionRemoveAll;
const messageId = bot.transformers.snowflake(payload.message_id);
const message = bot.messages.get(messageId);
+16 -16
View File
@@ -1,15 +1,15 @@
import {
Bot,
Channel,
Collection,
GuildBanAddRemove,
GuildEmojisUpdate,
GuildMemberRemove,
GuildRoleDelete,
MessageDelete,
MessageDeleteBulk,
DiscordChannel,
DiscordGuildBanAddRemove,
DiscordGuildEmojisUpdate,
DiscordGuildMemberRemove,
DiscordGuildRoleDelete,
DiscordMessageDelete,
DiscordMessageDeleteBulk,
DiscordUnavailableGuild,
} from "../deps.ts";
import { SnakeCasedPropertiesDeep, UnavailableGuild } from "../deps.ts";
import { BotWithCache } from "./addCacheCollections.ts";
export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
@@ -24,7 +24,7 @@ export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
} = bot.handlers;
bot.handlers.GUILD_DELETE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<UnavailableGuild>;
const payload = data.d as DiscordUnavailableGuild;
const id = bot.transformers.snowflake(payload.id);
bot.guilds.delete(id);
@@ -41,7 +41,7 @@ export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.CHANNEL_DELETE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<Channel>;
const payload = data.d as DiscordChannel;
// HANDLER BEFORE DELETING, BECAUSE HANDLER RUNS TRANSFORMER WHICH RECACHES
CHANNEL_DELETE(bot, data, shardId);
@@ -53,19 +53,19 @@ export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.GUILD_MEMBER_REMOVE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildMemberRemove>;
const payload = data.d as DiscordGuildMemberRemove;
bot.members.delete(bot.transformers.snowflake(payload.user.id));
GUILD_MEMBER_REMOVE(bot, data, shardId);
};
bot.handlers.GUILD_BAN_ADD = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildBanAddRemove>;
const payload = data.d as DiscordGuildBanAddRemove;
bot.members.delete(bot.transformers.snowflake(payload.user.id));
GUILD_BAN_ADD(bot, data, shardId);
};
bot.handlers.GUILD_EMOJIS_UPDATE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildEmojisUpdate>;
const payload = data.d as DiscordGuildEmojisUpdate;
const guild = bot.guilds.get(bot.transformers.snowflake(payload.guild_id));
if (guild) {
@@ -79,7 +79,7 @@ export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.MESSAGE_DELETE = function (_, data) {
const payload = data.d as SnakeCasedPropertiesDeep<MessageDelete>;
const payload = data.d as DiscordMessageDelete;
const id = bot.transformers.snowflake(payload.id);
const message = bot.messages.get(id);
bot.events.messageDelete(bot, {
@@ -91,13 +91,13 @@ export function setupCacheRemovals<B extends Bot>(bot: BotWithCache<B>) {
};
bot.handlers.MESSAGE_DELETE_BULK = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<MessageDeleteBulk>;
const payload = data.d as DiscordMessageDeleteBulk;
payload.ids.forEach((id) => bot.messages.delete(bot.transformers.snowflake(id)));
MESSAGE_DELETE_BULK(bot, data, shardId);
};
bot.handlers.GUILD_ROLE_DELETE = function (_, data, shardId) {
const payload = data.d as SnakeCasedPropertiesDeep<GuildRoleDelete>;
const payload = data.d as DiscordGuildRoleDelete;
const guild = bot.guilds.get(
bot.transformers.snowflake(payload.guild_id),
);
+9 -2
View File
@@ -1,4 +1,4 @@
import { Bot } from "../deps.ts";
import { Bot, Member } from "../deps.ts";
import { BotWithCache } from "./addCacheCollections.ts";
import { dispatchRequirements } from "./dispatchRequirements.ts";
@@ -39,13 +39,20 @@ export function enableCacheSweepers<B extends Bot>(bot: BotWithCache<B>) {
bot,
});
const setMember = bot.members.set;
bot.members.set = (id, member) => {
return setMember(id, {
...member,
cachedAt: Date.now(),
} as Member);
};
bot.members.startSweeper({
filter: function memberSweeper(member, _, bot: BotWithCache<B>) {
// Don't sweep the bot else strange things will happen
if (member.id === bot.id) return false;
// Only sweep members who were not active the last 30 minutes
return Date.now() - member.cachedAt > 1800000;
return Date.now() - (member as Member & { cachedAt: number }).cachedAt > 1800000;
},
interval: 300000,
bot,