mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26:07 +00:00
fix redis cache, prefixed commands
This commit is contained in:
parent
580837cdb3
commit
dbad537eae
7
src/cache/adapters/redis.ts
vendored
7
src/cache/adapters/redis.ts
vendored
@ -197,7 +197,9 @@ const isObject = (o: unknown) => {
|
||||
return !!o && typeof o === 'object' && !Array.isArray(o);
|
||||
};
|
||||
|
||||
function toNormal(target: Record<string, any>) {
|
||||
function toNormal(target: Record<string, any>): undefined | Record<string, any> | Record<string, any>[] {
|
||||
if (typeof target.ARRAY_OF === 'string') return JSON.parse(target.ARRAY_OF as string).map(toNormal);
|
||||
if (!Object.keys(target).length) return undefined;
|
||||
const result: Record<string, any> = {};
|
||||
for (const [key, value] of Object.entries(target)) {
|
||||
if (key.startsWith('O_')) {
|
||||
@ -213,7 +215,8 @@ function toNormal(target: Record<string, any>) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function toDb(target: Record<string, any>) {
|
||||
function toDb(target: Record<string, any>): Record<string, any> | Record<string, any>[] {
|
||||
if (Array.isArray(target)) return { ARRAY_OF: JSON.stringify(target.map(toDb)) };
|
||||
const result: Record<string, any> = {};
|
||||
for (const [key, value] of Object.entries(target)) {
|
||||
switch (typeof value) {
|
||||
|
4
src/cache/index.ts
vendored
4
src/cache/index.ts
vendored
@ -335,7 +335,7 @@ export class Cache {
|
||||
}
|
||||
relationshipsData[hashId].push(id);
|
||||
data.guild_id = guildId;
|
||||
allData.push([this[type]!.hashGuildId(id, guildId), this[type]!.parse(data, id, guildId!)]);
|
||||
allData.push([this[type]!.hashGuildId(guildId, id), this[type]!.parse(data, id, guildId!)]);
|
||||
}
|
||||
break;
|
||||
case 'users':
|
||||
@ -422,7 +422,7 @@ export class Cache {
|
||||
}
|
||||
relationshipsData[hashId].push(id);
|
||||
data.guild_id = guildId;
|
||||
allData.push([this[type]!.hashGuildId(id, guildId), this[type]!.parse(data, id, guildId!)]);
|
||||
allData.push([this[type]!.hashGuildId(guildId, id), this[type]!.parse(data, id, guildId!)]);
|
||||
}
|
||||
break;
|
||||
case 'users':
|
||||
|
12
src/cache/resources/default/guild-based.ts
vendored
12
src/cache/resources/default/guild-based.ts
vendored
@ -44,11 +44,11 @@ export class GuildBasedResource<T = any> {
|
||||
}
|
||||
|
||||
get(id: string, guild: string): ReturnCache<(T & { guild_id: string }) | undefined> {
|
||||
return this.adapter.get(this.hashGuildId(id, guild));
|
||||
return this.adapter.get(this.hashGuildId(guild, id));
|
||||
}
|
||||
|
||||
bulk(ids: string[], guild: string): ReturnCache<(T & { guild_id: string })[]> {
|
||||
return fakePromise(this.adapter.get(ids.map(id => this.hashGuildId(id, guild)))).then(x => x.filter(y => y));
|
||||
return fakePromise(this.adapter.get(ids.map(id => this.hashGuildId(guild, id)))).then(x => x.filter(y => y));
|
||||
}
|
||||
|
||||
set(__keys: string, guild: string, data: any): ReturnCache<void>;
|
||||
@ -64,7 +64,7 @@ export class GuildBasedResource<T = any> {
|
||||
).then(() =>
|
||||
this.adapter.set(
|
||||
keys.map(([key, value]) => {
|
||||
return [this.hashGuildId(key, guild), this.parse(value, key, guild)];
|
||||
return [this.hashGuildId(guild, key), this.parse(value, key, guild)];
|
||||
}),
|
||||
),
|
||||
) as void;
|
||||
@ -74,7 +74,7 @@ export class GuildBasedResource<T = any> {
|
||||
patch(__keys: [string, any][], guild: string): ReturnCache<void>;
|
||||
patch(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
|
||||
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
|
||||
return fakePromise(this.adapter.get(keys.map(([key]) => this.hashGuildId(key, guild)))).then(oldDatas =>
|
||||
return fakePromise(this.adapter.get(keys.map(([key]) => this.hashGuildId(guild, key)))).then(oldDatas =>
|
||||
fakePromise(
|
||||
this.addToRelationship(
|
||||
keys.map(x => x[0]),
|
||||
@ -84,7 +84,7 @@ export class GuildBasedResource<T = any> {
|
||||
this.adapter.set(
|
||||
keys.map(([key, value]) => {
|
||||
const oldData = oldDatas.find(x => x.id === key) ?? {};
|
||||
return [this.hashGuildId(key, guild), this.parse({ ...oldData, ...value }, key, guild)];
|
||||
return [this.hashGuildId(guild, key), this.parse({ ...oldData, ...value }, key, guild)];
|
||||
}),
|
||||
),
|
||||
),
|
||||
@ -94,7 +94,7 @@ export class GuildBasedResource<T = any> {
|
||||
remove(id: string | string[], guild: string) {
|
||||
const ids = Array.isArray(id) ? id : [id];
|
||||
return fakePromise(this.removeToRelationship(ids, guild)).then(() =>
|
||||
this.adapter.remove(ids.map(x => this.hashGuildId(x, guild))),
|
||||
this.adapter.remove(ids.map(x => this.hashGuildId(guild, x))),
|
||||
);
|
||||
}
|
||||
|
||||
|
1
src/cache/resources/default/guild-related.ts
vendored
1
src/cache/resources/default/guild-related.ts
vendored
@ -54,7 +54,6 @@ export class GuildRelatedResource<T = any> {
|
||||
set(__keys: [string, any][], guild: string): ReturnCache<void>;
|
||||
set(__keys: string | [string, any][], guild: string, data?: any): ReturnCache<void> {
|
||||
const keys: [string, any][] = Array.isArray(__keys) ? __keys : [[__keys, data]];
|
||||
|
||||
return fakePromise(
|
||||
this.addToRelationship(
|
||||
keys.map(x => x[0]),
|
||||
|
3
src/cache/resources/guilds.ts
vendored
3
src/cache/resources/guilds.ts
vendored
@ -141,9 +141,10 @@ export class Guilds extends BaseResource {
|
||||
}
|
||||
|
||||
for (const channel of data.channels ?? []) {
|
||||
if (channel.permission_overwrites?.length)
|
||||
if (channel.permission_overwrites?.length) {
|
||||
bulkData.push(['overwrites', channel.permission_overwrites, channel.id, id]);
|
||||
}
|
||||
}
|
||||
|
||||
for (const emoji of data.emojis ?? []) {
|
||||
bulkData.push(['emojis', emoji, emoji.id, id]);
|
||||
|
14
src/cache/resources/overwrites.ts
vendored
14
src/cache/resources/overwrites.ts
vendored
@ -7,20 +7,24 @@ import { GuildRelatedResource } from './default/guild-related';
|
||||
export class Overwrites extends GuildRelatedResource {
|
||||
namespace = 'overwrite';
|
||||
|
||||
parse(data: any, _id: string, _guild_id: string) {
|
||||
parse(data: any[], _id: string, guild_id: string) {
|
||||
data.forEach(x => {
|
||||
x.guild_id = guild_id;
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
override get(
|
||||
id: string,
|
||||
): ReturnCache<{ type: number; id: string; deny: PermissionsBitField; allow: PermissionsBitField }[] | undefined> {
|
||||
return fakePromise(super.get(id) as APIOverwrite[] | undefined).then(rawOverwrites =>
|
||||
return fakePromise(super.get(id) as (APIOverwrite & { guild_id: string })[] | undefined).then(rawOverwrites =>
|
||||
rawOverwrites
|
||||
? rawOverwrites.map(rawOverwrite => ({
|
||||
allow: new PermissionsBitField(BigInt(rawOverwrite.allow)),
|
||||
deny: new PermissionsBitField(BigInt(rawOverwrite.deny)),
|
||||
id: rawOverwrite.id,
|
||||
type: rawOverwrite.type,
|
||||
guildId: rawOverwrite.guild_id,
|
||||
}))
|
||||
: undefined,
|
||||
);
|
||||
@ -29,13 +33,14 @@ export class Overwrites extends GuildRelatedResource {
|
||||
override values(
|
||||
guild: string,
|
||||
): ReturnCache<{ type: number; id: string; deny: PermissionsBitField; allow: PermissionsBitField }[][]> {
|
||||
return fakePromise(super.values(guild) as APIOverwrite[][]).then(values =>
|
||||
return fakePromise(super.values(guild) as (APIOverwrite & { guild_id: string })[][]).then(values =>
|
||||
values.map(rawOverwrites =>
|
||||
rawOverwrites.map(rawOverwrite => ({
|
||||
allow: new PermissionsBitField(BigInt(rawOverwrite.allow)),
|
||||
deny: new PermissionsBitField(BigInt(rawOverwrite.deny)),
|
||||
id: rawOverwrite.id,
|
||||
type: rawOverwrite.type,
|
||||
guildId: rawOverwrite.guild_id,
|
||||
})),
|
||||
),
|
||||
);
|
||||
@ -44,13 +49,14 @@ export class Overwrites extends GuildRelatedResource {
|
||||
override bulk(
|
||||
ids: string[],
|
||||
): ReturnCache<{ type: number; id: string; deny: PermissionsBitField; allow: PermissionsBitField }[][]> {
|
||||
return fakePromise(super.bulk(ids) as APIOverwrite[][]).then(values =>
|
||||
return fakePromise(super.bulk(ids) as (APIOverwrite & { guild_id: string })[][]).then(values =>
|
||||
values.map(rawOverwrites =>
|
||||
rawOverwrites.map(rawOverwrite => ({
|
||||
allow: new PermissionsBitField(BigInt(rawOverwrite.allow)),
|
||||
deny: new PermissionsBitField(BigInt(rawOverwrite.deny)),
|
||||
id: rawOverwrite.id,
|
||||
type: rawOverwrite.type,
|
||||
guildId: rawOverwrite.guild_id,
|
||||
})),
|
||||
),
|
||||
);
|
||||
|
@ -37,7 +37,7 @@ function getCommandFromContent(
|
||||
|
||||
if (!(parent instanceof Command)) return { fullCommandName };
|
||||
|
||||
if (groupName && !parent.groups?.[groupName!]) return getCommandFromContent([parentName, subcommandName], self);
|
||||
if (groupName && !parent.groups?.[groupName!]) return getCommandFromContent([parentName, groupName], self);
|
||||
if (subcommandName && !parent.options?.some(x => x instanceof SubCommand && x.name === subcommandName))
|
||||
return getCommandFromContent([parentName], self);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user