Skip to content

Commit 6174807

Browse files
authored
Update prisma error handling (#168)
Add /bp update command
1 parent 630e876 commit 6174807

File tree

1 file changed

+122
-4
lines changed

1 file changed

+122
-4
lines changed

src/features/bp/bp-command.ts

+122-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { Command } from "../../shared/command/command";
1111
import { Subcommand } from "../../shared/command/subcommand";
1212
import { getTextChannel, prismaClient } from "../..";
13+
import { Prisma } from "@prisma/client"
1314
import {
1415
batphoneChannelId,
1516
raiderRoleId,
@@ -154,7 +155,7 @@ class setBp extends Subcommand {
154155
key = message.split(" ")[0].toLowerCase();
155156
}
156157
const location = this.getOption("location", interaction)
157-
?.value as string;
158+
?.value as string || "";
158159
key = truncate(String(key), { length: 100 }); // max option length = 100
159160
await prismaClient.batphone.create({
160161
data: {
@@ -174,7 +175,15 @@ class setBp extends Subcommand {
174175
}
175176
} catch (err) {
176177
console.error(err);
177-
interaction.editReply("Failed save batphone message: " + err);
178+
let errMsg = err;
179+
if (err instanceof Prisma.PrismaClientKnownRequestError) {
180+
if (err.code === 'P2002') {
181+
errMsg = "Key already exists";
182+
} else {
183+
errMsg = err.code;
184+
}
185+
}
186+
interaction.editReply("Failed save batphone message: " + errMsg);
178187
}
179188
}
180189
public async getOptionAutocomplete(
@@ -283,7 +292,11 @@ class getBp extends Subcommand {
283292
key: val,
284293
},
285294
});
286-
const message = savedMsg?.message || val;
295+
const message = savedMsg?.message;
296+
if (!message) {
297+
throw new Error(`No batphone with key ${val}`);
298+
}
299+
287300
if (typeof message === "string") {
288301
const formattedMessage = message.replace(
289302
/\\n/g,
@@ -297,7 +310,7 @@ ${formattedMessage}
297310
298311
--------
299312
Location: ${savedMsg?.location || "NO LOCATION SET"}
300-
To change this message, use \`/bp unset ${key}\` and then \`/bp set\` to set a new message.
313+
To change this message, use \`/bp update\` to set a new message.
301314
`;
302315
savedMsg
303316
? console.log(
@@ -344,6 +357,110 @@ To change this message, use \`/bp unset ${key}\` and then \`/bp set\` to set a n
344357
}
345358
}
346359

360+
class updateBp extends Subcommand {
361+
public async execute(interaction: CommandInteraction<CacheType>) {
362+
// authorize
363+
authorizeByMemberRoles(
364+
[officerRoleId, modRoleId, knightRoleId],
365+
interaction
366+
);
367+
368+
const message = this.getOption("message", interaction)?.value;
369+
try {
370+
if (typeof message === "string") {
371+
if (message.length > 2000) {
372+
// max message length is 2000 chars
373+
throw new Error("Message is too long.");
374+
}
375+
let key = this.getOption("key", interaction)?.value;
376+
if (!key) {
377+
key = message.split(" ")[0].toLowerCase();
378+
}
379+
const location = this.getOption("location", interaction)
380+
?.value as string || "";
381+
key = truncate(String(key), { length: 100 }); // max option length = 100
382+
383+
let updateNoLocation = {
384+
message: message
385+
};
386+
387+
let updateWithLocation = {
388+
message: message,
389+
location: location
390+
};
391+
392+
let update = location ? updateWithLocation : updateNoLocation;
393+
394+
await prismaClient.batphone.update({
395+
where: {
396+
key: key
397+
},
398+
data: update
399+
});
400+
401+
console.log(
402+
`Updated batphone - key: ${key}, location: ${
403+
location || "unset"
404+
}, message: ${message}`
405+
);
406+
interaction.editReply("Updated Batphone: " + key);
407+
} else {
408+
throw error;
409+
}
410+
} catch (err) {
411+
console.error(err);
412+
let errMsg = err;
413+
if (err instanceof Prisma.PrismaClientKnownRequestError) {
414+
if (err.code === 'P2025') {
415+
errMsg = "No batphone found with the provided key";
416+
} else {
417+
errMsg = err.code;
418+
}
419+
}
420+
interaction.editReply("Failed update batphone message: " + errMsg);
421+
}
422+
}
423+
424+
public async getOptionAutocomplete(
425+
option: string,
426+
interaction: AutocompleteInteraction<CacheType>
427+
): Promise<
428+
ApplicationCommandOptionChoiceData<string | number>[] | undefined
429+
> {
430+
switch (option) {
431+
case "location":
432+
return LocationService.getInstance().getLocationOptions();
433+
default:
434+
return [];
435+
}
436+
}
437+
438+
public get command() {
439+
return super.command
440+
.addStringOption((o) =>
441+
o
442+
.setName("message")
443+
.setDescription("BP Message")
444+
.setRequired(true)
445+
.setAutocomplete(false)
446+
)
447+
.addStringOption((o) =>
448+
o
449+
.setName("key")
450+
.setDescription("Key (optional")
451+
.setRequired(false)
452+
.setAutocomplete(false)
453+
)
454+
.addStringOption((o) =>
455+
o
456+
.setName("location")
457+
.setDescription("Location of the batphone")
458+
.setRequired(false)
459+
.setAutocomplete(true)
460+
);
461+
}
462+
}
463+
347464
export const batphoneCommand = new Command(
348465
"bp",
349466
"set and send batphone messages",
@@ -352,5 +469,6 @@ export const batphoneCommand = new Command(
352469
new setBp("set", "save a BP preset"),
353470
new unsetBp("unset", "remove BP preset"),
354471
new getBp("get", "show BP message in this channel"),
472+
new updateBp("update", "update a BP")
355473
]
356474
);

0 commit comments

Comments
 (0)