Files
becca-lyria/src/modules/start.ts
T
naomi 04b18472ba
Node.js CI / Lint and Test (push) Successful in 1m4s
Code Analysis / SonarQube (push) Failing after 1m18s
feat: migrate to claude 4 (#1)
### Explanation

_No response_

### Issue

_No response_

### Attestations

- [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [ ] I have pinned the dependencies to a specific patch version.

### Style

- [ ] I have run the linter and resolved any errors.
- [ ] My pull request uses an appropriate title, matching the conventional commit standards.
- [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [ ] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [ ] All new and existing tests pass locally with my changes.
- [ ] Code coverage remains at or above the configured threshold.

### Documentation

_No response_

### Versioning

_No response_

Reviewed-on: #1
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
2025-05-22 11:28:25 -07:00

70 lines
2.1 KiB
TypeScript

/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import {
MessageFlags,
type ChatInputCommandInteraction,
} from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribedInteraction } from "../utils/isSubscribed.js";
import { logger } from "../utils/logger.js";
import { replyToError } from "../utils/replyToError.js";
/**
* Asks the AI to start a new story.
* @param interaction - The interaction payload from Discord.
*/
export const start = async(
interaction: ChatInputCommandInteraction,
): Promise<void> => {
try {
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
const subbed = await isSubscribedInteraction(interaction);
if (!subbed) {
return;
}
const messages = await ai.messages.create({
// eslint-disable-next-line @typescript-eslint/naming-convention -- Required key format for SDK.
max_tokens: 2000,
messages: [
{ content: "Please start a new story for me.", role: "user" },
],
model: "claude-sonnet-4-20250514",
system: `${personality} The user has requested to start a new adventure. You should generate the first message for the game. The user's name is ${interaction.user.displayName}`,
});
const response = messages.content.find((m) => {
return m.type === "text";
});
const sent = response
? await interaction.user.send({
content: response.text,
}).catch(() => {
return null;
})
: null;
await interaction.editReply({
content: sent
? "I have sent you a DM to start your adventure!"
// eslint-disable-next-line stylistic/max-len -- This is a long string.
: "I was unable to send you a DM. Please ensure your privacy settings allow direct messages.",
});
await calculateCost(messages.usage, interaction.user.username);
} catch (error) {
await replyToError(interaction);
if (error instanceof Error) {
await logger.error("about command", error);
}
}
};