-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
291 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
SlashCommandProps, | ||
CommandData, | ||
unstable_cacheTag as cacheTag, | ||
} from 'commandkit'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
import { database } from '../../database/store'; | ||
|
||
export const data: CommandData = { | ||
name: 'xp', | ||
description: 'This is an xp command.', | ||
}; | ||
|
||
async function getUserXP(guildId: string, userId: string) { | ||
'use cache'; | ||
|
||
cacheTag(`xp:${guildId}:${userId}`); | ||
|
||
const xp: number = (await database.get(`${guildId}:${userId}`)) ?? 0; | ||
|
||
return xp; | ||
} | ||
|
||
export async function run({ interaction }: SlashCommandProps) { | ||
await interaction.deferReply(); | ||
|
||
const dataRetrievalStart = Date.now(); | ||
const xp = await getUserXP(interaction.guildId!, interaction.user.id); | ||
const dataRetrievalEnd = Date.now() - dataRetrievalStart; | ||
|
||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: 'XP', | ||
description: `Hello ${interaction.user}, your xp is ${xp}.`, | ||
color: 0x7289da, | ||
timestamp: new Date().toISOString(), | ||
footer: { | ||
text: `Data retrieval took ${dataRetrievalEnd}ms`, | ||
}, | ||
}, | ||
], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
// Simulate a random latency between 30ms to 1.5s | ||
const randomLatency = () => setTimeout(Math.floor(Math.random() * 1500) + 30); | ||
|
||
class DataStore { | ||
private store = new Map<string, any>(); | ||
|
||
async get(key: string) { | ||
await randomLatency(); | ||
return this.store.get(key); | ||
} | ||
|
||
async set(key: string, value: any) { | ||
await randomLatency(); | ||
this.store.set(key, value); | ||
} | ||
|
||
async delete(key: string) { | ||
await randomLatency(); | ||
this.store.delete(key); | ||
} | ||
|
||
async clear() { | ||
await randomLatency(); | ||
this.store.clear(); | ||
} | ||
} | ||
|
||
export const database = new DataStore(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Message } from 'discord.js'; | ||
import { unstable_revalidate as revalidate } from 'commandkit'; | ||
import { database } from '../../database/store'; | ||
|
||
export default async function (message: Message) { | ||
if (message.author.bot || !message.inGuild()) return; | ||
|
||
const oldXp = | ||
(await database.get(`${message.guildId}:${message.author.id}`)) ?? 0; | ||
const xp = Math.floor(Math.random() * 10) + 1; | ||
|
||
await database.set(`${message.guildId}:${message.author.id}`, oldXp + xp); | ||
|
||
// revalidate the cache | ||
await revalidate(`xp:${message.guildId}:${message.author.id}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.