Creating AI Commands
Creating AI commands
AI commands can be created by exporting a function called ai
from your regular command file. You can also export aiConfig
object along with the ai
function to specify the parameters for the command.
The ai
function behaves similar to a message
command, but it receives an additional ctx.ai
property (which is an instance of AiContext
) that contains the parameters passed by the AI model.
src/app/commands/balance.ts
import { ApplicationCommandOptionType } from 'discord.js';
import { CommandData, ChatInputCommand } from 'commandkit';
import { AiConfig, AiCommand } from '@commandkit/ai';
import { z } from 'zod';
export const command: CommandData = {
name: 'balance',
description: 'Get the current balance of the user.',
options: [
{
name: 'user',
description: 'The user to get the balance for.',
type: ApplicationCommandOptionType.User,
},
],
};
export const aiConfig: AiConfig = {
parameters: z.object({
userId: z.string().describe('The ID of the user to get the balance of'),
}),
};
export const chatInput: ChatInputCommand = async (ctx) => {
const { interaction } = ctx;
const user = interaction.options.getUser('user');
const balance = await db.getBalance(user.id);
await interaction.reply({
content: `The balance of ${user.username} is ${balance}`,
});
};
// AI will call this function to get the balance of the user
export const ai: AiCommand<typeof aiConfig> = async (ctx) => {
const { userId } = ctx.ai.params;
const balance = await db.getBalance(userId);
// return object with the balance
return {
userId,
balance,
};
};
Now, you can simply mention the bot in a message to get the balance of the user. Eg:
@bot what is the balance of @user?
AI can also call multiple commands in a single message. Eg:
@bot show me the basic details of the user @user and also include the balance of that user.
The above prompt will call the built-in getUserInfo
tool and the balance
command.
Example of AI creating a poll
This is an example of how AI can create a poll using the createPoll
command. The AI will generate a poll based on the user's input.