Skip to content

Commit

Permalink
feat:เรียงความ
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Jul 21, 2022
1 parent 35b0814 commit c1c2581
Show file tree
Hide file tree
Showing 17 changed files with 4,230 additions and 1,973 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ backup

.vscode

# Database

db.sqlite
db.sqlite-journal
# Yarn
cache
install-state.gz
786 changes: 786 additions & 0 deletions .yarn/releases/yarn-3.2.1.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.2.1.cjs
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Notable Changes for Salim Bot will be noted here!

## [4.2.1300] - 2022-07-21

- Migrate to discord.js 14 with Cocoa Discord Utils v2
- NEW! เขียนเรียงความถึงพ่อหลวงเพื่อรับคะแนนสังคม ๙ คะแนน

## [4.1.1193] - 2022-06-30

- Now use PostgreSQL because PostgreSQL logo is elephant, and elephant is national animal of Thailand
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ In case you want to (not recommended)

`git clone https://github.com/Leomotors/Salim-Bot`

- Add your token in DISCORD_TOKEN of .env
- Add required .env parameters

- For the first time, do `npm install` or `yarn install` to install dependencies
and `npm run build` or `yarn build` to build the bot.
Expand All @@ -85,6 +85,8 @@ and `npm run build` or `yarn build` to build the bot.

- Latest nodejs 16+ (discord.js requirements)

- PostgreSQL 12 เราเลือกใช้ Postgres เพราะมีโลโก้เป็นรูปช้าง และช้างเป็นสัตว์ประจำชาติไทย

## 📚 Documents

As This Bot is powered by [s-bot-framework](https://github.com/Leomotors/s-bot-framework)
Expand Down
10 changes: 5 additions & 5 deletions data/activity.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"activities": [
{
"type": "WATCHING",
"type": "Watching",
"name": "TOP NEWS"
},
{
"type": "WATCHING",
"type": "Watching",
"name": "ข่าวในพระราชสำนัก"
},
{
"type": "LISTENING",
"type": "Listening",
"name": "เพลงสรรเสริญพระบารมี"
},
{
"type": "COMPETING",
"type": "Competing",
"name": "รายการประกวดร้องเพลงพระราชนิพนธ์"
},
{
"type": "PLAYING",
"type": "Playing",
"name": "Escape FishStop 3D"
}
]
Expand Down
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "salim-bot",
"version": "4.1.1193",
"version": "4.2.1300",
"description": "This bot is Salim. Whenever you trigger it, It become mad.",
"type": "module",
"main": "dist/bot.js",
Expand All @@ -23,24 +23,25 @@
"homepage": "https://github.com/Leomotors/Salim-Bot",
"devDependencies": {
"@leomotors/scripts": "^3.1.0",
"@trivago/prettier-plugin-sort-imports": "^3.2.0",
"@types/node": "^18.0.0",
"@trivago/prettier-plugin-sort-imports": "^3.3.0",
"@types/node": "^18.0.6",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"eslint": "^8.18.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"eslint": "^8.20.0",
"prettier": "^2.7.1",
"prisma": "^4.0.0",
"prisma": "^4.1.0",
"typescript": "^4.7.4"
},
"dependencies": {
"@discordjs/opus": "^0.7.0",
"@prisma/client": "4.0.0",
"@discordjs/opus": "^0.8.0",
"@prisma/client": "4.1.0",
"chalk": "^5.0.1",
"cocoa-discord-utils": "^1.9.0",
"discord.js": "^13.8.1",
"cocoa-discord-utils": "^2.0.0",
"discord.js": "^14.0.3",
"libsodium-wrappers": "^0.7.10",
"s-bot-framework": "^1.0.430",
"s-bot-framework": "^1.1.444",
"uuid": "^8.3.2"
}
}
},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- ChangeTableName
ALTER TABLE "User" RENAME TO "user";
ALTER TABLE "user" DROP COLUMN "username";

