-
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.
feat: context, cache, macro and more
- Loading branch information
Showing
22 changed files
with
958 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.env | ||
.env | ||
.commandkit |
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,12 @@ | ||
import { SlashCommandProps, CommandData, dmOnly } from 'commandkit'; | ||
|
||
export const data: CommandData = { | ||
name: 'dm-only', | ||
description: 'This is a dm only command', | ||
}; | ||
|
||
export async function run({ interaction }: SlashCommandProps) { | ||
dmOnly(); | ||
|
||
await interaction.reply('This command can only be used in a dm.'); | ||
} |
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,12 @@ | ||
import { SlashCommandProps, CommandData, guildOnly } from 'commandkit'; | ||
|
||
export const data: CommandData = { | ||
name: 'guild-only', | ||
description: 'This is a guild only command.', | ||
}; | ||
|
||
export async function run({ interaction }: SlashCommandProps) { | ||
guildOnly(); | ||
|
||
await interaction.reply('This command can only be used in a guild.'); | ||
} |
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,43 @@ | ||
import { unstable_cache, SlashCommandProps, CommandData } from 'commandkit'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
export const data: CommandData = { | ||
name: 'help', | ||
description: 'This is a help command.', | ||
}; | ||
|
||
function $botVersion() { | ||
'use macro'; | ||
// this function is inlined in production build | ||
const process = require('node:process'); | ||
return require(`${process.cwd()}/package.json`).version; | ||
} | ||
|
||
async function someExpensiveDatabaseCall() { | ||
await setTimeout(3000); | ||
return Date.now(); | ||
} | ||
|
||
export async function run({ interaction }: SlashCommandProps) { | ||
await unstable_cache({ name: interaction.commandName, ttl: 60_000 }); | ||
|
||
await interaction.deferReply(); | ||
|
||
const time = await someExpensiveDatabaseCall(); | ||
|
||
const version = $botVersion(); | ||
|
||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: 'Help', | ||
description: `This is a help command. The current time is \`${time}\``, | ||
color: 0x7289da, | ||
footer: { | ||
text: `Bot Version: ${version}`, | ||
}, | ||
timestamp: new Date().toISOString(), | ||
}, | ||
], | ||
}); | ||
} |
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,20 @@ | ||
import { SlashCommandProps, CommandData, after } from 'commandkit'; | ||
|
||
export const data: CommandData = { | ||
name: 'run-after', | ||
description: 'This is a run-after command', | ||
}; | ||
|
||
export async function run({ interaction }: SlashCommandProps) { | ||
after((env) => { | ||
console.log( | ||
`The command ${interaction.commandName} was executed successfully in ${env | ||
.getExecutionTime() | ||
.toFixed(2)} milliseconds!`, | ||
); | ||
}); | ||
|
||
await interaction.reply( | ||
'Hello, you will see a new message printed in your console after this command runs.', | ||
); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export abstract class CacheProvider { | ||
abstract get<T>(key: string): Promise<T | undefined>; | ||
abstract set<T>(key: string, value: T, ttl?: number): Promise<void>; | ||
abstract exists(key: string): Promise<boolean>; | ||
abstract delete(key: string): Promise<void>; | ||
abstract clear(): Promise<void>; | ||
} |
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,46 @@ | ||
import { CacheProvider } from './CacheProvider'; | ||
|
||
export interface MemoryCacheEntry { | ||
key: string; | ||
value: any; | ||
ttl?: number; | ||
} | ||
|
||
export class MemoryCache extends CacheProvider { | ||
#cache = new Map<string, MemoryCacheEntry>(); | ||
|
||
public async get<T>(key: string): Promise<T | undefined> { | ||
const entry = this.#cache.get(key); | ||
|
||
if (!entry) { | ||
return undefined; | ||
} | ||
|
||
if (entry.ttl && Date.now() > entry.ttl) { | ||
this.#cache.delete(key); | ||
return undefined; | ||
} | ||
|
||
return entry.value; | ||
} | ||
|
||
public async set<T>(key: string, value: T, ttl?: number): Promise<void> { | ||
this.#cache.set(key, { | ||
key, | ||
value, | ||
ttl: ttl != null ? Date.now() + ttl : undefined, | ||
}); | ||
} | ||
|
||
public async exists(key: string): Promise<boolean> { | ||
return this.#cache.has(key); | ||
} | ||
|
||
public async delete(key: string): Promise<void> { | ||
this.#cache.delete(key); | ||
} | ||
|
||
public async clear(): Promise<void> { | ||
this.#cache.clear(); | ||
} | ||
} |
Oops, something went wrong.