Skip to content

Commit

Permalink
feat(feedback): add feedback command
Browse files Browse the repository at this point in the history
  • Loading branch information
jwford committed Apr 14, 2024
1 parent f27d268 commit 37c5ee0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
9 changes: 9 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ datasource db {
url = env("DATABASE_URL")
}

model Feedback {
id String @id @default(auto()) @map("_id") @db.ObjectId
content String
timestamp DateTime
user String
@@map("feedback")
}

model Guild {
id String @id @default(auto()) @map("_id") @db.ObjectId
id_ String @unique @map("id") // snowflake
Expand Down
77 changes: 77 additions & 0 deletions src/commands/System/feedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { SteveCommand } from "#lib/structures/commands/SteveCommand";
import { registerBasicCommand } from "#utils/util";
import { ApplyOptions } from "@sapphire/decorators";
import {
ApplicationCommandRegistry,
CommandOptions,
} from "@sapphire/framework";
import { TFunction, fetchT } from "@sapphire/plugin-i18next";
import { Time } from "@sapphire/timestamp";
import {
ActionRowBuilder,
ChatInputCommandInteraction,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
} from "discord.js";

@ApplyOptions<CommandOptions>({
description: "Send feedback to my developers.",
})
export default class extends SteveCommand {
public override registerApplicationCommands(
registry: ApplicationCommandRegistry,
) {
registerBasicCommand(registry, this.name, this.description);
}

public override async chatInputRun(interaction: ChatInputCommandInteraction) {
const t = await fetchT(interaction);

try {
await this.sendFeedbackModal(interaction, t);

const submissionData = await interaction.awaitModalSubmit({
time: Time.Minute * 5,
});

const feedback =
submissionData.fields.getTextInputValue("feedback_input");

await this.container.settings.feedback.addFeedback(
feedback,
submissionData.createdAt,
submissionData.user.id,
);

return submissionData.reply(t("commands/feedback:submissionSuccess"));
} catch (e) {
console.log(e); // TODO: set up logger plugin
return interaction.reply(t("commands/feedback:submissionFailure"));
}
}

private sendFeedbackModal(
interaction: ChatInputCommandInteraction,
t: TFunction,
) {
const input = new TextInputBuilder()
.setCustomId("feedback_input")
.setLabel(t("commands/feedback:modal.input.label"))
.setMaxLength(1900)
.setPlaceholder(t("commands/feedback:modal.input.placeholder"))
.setRequired(true)
.setStyle(TextInputStyle.Paragraph);

const actionRow = new ActionRowBuilder<TextInputBuilder>().addComponents(
input,
);

const modal = new ModalBuilder()
.setCustomId("feedback_modal")
.setTitle(t("commands/feedback:modal.title"))
.addComponents(actionRow);

return interaction.showModal(modal);
}
}
11 changes: 11 additions & 0 deletions src/languages/en-US/commands/feedback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"modal": {
"input": {
"label": "What feedback do you have for us?",
"placeholder": "Help make Steve better!"
},
"title": "Feedback"
},
"submissionFailure": "There was a problem submitting your feedback, please try again later!",
"submissionSuccess": "Thanks for your feedback!"
}
24 changes: 24 additions & 0 deletions src/lib/database/FeedbackSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PrismaClient } from "@prisma/client";
import { Snowflake } from "discord.js";

export default class FeedbackSettings {
private prisma: PrismaClient;

public constructor(prisma: PrismaClient) {
this.prisma = prisma;
}

public addFeedback(
content: string,
timestamp: Date,
userSnowflake: Snowflake,
) {
return this.prisma.feedback.create({
data: {
content,
timestamp,
user: userSnowflake,
},
});
}
}
3 changes: 3 additions & 0 deletions src/lib/database/SettingsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { PrismaClient } from "@prisma/client";
import GuildSettings from "./GuildSettings.js";
import FeedbackSettings from "./FeedbackSettings.js";

export default class SettingsProvider {
public feedback: FeedbackSettings;
public guilds: GuildSettings;

private prisma: PrismaClient;

public constructor() {
this.prisma = new PrismaClient();
this.feedback = new FeedbackSettings(this.prisma);
this.guilds = new GuildSettings(this.prisma);
}
}

0 comments on commit 37c5ee0

Please sign in to comment.