-- CreateTable
CREATE TABLE "profile" (
"user_id" TEXT NOT NULL,
"username" TEXT NOT NULL,
"registered_guild" TEXT NOT NULL,
"last_interact_guild" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "cooldown" (
"userId" TEXT NOT NULL,
"essay_to_dad" TIMESTAMP(3)
);

-- CreateIndex
CREATE UNIQUE INDEX "user_id_key" ON "user"("id");

-- CreateIndex
CREATE UNIQUE INDEX "profile_user_id_key" ON "profile"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "cooldown_userId_key" ON "cooldown"("userId");

-- AddForeignKey
ALTER TABLE "profile" ADD CONSTRAINT "profile_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "cooldown" ADD CONSTRAINT "cooldown_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "essay" (
"userId" TEXT NOT NULL,
"content" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateIndex
CREATE UNIQUE INDEX "essay_userId_key" ON "essay"("userId");

-- AddForeignKey
ALTER TABLE "essay" ADD CONSTRAINT "essay_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
41 changes: 36 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,40 @@ datasource db {
}

model User {
id String @unique
username String? // For DB Inspect
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
socialCredit Float @default(1000) @map("social_credit")
id String @unique
profile Profile?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
socialCredit Float @default(1000) @map("social_credit")
cooldown Cooldown[]
essay Essay[]
@@map("user")
}

model Profile {
userId String @unique @map("user_id")
user User @relation(fields: [userId], references: [id])
username String
registeredGuild String @map("registered_guild")
lastInteractGuild String @map("last_interact_guild")
@@map("profile")
}

model Cooldown {
userId String @unique
user User @relation(fields: [userId], references: [id])
essayToDad DateTime? @map("essay_to_dad")
@@map("cooldown")
}

model Essay {
userId String @unique
user User @relation(fields: [userId], references: [id])
content String
createdAt DateTime @default(now()) @map("created_at")
@@map("essay")
}
1 change: 1 addition & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export enum Actions {
DuplicateTrain = -10,
TrainQuote = 15,
QuizVar = 112,
EssayToDad = 9,
}
2 changes: 1 addition & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const salimCenter = new SlashCenter(
sclient.client,
process.env.GUILD_IDS?.split(",")
);
salimCenter.addCogs(new Salim(), new QuizCog(sclient.client));
salimCenter.addCogs(new Salim(sclient.client), new QuizCog(sclient.client));
salimCenter.useHelpCommand(style);
salimCenter.on("error", async (name, err, ctx) => {
await ctx.channel?.send(
Expand Down
58 changes: 30 additions & 28 deletions src/commands/quiz.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { CocoaEmbed, Loader } from "cocoa-discord-utils";
import { CocoaEmbed, ArrayLoader } from "cocoa-discord-utils";
import { NonEmptyArray } from "cocoa-discord-utils/internal/base";
import { CogSlashClass, SlashCommand } from "cocoa-discord-utils/slash/class";
import { AutoBuilder, CocoaOption } from "cocoa-discord-utils/template";
import {
CogSlashClass,
Param,
SlashCommand,
} from "cocoa-discord-utils/slash/class";

import {
ActionRowBuilder,
Client,
CommandInteraction,
MessageActionRow,
MessageSelectMenu,
SelectMenuBuilder,
SelectMenuInteraction,
} from "discord.js";

Expand Down Expand Up @@ -39,19 +41,21 @@ function trim(str: string, len: number) {
return str;
}

const quizes = Loader.fromFile<Quiz>("Quiz Loader", "./data/quiz.json");
const quizes = ArrayLoader.fromFile<Quiz>("Quiz Loader", "./data/quiz.json");

class QuizManager {
private quiz: Quiz;
private readonly quizId = uuid().split("-")[0]!;
private originalContext: CommandInteraction;
private originalContext: SlashCommand.Context;
private currentChoiceOrder: string[] = [];
private currentIndex = 0;
private score = 0;
ongoing = true;

/** Make Index for Quiz, **WARNING**: This method mutates the class */
makeEmbed(index: number): [CocoaEmbed, MessageActionRow] {
makeEmbed(
index: number
): [CocoaEmbed, ActionRowBuilder<SelectMenuBuilder>] {
this.currentIndex = index;
const questions = this.quiz.questions;
const question = questions[index]!;
Expand All @@ -66,7 +70,7 @@ class QuizManager {
question.question
}**\n\n${choices.map((c) => `- ${c}`).join("\n")}\n`;

const select = new MessageSelectMenu()
const select = new SelectMenuBuilder()
.setCustomId(this.quizId)
.setPlaceholder("คำตอบของคุณ")
.setMinValues(1)
Expand All @@ -81,7 +85,9 @@ class QuizManager {
})
);

const row = new MessageActionRow().addComponents(select);
const row = new ActionRowBuilder<SelectMenuBuilder>().addComponents(
select
);

return [
quiz_style
Expand Down Expand Up @@ -122,7 +128,7 @@ class QuizManager {
const emb = this.summaryEmbed();

await updateUserCredit(
this.originalContext.user,
this.originalContext,
(this.score / this.quiz.questions.length - 0.6) *
Actions.QuizVar
);
Expand Down Expand Up @@ -167,7 +173,7 @@ class QuizManager {
return emb;
}

constructor(ctx: CommandInteraction, index: number) {
constructor(ctx: SlashCommand.Context, index: number) {
this.quiz = quizes.data[index]!;
this.quiz.questions.sort((_, __) => Math.random() - 0.5);
this.originalContext = ctx;
Expand Down Expand Up @@ -203,20 +209,16 @@ export default class QuizCog extends CogSlashClass {
});
}

@SlashCommand(
AutoBuilder("Start the Quiz")
.addStringOption((option) =>
option
.setName("quiz_name")
.setDescription("แบบทดสอบที่คุณต้องการทำ")
.addChoices(...getChoices())
.setRequired(true)
)
.addBooleanOption(CocoaOption("force", "Force creating new quiz"))
)
async quiz(ctx: CommandInteraction) {
const force = ctx.options.getBoolean("force");
const quiz_id = +ctx.options.getString("quiz_name", true);
@SlashCommand("Start the Quiz")
async quiz(
ctx: SlashCommand.Context,
@Param.Choices<Param.String.Type>(getChoices())
@Param.String("แบบทดสอบที่คุณต้องการทำ")
quiz_name: Param.String.Type,
@Param.Boolean("Force creating new quiz", { required: false })
force: Param.Boolean.Nullable
) {
const quiz_id = +quiz_name;

await quizes.initialPromise;

Expand All @@ -229,6 +231,6 @@ export default class QuizCog extends CogSlashClass {

const [emb, row] = this.quizManager[ctx.guildId!]!.makeEmbed(0);

await ctx.reply({ embeds: [emb.toJSON()], components: [row] });
await ctx.reply({ embeds: [emb], components: [row] });
}
}
Loading

0 comments on commit c1c2581

Please sign in to comment.