Add user data caching

This commit is contained in:
2022-06-06 20:11:07 +07:00
parent aa268b584d
commit 22f711dc80
5 changed files with 98 additions and 25 deletions
+62 -14
View File
@@ -1,38 +1,51 @@
import { Guild } from '@prisma/client';
import { User, Guild } from '@prisma/client';
import { Snowflake } from 'discord-api-types/v10';
import Prisma from './Prisma';
class Cache {
private cache: any;
private cache: {
Guilds: { [key: Snowflake]: Guild };
Users: { [key: Snowflake]: User };
};
constructor() {
this.cache = {
Guilds: {}
Guilds: {},
Users: {}
};
}
public async updateGuildsCache() {
const Guilds = await Prisma.client.guild.findMany();
for (let Guild of Guilds) {
this.setGuildData(Guild.id, Guild);
for (let guild of Guilds) {
this.setGuildCache(guild.id, guild);
}
}
public async updateGuildCache(guildID: Snowflake) {
const DBGuild = await Prisma.client.guild.findFirst({ where: { id: guildID } });
public async updateGuildCache(id: Snowflake) {
const DBGuild = await Prisma.client.guild.findFirst({ where: { id: id } });
// TODO: Try to fetch guild, create if not found or throw error
if (DBGuild === null) return;
this.setGuildData(guildID, DBGuild);
this.setGuildCache(id, DBGuild);
}
public setGuildData(id: string, data: Object): void {
this.cache.Guilds[id] = data;
public async updateUserCache(id: Snowflake) {
let user = await Prisma.client.user.findUnique({ where: { id } });
if (!user) {
user = await Prisma.client.user.create({
data: {
id
}
});
}
this.setUserCache(user.id, user);
}
public async getGuild(id: string): Promise<Guild | undefined> {
public async getCachedGuild(id: Snowflake): Promise<Guild | undefined> {
if (typeof this.cache.Guilds[id] !== 'undefined') return this.cache.Guilds[id];
const Guild = await Prisma.client.guild.findFirst({
const Guild = await Prisma.client.guild.findUnique({
where: {
id: id
}
@@ -40,13 +53,48 @@ class Cache {
if (Guild === null) return undefined;
this.setGuildData(Guild.id, Guild);
this.setGuildCache(Guild.id, Guild);
return this.cache.Guilds[id];
}
public getGuilds(): void {
public async getCachedUser(id: Snowflake): Promise<User | undefined> {
if (typeof this.cache.Users[id] !== 'undefined') return this.cache.Users[id];
const user = await Prisma.client.user.findUnique({
where: {
id: id
}
});
if (user === null) return undefined;
this.setUserCache(user.id, user);
return this.cache.Users[id];
}
public setGuildCache(id: Snowflake, data: Guild): void {
this.cache.Guilds[id] = data;
}
public setUserCache(id: Snowflake, data: User): void {
this.cache.Users[id] = data;
}
public isGuildCached(id: Snowflake) {
return typeof this.cache.Guilds[id] !== 'undefined';
}
public isUserCached(id: Snowflake) {
return typeof this.cache.Users[id] !== 'undefined';
}
public getCachedGuilds(): Object {
return this.cache.Guilds;
}
public getCachedUsers(): Object {
return this.cache.Users;
}
}
export default new Cache();
+12 -7
View File
@@ -124,7 +124,11 @@ class Discord {
});
// Interaction create event to modules
this.client.on('interactionCreate', (interaction: Interaction) => {
this.client.on('interactionCreate', async (interaction: Interaction) => {
if(!Cache.isUserCached(interaction.user.id))
await Cache.updateUserCache(interaction.user.id);
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
@@ -175,7 +179,7 @@ class Discord {
if (message.author.bot) return;
if (typeof message.guild?.id === 'undefined') return;
let GuildCache = await Cache.getGuild(message.guild.id);
let GuildCache = await Cache.getCachedGuild(message.guild.id);
if (typeof GuildCache === 'undefined' || typeof GuildCache.prefix === 'undefined')
return;
@@ -243,15 +247,10 @@ class Discord {
} else {
if (noPrefixMessage.charAt(0) !== ' ') return;
}
//if(noPrefixMessage.charAt(0) !== ' ' && !symbols.includes(noPrefixMessage.charAt(0))) return;
}
if (noPrefixMessage === '') return;
if (noPrefixMessage.charAt(0) === ' ') {
/*if(symbols.includes(GuildCache.prefix.charAt(GuildCache.prefix.length - 1))) {
if((GuildCache.prefix.indexOf(' ') >= 0)) return;
}*/
noPrefixMessage = noPrefixMessage.substring(1);
}
@@ -264,6 +263,9 @@ class Discord {
if (args.length === 0) args = [];
if(!Cache.isUserCached(message.author.id))
await Cache.updateUserCache(message.author.id);
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
thisModule.GuildOnCommand(command, args, message);
@@ -294,6 +296,9 @@ class Discord {
if (args.length === 0) args = [];
if(!Cache.isUserCached(message.author.id))
await Cache.updateUserCache(message.author.id);
for (const module of this.loaded_module) {
let thisModule: DiscordModule = module[1];
thisModule.GuildOnCommand(command, args, message);