From 52793a931d86dae9b2a80d39744a9d73c9cd8856 Mon Sep 17 00:00:00 2001 From: Joehoel <31251240+Joehoel@users.noreply.github.com> Date: Wed, 8 Sep 2021 22:25:00 +0200 Subject: [PATCH 1/3] Typescript fix --- src/commands/typo.ts | 2 +- src/entity/Brain.ts | 10 ++++---- src/entity/Config.ts | 10 ++++---- src/entity/Game.ts | 12 ++++----- src/entity/Guild.ts | 8 +++--- src/entity/Leaderboard.ts | 12 ++++----- src/entity/Question.ts | 10 ++++---- src/entity/Score.ts | 14 +++++------ src/entity/Swear.ts | 10 ++++---- tsconfig.json | 51 ++++++++++++++++++++------------------- 10 files changed, 70 insertions(+), 69 deletions(-) diff --git a/src/commands/typo.ts b/src/commands/typo.ts index fc7fbd8..5b8386d 100644 --- a/src/commands/typo.ts +++ b/src/commands/typo.ts @@ -6,7 +6,7 @@ export default new Command({ description: "Call when a user makes a typo.", aliases: ["tp"], exclusive: true, - execute(client, message, args) { + execute(_, message) { const target = message.mentions.users.first(); message.channel.send({ embeds: [embed({ description: `${target}` })] }); }, diff --git a/src/entity/Brain.ts b/src/entity/Brain.ts index f2cabbb..86cb320 100644 --- a/src/entity/Brain.ts +++ b/src/entity/Brain.ts @@ -3,21 +3,21 @@ import { BaseEntity, Column, CreateDateColumn, Entity, Index, PrimaryGeneratedCo @Entity("brains") export class Brain extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Index() @Column({ unique: true, nullable: false }) - user: string; + user!: string; @Index() @Column({ default: 1 }) - score: number; + score!: number; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Config.ts b/src/entity/Config.ts index a372ef7..c36b2f3 100644 --- a/src/entity/Config.ts +++ b/src/entity/Config.ts @@ -13,20 +13,20 @@ import { Guild } from "./Guild"; @Entity("configs") export class Config extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @OneToOne(() => Guild) @JoinColumn() - guild: Guild; + guild!: Guild; @Column({ default: "!" }) - prefix: string; + prefix!: string; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Game.ts b/src/entity/Game.ts index f877707..45dab52 100644 --- a/src/entity/Game.ts +++ b/src/entity/Game.ts @@ -13,23 +13,23 @@ import { Leaderboard } from "./Leaderboard"; @Entity("games") export class Game extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Index() @Column({ unique: true }) - name: string; + name!: string; @Column({ nullable: true }) - imageUrl: string; + imageUrl!: string; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; @OneToMany(() => Leaderboard, (leaderboard) => leaderboard.game) - leaderboards: Leaderboard[]; + leaderboards!: Leaderboard[]; constructor(model?: Partial) { super(); diff --git a/src/entity/Guild.ts b/src/entity/Guild.ts index 923f3df..cc56866 100644 --- a/src/entity/Guild.ts +++ b/src/entity/Guild.ts @@ -3,17 +3,17 @@ import { BaseEntity, Column, CreateDateColumn, Entity, Index, PrimaryColumn, Upd @Entity("guilds") export class Guild extends BaseEntity { @PrimaryColumn() - id: string; + id!: string; @Index() @Column({ nullable: false }) - ownerId: string; + ownerId!: string; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Leaderboard.ts b/src/entity/Leaderboard.ts index ff6bc31..f2de33c 100644 --- a/src/entity/Leaderboard.ts +++ b/src/entity/Leaderboard.ts @@ -15,24 +15,24 @@ import { Score } from "./Score"; @Entity("leaderboards") export class Leaderboard extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Index() @Column() - name: string; + name!: string; @Index() @ManyToOne(() => Game, (game) => game.leaderboards) - game: Game; + game!: Game; @OneToMany(() => Score, (score) => score.leaderboard) - scores: Score[]; + scores!: Score[]; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Question.ts b/src/entity/Question.ts index 78da19f..f6e889d 100644 --- a/src/entity/Question.ts +++ b/src/entity/Question.ts @@ -3,19 +3,19 @@ import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, U @Entity("questions") export class Question extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Column() - text: string; + text!: string; @Column() - date: Date; + date!: Date; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Score.ts b/src/entity/Score.ts index f448f6b..96db9e6 100644 --- a/src/entity/Score.ts +++ b/src/entity/Score.ts @@ -13,26 +13,26 @@ import { Leaderboard } from "./Leaderboard"; @Entity("scores") export class Score extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Index() @Column() - user: string; + user!: string; @Column() - score: string; + score!: string; @Column({ default: "N/A" }) - proof: string; + proof!: string; @ManyToOne(() => Leaderboard, (leaderboard) => leaderboard.scores) - leaderboard: Leaderboard; + leaderboard!: Leaderboard; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/src/entity/Swear.ts b/src/entity/Swear.ts index 16ea8e2..62493ed 100644 --- a/src/entity/Swear.ts +++ b/src/entity/Swear.ts @@ -3,21 +3,21 @@ import { BaseEntity, Column, CreateDateColumn, Entity, Index, PrimaryGeneratedCo @Entity("swears") export class Swear extends BaseEntity { @PrimaryGeneratedColumn() - id: number; + id!: number; @Index() @Column() - user: string; + user!: string; @Index() @Column({ default: 0 }) - swears: number; + swears!: number; @CreateDateColumn() - createdAt: Date; + createdAt!: Date; @UpdateDateColumn() - updatedAt: Date; + updatedAt!: Date; constructor(model?: Partial) { super(); diff --git a/tsconfig.json b/tsconfig.json index 0ff651a..4f24f2a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,27 +1,28 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": ["esnext", "esnext.array"], - "target": "ES2015", - "sourceMap": true, - "outDir": "./dist", - "baseUrl": "./src", - "moduleResolution": "node", - "esModuleInterop": true, - "allowSyntheticDefaultImports": true, - "noImplicitAny": true, - "strictNullChecks": true, - "alwaysStrict": true, - "strictFunctionTypes": true, - "downlevelIteration": true, - "skipLibCheck": true, - "typeRoots": ["./src/typings", "node_modules/@types"], - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "resolveJsonModule": true, - "paths": { - "@/*": ["./*"] - } - }, - "exclude": ["node_modules"] + "compilerOptions": { + "module": "commonjs", + "lib": ["esnext", "esnext.array"], + "target": "ES2015", + "sourceMap": true, + "outDir": "./dist", + "baseUrl": "./src", + "moduleResolution": "node", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "noImplicitAny": true, + "strictNullChecks": true, + "alwaysStrict": true, + "strictFunctionTypes": true, + "downlevelIteration": true, + "skipLibCheck": true, + "strict": true, + "typeRoots": ["./src/typings", "node_modules/@types"], + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "resolveJsonModule": true, + "paths": { + "@/*": ["./*"] + } + }, + "exclude": ["node_modules"] } From 2b48bc30fd7380ca2b5711c4f110477d1010323e Mon Sep 17 00:00:00 2001 From: Joehoel <31251240+Joehoel@users.noreply.github.com> Date: Wed, 15 Sep 2021 09:56:53 +0200 Subject: [PATCH 2/3] feat: :sparkles: Send anwers the next morning Making it so the bot sends the correct answer from the previous days' question --- src/data/antwoorden.txt | 120 +++++++++++++++++++++++++++++++++++++ src/entity/Answer.ts | 24 ++++++++ src/features/command.ts | 3 +- src/features/playground.ts | 9 +++ src/lib/helpers.ts | 22 ++++++- src/lib/lyrics.ts | 2 +- 6 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 src/data/antwoorden.txt create mode 100644 src/entity/Answer.ts diff --git a/src/data/antwoorden.txt b/src/data/antwoorden.txt new file mode 100644 index 0000000..2a914e4 --- /dev/null +++ b/src/data/antwoorden.txt @@ -0,0 +1,120 @@ +Zeilsport +San Francisco Giants +Gucci +Wit +HUB +Dr. Alban +Octrooi of Patent +Bij een UV-Index van 5 +Denemarken, Noorwegen en Zweden +Blaffen +Boston +Celtic +C3PO en R2D2 +Wie is het? +Karnen +Fluitje, Vaasje, Jonker, Pul +Lady GaGa +Wolf +Cache +Geel +Utrecht (1385,02 km2) +Noordelijker +George Baker Selection +Noordpool +Cyaan, Yellow, Magenta en Key (Key = Zwart) +Rotterdam +Scarface +Toog +Basketbal +Elton John +Via natuurlijke ingang in lichaam kijken +Nintendo Game Cube +Cholesterol +Tilde +Twee keer +Steeplechase +Unisex +Belarus +Gioacchino Rossini +Schilder en Keizer +Nikkel +Gnocchi +Accent Grave +Niet Waar +De Unie van Socialistische Sovjetrepublieken +Johan Neeskens +Hurling +Vlaggetjesdag +Martin Brozius +Aardappel +Vincent van Gogh +Glazen vlekvrij afdrogen +BAFTA +Luchtwegen +Lift +Michael Chrichton +Gelderland (Apeldoorn) +Helsinki +Brahma +Benetton +Nurburgring +Repetitive Strain Injury +Willeke Alberi +27 +Sneeuwwitje en de Zeven Dwergen +Roland Garros +Zuid Holland, Zeeland en Limburg +Drenthe (Emmen) +50 km +(Staats)Doema +Esdoorn +Zaalvoetbal +Disk Operating System +Kaneel +Philips en Grundig +.za +Sesamzaad/sesampasta +Oostelijk halfrond +Radar Love, Golden Earring +Koning en Toren +Donna Karen (Donna Faske) +Zeven meter +Horeca +Goldeneye +Koe/Rund +Paul Gauguin +Japanse Zee +Oranje, zwart, wit +Java +Rood +In het zuiden +Green Day +AardappelNegen +Doetinchem +Terschelling, Vlieland +Een anker +Jacques Brel +funda.nl +Valencia +Zweden +Twaalf +Kathmandu +Ziekte van Lyme +Het Dollarteken +Nepal +Katachtig roofdier +Copenhagen, Brussel en Amsterdam +Hans Teeuwen +Scooter +Flight Data Recorder +Bowser +Mormonen +RotterdamArmani +Vier +The Viper Room +3,5 inch (90mm) +Farfalle +Gecastreerde Haan +12 +Kalk diff --git a/src/entity/Answer.ts b/src/entity/Answer.ts new file mode 100644 index 0000000..44828f8 --- /dev/null +++ b/src/entity/Answer.ts @@ -0,0 +1,24 @@ +import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm"; + +@Entity("answers") +export class Answer extends BaseEntity { + @PrimaryGeneratedColumn() + id!: number; + + @Column() + text!: string; + + @Column() + date!: Date; + + @CreateDateColumn() + createdAt!: Date; + + @UpdateDateColumn() + updatedAt!: Date; + + constructor(model?: Partial) { + super(); + Object.assign(this, model); + } +} diff --git a/src/features/command.ts b/src/features/command.ts index 0d0b8df..c2de0db 100644 --- a/src/features/command.ts +++ b/src/features/command.ts @@ -1,3 +1,4 @@ +import { NotInVoice } from "@/lib/errors"; import { Client, Message } from "discord.js"; import { embed, isAllowed } from "../lib/helpers"; const { PREFIX } = process.env; @@ -66,7 +67,7 @@ export default async (client: Client, message: Message) => { command.execute(client, message, args); return; } catch (error) { - if (error.message === "NotInVoice") { + if (error instanceof NotInVoice && error.message === "NotInVoice") { return message.channel.send({ embeds: [ embed({ title: "Error", description: "You must be in a voice channel to execute that command!" }), diff --git a/src/features/playground.ts b/src/features/playground.ts index 5ff829e..394a350 100644 --- a/src/features/playground.ts +++ b/src/features/playground.ts @@ -1,6 +1,15 @@ import { Client } from "discord.js"; +import { readdirSync, readFileSync } from "fs"; +import { join } from "path"; // eslint-disable-next-line @typescript-eslint/no-unused-vars export default async (client: Client) => { // * playground + const answers = readFileSync(join(__dirname, "../data/antwoorden.txt"), "utf-8") + .split("\n") + .map((answer) => answer.trim()); + + answers.map((answer, index) => { + const date = new Date(2021); + }); }; diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 36736c2..36931f7 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -1,3 +1,4 @@ +import { Answer } from "@/entity/Answer"; import "cross-fetch"; import fetch from "cross-fetch"; import { @@ -256,10 +257,14 @@ export const scoreboard = async () => { ).format(new Date())})`; }; -const getDate = () => { +const getQuestionDate = () => { const now = new Date(); return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 11, 0, 0, 0).toUTCString(); }; +const getAnswerDate = () => { + const now = new Date(); + return new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1, 11, 0, 0, 0).toUTCString(); +}; export const sendQuestion = async (client: Client) => { const question = await getQuestion(); @@ -271,5 +276,18 @@ export const sendQuestion = async (client: Client) => { }; export const getQuestion = async () => { - return await Question.findOne({ where: { date: getDate() } }); + return await Question.findOne({ where: { date: getQuestionDate() } }); +}; + +export const sendAnswer = async (client: Client) => { + const answer = await getAnswer(); + const channel = (await client.channels.fetch(CHANNELS.VRAGEN, { + cache: true, + force: true, + })) as TextChannel; + return channel.send(`||${answer!.text}||`); +}; + +export const getAnswer = async () => { + return await Answer.findOne({ where: { data: getAnswerDate() } }); }; diff --git a/src/lib/lyrics.ts b/src/lib/lyrics.ts index 6ea001b..9fe4a58 100644 --- a/src/lib/lyrics.ts +++ b/src/lib/lyrics.ts @@ -80,7 +80,7 @@ export async function getJson(path: string, params: Record = {}, he const { data } = await axios.get(url, { params, headers }); return data; } catch (error) { - throw new Error(error); + // throw new Error(error); } } From 8bbc8d84751b6be16155ee4c63eb554d4b7c28e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Kuijper?= Date: Wed, 15 Sep 2021 12:42:58 +0200 Subject: [PATCH 3/3] feat: :sparkles: Answer of the day feature Send answer of the previous day --- src/features/playground.ts | 21 ++++++++++++++------- src/features/quiz.ts | 3 ++- src/lib/helpers.ts | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/features/playground.ts b/src/features/playground.ts index 394a350..c3fd06c 100644 --- a/src/features/playground.ts +++ b/src/features/playground.ts @@ -1,15 +1,22 @@ +import { Answer } from "../entity/Answer"; import { Client } from "discord.js"; import { readdirSync, readFileSync } from "fs"; import { join } from "path"; +import { sendAnswer } from "../lib/helpers"; // eslint-disable-next-line @typescript-eslint/no-unused-vars export default async (client: Client) => { // * playground - const answers = readFileSync(join(__dirname, "../data/antwoorden.txt"), "utf-8") - .split("\n") - .map((answer) => answer.trim()); - - answers.map((answer, index) => { - const date = new Date(2021); - }); + // const answers = readFileSync(join(__dirname, "../data/antwoorden.txt"), "utf-8") + // .split("\n") + // .map((answer) => answer.trim()); + // answers.map(async (answer, index) => { + // const now = new Date(); + // const date = new Date(now.getFullYear(), now.getMonth(), now.getDate() + index - 14, 9, 0, 0, 0); + // const newAnswer = new Answer({ + // date, + // text: answer, + // }); + // await newAnswer.save(); + // }); }; diff --git a/src/features/quiz.ts b/src/features/quiz.ts index 468c355..de22bbd 100644 --- a/src/features/quiz.ts +++ b/src/features/quiz.ts @@ -1,9 +1,10 @@ -import { sendQuestion } from "@/lib/helpers"; +import { sendAnswer, sendQuestion } from "@/lib/helpers"; import { CronJob } from "cron"; import { Client } from "discord.js"; export default async (client: Client) => { const job = new CronJob("0 9 * * *", async () => { + await sendAnswer(client); await sendQuestion(client); }); job.start(); diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 36931f7..11b5dfb 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -1,4 +1,4 @@ -import { Answer } from "@/entity/Answer"; +import { Answer } from "../entity/Answer"; import "cross-fetch"; import fetch from "cross-fetch"; import { @@ -289,5 +289,5 @@ export const sendAnswer = async (client: Client) => { }; export const getAnswer = async () => { - return await Answer.findOne({ where: { data: getAnswerDate() } }); + return await Answer.findOne({ where: { date: getAnswerDate() } }); };