Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit e7853ebd402fe51926faa7a628642b2d076459c4
parent 5727a2d7a02bc6a134fcabe63a5309ac9a980a1c
Author: Demonstrandum <moi@knutsen.co>
Date:   Thu, 26 Mar 2020 19:16:08 +0000

I'm the biggest bitch around.

Diffstat:
Mlib/commands/cron.ts | 46+++++++++++++++++++++++-----------------------
Mlib/commands/hangman.ts | 111++++++++++++++++++++++++++++++++++++++++---------------------------------------
2 files changed, 79 insertions(+), 78 deletions(-)

diff --git a/lib/commands/cron.ts b/lib/commands/cron.ts @@ -12,20 +12,20 @@ interface Schedule { month?: string; dayOfWeek?: string; greenwich?: Greenwich; - ordinal?: Ordinal + ordinal?: Ordinal; } interface Command { - name: string, - args?: string[] -}; + name: string; + args?: string[]; +} interface Cron { - id: number, - schedule?: Schedule, - command?: Command + id: number; + schedule?: Schedule; + command?: Command; executed_at?: number; -}; +} const MATCHERS = { hour_mins: @@ -53,7 +53,7 @@ const RESPONSES = { added: (cron: Cron) => `New cron (#${cron.id.toString().format(FORMATS.bold)}) has been added.`, list: (cron: Cron) => { - let { schedule } = cron; + const { schedule } = cron; let result: string = ""; result += `#${cron.id} `.format(FORMATS.bold); @@ -67,7 +67,7 @@ const RESPONSES = { } if (schedule?.dayOfWeek) { - let weekday = MATCHERS.weekdays[ + const weekday = MATCHERS.weekdays[ Number(schedule.dayOfWeek) - 1 ]?.toUpperCase(); @@ -75,7 +75,7 @@ const RESPONSES = { } if (schedule?.dayOfMonth) { - let month = MATCHERS.months[ + const month = MATCHERS.months[ Number(schedule.month) - 1 ]?.capitalize(); @@ -97,7 +97,7 @@ export class Timer { } compare(job: Cron): void { - let current = new Date(); + const current = new Date(); current.setDate(current.getDate()); current.setUTCHours(current.getHours() % 12); current.setSeconds(0); @@ -108,8 +108,8 @@ export class Timer { } timestamp(job: Cron): number { - let date = new Date(); - let { hours, minutes, month, dayOfMonth } = job.schedule; + const date = new Date(); + const { hours, minutes, month, dayOfMonth } = job.schedule; date.setUTCHours(Number(hours), Number(minutes), 0); date.setMonth(Number(month) - 1); @@ -136,7 +136,7 @@ export class Timer { } verify(jobs: Cron[]): void { - jobs.forEach((job: Cron) => this.compare(job)) + jobs.forEach((job: Cron) => this.compare(job)); } } @@ -177,24 +177,24 @@ export default (home_scope: HomeScope) => { }; const list = () => { - if (crons.length == 0) + if (crons.length === 0) return message.answer(RESPONSES.empty); console.log('list command:', crons - .filter(x => x != null) + .filter(x => x !== null) .map(x => RESPONSES.list(x)) .join("\n")); message.channel.send( crons - .filter(x => x != null) + .filter(x => x !== null) .map(x => RESPONSES.list(x)) .join("\n") ); - } + }; const parse = (argm: string[]): Cron => { - let cron: Cron = { + const cron: Cron = { id: crons.slice(-1)[0]?.id + 1 || 0 }; @@ -224,7 +224,7 @@ export default (home_scope: HomeScope) => { argument.split(MATCHERS.ordinals); const date = - matches(argument, MATCHERS.ordinals) == undefined + matches(argument, MATCHERS.ordinals) === undefined ? { month: argument } : { dayOfMonth, ordinal: ordinal as Ordinal }; @@ -253,7 +253,7 @@ export default (home_scope: HomeScope) => { if (args[0] === 'ls') list(); else if (args[0] === 'rm') { - let job: number = Number(args[1]); + const job: number = Number(args[1]); (isNaN(job)) ? message.answer(RESPONSES.help.rm) @@ -265,7 +265,7 @@ export default (home_scope: HomeScope) => { if (!cron?.command) message.answer(RESPONSES.help.command); else if (!cron?.schedule) - message.answer(RESPONSES.help.schedule) + message.answer(RESPONSES.help.schedule); else { crons.push(cron); submit(); diff --git a/lib/commands/hangman.ts b/lib/commands/hangman.ts @@ -1,48 +1,48 @@ import { Message, User } from 'discord.js'; import { FORMATS } from '../extensions'; -namespace Types { - type ScoreStats = { - missed: number, - scored: number - }; - - export type Mask = '_' | '.' | '-' | ':'; - - export interface Score { - [id: string]: ScoreStats - }; - - export interface Guess { - id: string, - answer: string, - attempt?: string - }; - - export interface Config { - attempt_limit: number, - mask: Mask; - }; - - export interface Messages { - help: string, - lose: string, - miss: string, - in_progress: string, - win: (id: string) => string, - start: (word: string) => string, - guess: (attempt: string) => string, - result: (missed: number, - scored: number) => string; - }; - - export enum Status { - Started, - InProgress +declare global { + namespace Types { + type ScoreStats = { + missed: number, + scored: number + }; + + export type Mask = '_' | '.' | '-' | ':'; + + export interface Score { + [id: string]: ScoreStats; + } + + export interface Guess { + id: string; + answer: string; + attempt?: string; + } + + export interface HangmanConfig { + attempt_limit: number; + mask: Mask; + } + + export interface Messages { + help: string; + lose: string; + miss: string; + in_progress: string; + win: (id: string) => string; + start: (word: string) => string; + guess: (attempt: string) => string; + result: (missed: number, + scored: number) => string; + } + + export enum Status { + Started, + InProgress + } } -} -declare global { interface Array<T> { result(): string; pick(): string; @@ -59,7 +59,7 @@ Array.prototype.result = function () { Array.prototype.pick = function (): string { return this[Math.floor(Math.random() * this.length)]; -} +}; String.prototype.mask_with = function (mask: Types.Mask = '_') { return [...(mask.repeat(this.length))]; @@ -72,17 +72,17 @@ const WORDS: string[] = [ let GAME_STATUS: Types.Status; -let scores: Types.Score = { +const scores: Types.Score = { "29138138129139128": { missed: 0, scored: 0 } -} +}; -const CONFIG: Types.Config = { +const CONFIG: Types.HangmanConfig = { attempt_limit: 10, mask: '_' -} +}; const MESSAGES: Types.Messages = { help: "To start a new hangman game" @@ -128,11 +128,11 @@ const [id, attempts, output]: [string, number, string[]] = [ answer.mask_with(CONFIG.mask) ]; -const score = (id: string) => - scores[id].scored++; +const score = (u_id: string) => + scores[u_id].scored++; -const miss = (id: string) => - scores[id].missed++; +const miss = (u_id: string) => + scores[u_id].missed++; const guessed = (attempt: string) => MESSAGES.guess(attempt); @@ -144,15 +144,16 @@ const lose = () => console.log(MESSAGES.lose); // message.channel.send(MESSAGES.lose); -const win = (id: string) => { - score(id); - const { missed, scored } = scores[id]; - MESSAGES.win(id); +const win = (u_id: string) => { + score(u_id); + const { missed, scored } = scores[u_id]; + MESSAGES.win(u_id); MESSAGES.result(missed, scored); }; +// FIX THESE SHADOW VARIABLES (id, answer) const guess = ({ id, answer, attempt }: Types.Guess) => { - if (GAME_STATUS != (Types.Status.Started | Types.Status.InProgress)) { + if (GAME_STATUS !== (Types.Status.Started | Types.Status.InProgress)) { console.log(MESSAGES.in_progress); return; } @@ -162,7 +163,7 @@ const guess = ({ id, answer, attempt }: Types.Guess) => { if (attempt.length === answer.length) (attempt === answer) ? win(id) : miss(id); - if (answer.indexOf(attempt) != -1) { + if (answer.indexOf(attempt) !== -1) { miss(id); return (scores[id].missed >= attempts)