Skip to content

Commit

Permalink
Update bp-command.ts (#132)
Browse files Browse the repository at this point in the history
* prisma, bank request refactor

* gitignore

* remove unused files

* migrate deploy

* db-sync error handling, prisma migrate in procfile

* prisma migration

* procfile

* disable prisma migrate

* prisma migration

* procfile disable prisma

* prisma migrate

* fix merge

* fix regresssoins

* add bankGoogleDriveFolderId to .gitpod.yml

* bank-data class

* prisma schema: set slot count default

* prisma migrate: slot count default

* clean up console logs

* added bank-request options, multi-role auth utility, modRoleId

* reduce length of stock list

* add /bank search

* add icon to bank requests

* use emoji instead of 'approved by'

* delete bank bot requests with 'x'

* annotations

* bank-item-data wip

* add prices with /bank set-item-data

* resolve conflicts

* bank-inventory

* bankbot-dev

* prisma migrate deploy post deploy

* remove message from template literal

* replace \n

* replace \n with newlines in /bp

* show key as option

* load saved message from redis if key exists

* truncate messaeges and keys if too long

* max message length
  • Loading branch information
schuyberg authored Jul 30, 2024
1 parent eeb33ed commit 528461b
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/features/bp/bp-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { redis } from "googleapis/build/src/apis/redis";
import { isObject } from "lodash";
import { container } from "tsyringe";
import { WakeupService } from "../wakeup/wakeup.service";
import { truncate } from "lodash";

class sendBp extends Subcommand {
public async execute(interaction: CommandInteraction<CacheType>) {
Expand All @@ -25,12 +26,16 @@ class sendBp extends Subcommand {
[officerRoleId, modRoleId, knightRoleId],
interaction
);

const message = this.getOption("message", interaction)?.value;
const bpChannel = await getTextChannel(batphoneChannelId);
const val = this.getOption("message", interaction)?.value;
const savedMsg = await redisClient.hGet("bp", String(val));
const message = savedMsg || val;
if (typeof message === "string") {
const formattedMessage = message.replace(/\\n/g, `
`);
await bpChannel.send({
content: `[${interaction.user}] <@&${raiderRoleId}> ${message}`,
content: `[${interaction.user}] <@&${raiderRoleId}>
` + formattedMessage,
});
interaction.editReply("Batphone posted: " + message);

Expand All @@ -54,8 +59,8 @@ class sendBp extends Subcommand {
const res = await getBpOptions();
if (isObject(res)) {
const opts = Object.entries(res).map(([key, value]) => ({
name: value,
value: value,
name: key,
value: key,
}));
return opts;
}
Expand All @@ -81,22 +86,25 @@ class setBp extends Subcommand {
interaction
);

const message = this.getOption("message", interaction)?.value;
let message = this.getOption("message", interaction)?.value;
try {
if (typeof message === "string") {
if (message.length > 2000) {
throw new Error("Message is too long.");
}
let key = this.getOption("key", interaction)?.value;
if (!key) {
key = message.split(" ")[0].toLowerCase();
}
const formattedMessage = message.replace(/\\n/g, "\n");
await redisClient.hSet("bp", String(key), formattedMessage);
interaction.editReply("Saved preset message: " + formattedMessage);
key = truncate(String(key), { length: 100}) // max option length = 100
await redisClient.hSet("bp", String(key), message);
interaction.editReply("Saved preset message: " + message);
} else {
throw error;
}
} catch (err) {
console.error(err);
interaction.editReply("Failed save batphone message.");
interaction.editReply("Failed save batphone message: " + err);
}
}
public async getOptionAutocomplete(
Expand Down Expand Up @@ -157,7 +165,7 @@ class unsetBp extends Subcommand {
const res = await getBpOptions();
if (isObject(res)) {
const opts = Object.entries(res).map(([key, value]) => ({
name: value,
name: key,
value: key,
}));
return opts;
Expand Down

0 comments on commit 528461b

Please sign in to comment.