Skip to content

Commit b17dfc9

Browse files
committed
Update API
1 parent 536a1a6 commit b17dfc9

3 files changed

Lines changed: 49 additions & 23 deletions

File tree

src/commands/misc/reminderadd.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CommandInteraction, Message, MessageEmbed } from "discord.js"
22
import client from "../../main"
33

44
import Command from "../../utils/Command"
5-
import { Colors, getUserID, sendMessage, timeLeft } from "../../utils/Utils"
5+
import { Colors, getUserID, parseDuration, sendMessage, timeLeft } from "../../utils/Utils"
66
import config from "../../data/config.json"
77
import { CommandSource, SendMessage } from "../../utils/Types"
88

@@ -68,19 +68,7 @@ Example: \`${config.prefix}ar Weekly boss in 36 resin\``,
6868
else return this.sendHelp(source)
6969
}
7070

71-
let duration = 0
72-
const times = [...time.matchAll(/((\d+) ?(months?|mo|weeks?|w|days?|d|hours?|h|minutes?|min|m|seconds?|sec|s|resins?|r))/gi)]
73-
74-
for (const time of times) {
75-
const name = time[3].toLowerCase(), amount = parseInt(time[2])
76-
if (name.startsWith("mo")) duration += amount * 30 * 24 * 60 * 60 * 1000
77-
else if (name.startsWith("w")) duration += amount * 7 * 24 * 60 * 60 * 1000
78-
else if (name.startsWith("d")) duration += amount * 24 * 60 * 60 * 1000
79-
else if (name.startsWith("h")) duration += amount * 60 * 60 * 1000
80-
else if (name.startsWith("m")) duration += amount * 60 * 1000
81-
else if (name.startsWith("s")) duration += amount * 1000
82-
else if (name.startsWith("r")) duration += amount * client.data.minutes_per_resin * 60 * 1000
83-
}
71+
const duration = parseDuration(time)
8472

8573
if (duration == 0) return this.sendHelp(source)
8674

src/utils/Utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,25 @@ export function getUserID(source: CommandSource): string {
533533
}
534534

535535

536+
export function parseDuration(time: string): number {
537+
let duration = 0
538+
const times = [...time.matchAll(/((\d+) ?(months?|mo|weeks?|w|days?|d|hours?|h|minutes?|min|m|seconds?|sec|s|resins?|r))/gi)]
539+
540+
for (const time of times) {
541+
const name = time[3].toLowerCase(), amount = parseInt(time[2])
542+
if (name.startsWith("mo")) duration += amount * 30 * 24 * 60 * 60 * 1000
543+
else if (name.startsWith("w")) duration += amount * 7 * 24 * 60 * 60 * 1000
544+
else if (name.startsWith("d")) duration += amount * 24 * 60 * 60 * 1000
545+
else if (name.startsWith("h")) duration += amount * 60 * 60 * 1000
546+
else if (name.startsWith("m")) duration += amount * 60 * 1000
547+
else if (name.startsWith("s")) duration += amount * 1000
548+
else if (name.startsWith("r")) duration += amount * client.data.minutes_per_resin * 60 * 1000
549+
}
550+
551+
return duration
552+
}
553+
554+
536555
export function getLinkToGuide(guide: Guide, page: GuidePage): string {
537556
return `[${page.name}](${client.data.baseURL}guides/${urlify(guide.name, false)}/${urlify(page.name, true)})`
538557
}

src/utils/WebManager.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express, { Express, NextFunction, Request, Response } from "express"
33
import log4js from "log4js"
44
import config from "../data/config.json"
55
import client from "../main"
6+
import { parseDuration } from "./Utils"
67

78
const Logger = log4js.getLogger("WebManager")
89

@@ -19,46 +20,56 @@ export default class WebManager {
1920
this.app.use(bodyParser.json())
2021
this.app.use((req: Req<unknown>, res: Res, next: NextFunction) => {
2122
if (req.headers.authorization === config.web) {
22-
Logger.info("Wrong authorization used!")
2323
next()
2424
return
2525
}
26+
Logger.error("Wrong authorization used!")
2627

2728
res.sendStatus(403)
2829
})
2930

3031
this.app.get("/reminders/:user/get", this.getReminder)
3132
this.app.post("/reminders/:user/create", this.addReminder)
3233
this.app.post("/reminders/:user/delete", this.deleteReminder)
34+
this.app.post("/testmessage/:user", this.testMessage)
3335

3436
this.app.listen(config.webPort)
3537
}
3638

3739
getReminder(req: Req<{ user: string }>, res: Res): void {
40+
const userid = req.params.user
41+
42+
Logger.info(`Getting reminders for ${userid} from remote`)
3843
res.send(client.reminderManager.getReminders(req.params.user))
3944
}
4045

41-
addReminder(req: Req<{ user: string }, { subject?: string, duration?: number, timestamp?: number}>, res: Res): void {
46+
addReminder(req: Req<{ user: string }, { name?: string, duration?: string}>, res: Res): void {
4247
const { reminderManager } = client
4348
const userid = req.params.user
44-
const subject = req.body.subject
45-
const duration = req.body.duration
46-
const timestamp = req.body.timestamp
49+
const name = req.body.name
50+
const time = req.body.duration
4751

48-
if (subject == undefined || duration == undefined || timestamp == undefined) {
52+
if (name == undefined || time == undefined || name.length > 128) {
4953
res.sendStatus(400)
5054
return
5155
}
5256

53-
Logger.info(`Adding reminder for ${userid} from remote`)
5457
const reminders = reminderManager.getReminders(userid)
58+
const duration = parseDuration(time)
5559

5660
let id = 1
5761
while (reminders.some(r => r.id == id) || reminderManager.getReminderById(userid, id))
5862
id++
5963

60-
reminderManager.addReminder(id, subject, userid, duration, timestamp)
61-
res.sendStatus(200)
64+
if (id > 25 || duration <= 0) {
65+
res.sendStatus(400)
66+
return
67+
}
68+
69+
Logger.info(`Adding reminder for ${userid} from remote`)
70+
71+
const r = reminderManager.addReminder(id, name, userid, duration, Date.now() + duration)
72+
res.send(r)
6273
}
6374

6475
deleteReminder(req: Req<{ user: string }, { id?: number, timestamp?: number }>, res: Res): void {
@@ -79,9 +90,17 @@ export default class WebManager {
7990
res.sendStatus(404)
8091
return
8192
}
93+
8294
Logger.info(`Deleting reminder for ${userid} from remote`)
8395

8496
reminderManager.deleteReminder(userid, id, timestamp)
8597
res.sendStatus(200)
8698
}
99+
100+
async testMessage(req: Req<{ user: string }>, res: Res): Promise<void> {
101+
const userid = req.params.user
102+
const user = await client.users.fetch(userid)
103+
await user.send("This is a test")
104+
res.sendStatus(200)
105+
}
87106
}

0 commit comments

Comments
 (0)