Skip to content

Commit

Permalink
*411 search complete
Browse files Browse the repository at this point in the history
  • Loading branch information
SunburntRock89 committed Dec 27, 2022
1 parent f61d986 commit 3f799ed
Show file tree
Hide file tree
Showing 29 changed files with 325 additions and 142 deletions.
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextIndex"]
}

// generator erd {
Expand Down Expand Up @@ -164,4 +165,6 @@ model Phonebook {
description String
numberDoc Numbers @relation(fields: [number], references: [number])
@@fulltext([description])
}
138 changes: 76 additions & 62 deletions src/commands/standard/call.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Command from "../../internals/commandProcessor";
import CallClient from "../../internals/callClient";
import { ActionRowBuilder, APIEmbed, ButtonBuilder, ButtonStyle, Embed, EmbedBuilder, SelectMenuBuilder, SelectMenuOptionBuilder } from "discord.js";
import { ActionRowBuilder, APIEmbed, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, EmbedBuilder, MessageComponentInteraction, SelectMenuBuilder, SelectMenuOptionBuilder } from "discord.js";
import { getFixedT } from "i18next";
import { formatBalance, formatDate, upperFirst } from "../../internals/utils";
import { client } from "../../dtel";
import { Numbers } from "@prisma/client";
import config from "../../config/config";

export default class Call extends Command {
async run(): Promise<void> {
Expand All @@ -16,76 +19,87 @@ export default class Call extends Command {
break;
}
default: {
await this.interaction.deferReply();
try {
Call.call(this.interaction, this.interaction.options.getString("number", true), this.number!);
} catch {
// Ignore
}
}
}
}

const callObject = new CallClient(this.client, {
from: this.number!.number,
// TODO: Remove fromNum and have this be standalone?
static async call(interaction: ChatInputCommandInteraction | MessageComponentInteraction, toNum: string, fromNum: Numbers): Promise<CallClient> {
const t = getFixedT(interaction.locale, undefined, `commands.call`);
await interaction.deferReply();

to: this.interaction.options.getString("number", true),
startedBy: this.interaction.user.id,
random: false,
});
const callObject = new CallClient(client, {
from: fromNum.number,

try {
await callObject.initiate();
this.interaction.editReply({
to: toNum,
startedBy: interaction.user.id,
random: false,
});

try {
await callObject.initiate();
interaction.editReply({
embeds: [{
color: config.colors.info,
...t("initiated", {
number: toNum,
callID: callObject.id,
}) as APIEmbed,
}],
});
} catch (e) {
// This works as when we error out in CallClient, we return a translation path instead of an error message
// Feel free to change it
if (e instanceof Error) {
if (e.message === "otherSideInCall") {
interaction.editReply({
embeds: [{
color: this.config.colors.info,
...this.t("initiated", {
number: this.interaction.options.getString("number", true),
callID: callObject.id,
}) as APIEmbed,
color: config.colors.info,
...t("waitPrompt") as APIEmbed,
}],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents([
new ButtonBuilder({
customId: "call-waitAccept",
label: t("waitAccept")!,
style: ButtonStyle.Primary,
emoji: "✅",
}),
new ButtonBuilder({
customId: "call-waitDeny",
label: t("waitDeny")!,
style: ButtonStyle.Secondary,
emoji: "❌",
}),
]),
],
});
} catch (e) {
// This works as when we error out in CallClient, we return a translation path instead of an error message
// Feel free to change it
if (e instanceof Error) {
if (e.message === "otherSideInCall") {
this.interaction.editReply({
embeds: [{
color: this.config.colors.info,
...this.t("waitPrompt") as APIEmbed,
}],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents([
new ButtonBuilder({
customId: "call-waitAccept",
label: this.t("waitAccept")!,
style: ButtonStyle.Primary,
emoji: "✅",
}),
new ButtonBuilder({
customId: "call-waitDeny",
label: this.t("waitDeny")!,
style: ButtonStyle.Secondary,
emoji: "❌",
}),
]),
],
});

setTimeout(() => {
this.interaction.deleteReply().catch(() => null);
}, 60000);

// TODO: Deal with call waiting in some way

return;
}

this.interaction.editReply({
embeds: [this.client.errorEmbed(this.t(`errors.${e.message}`))],
});
} else {
this.interaction.editReply({
embeds: [this.client.errorEmbed(this.t(`errors.unexpected`))],
});
}

setTimeout(() => {
interaction.deleteReply().catch(() => null);
}, 60000);

// TODO: Deal with call waiting in some way

throw new Error("Client encountered an error.");
}

interaction.editReply({
embeds: [client.errorEmbed(t(`errors.${e.message}`))],
});
} else {
interaction.editReply({
embeds: [client.errorEmbed(t(`errors.unexpected`))],
});
}
}
return callObject;
}

async twoThreeThree(): Promise<void> {
Expand Down
10 changes: 4 additions & 6 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Commands from "../config/commands";
import Command, { CommandType, PermissionLevel, SubcommandData } from "../interfaces/commandData";
import Constructable from "../interfaces/constructable";
import DTelClient from "../internals/client";
import Processor from "../internals/processor";
import Processor, { ChannelBasedInteraction } from "../internals/processor";
import i18n, { getFixedT } from "i18next";
import { winston } from "../dtel";
import config from "../config/config";
Expand Down Expand Up @@ -44,7 +44,7 @@ export default async(client: DTelClient, _interaction: Interaction): Promise<voi

commandName = typedInteraction.commandName;
const cmd = Commands.find(c => c.name === commandName);
if (!cmd) throw new Error();
if (!cmd) throw new Error(`Could not find command data for command ${commandName}`);
commandData = cmd;

if (commandData.notExecutableInCall && call) {
Expand Down Expand Up @@ -125,7 +125,7 @@ export default async(client: DTelClient, _interaction: Interaction): Promise<voi
if (commandName.startsWith("dtelnoreg")) return;

const cmd = Commands.find(c => c.name === commandName);
if (!cmd) throw new Error();
if (!cmd) throw new Error(`Could not find command data for command ${commandName}`);
commandData = cmd;

toRunPath = `${__dirname}/../interactions/${commandName}`;
Expand All @@ -144,8 +144,6 @@ export default async(client: DTelClient, _interaction: Interaction): Promise<voi

const paramsToSend: string[] = [];

console.log(interactionName);

if (interactionName.includes("-params-")) {
const paramSplit = interactionName.split("-params-");
interactionName = paramSplit[0];
Expand Down Expand Up @@ -173,7 +171,7 @@ export default async(client: DTelClient, _interaction: Interaction): Promise<voi
return;
}

let processorFile: Constructable<Processor>;
let processorFile: Constructable<Processor<ChannelBasedInteraction>>;
try {
if (client.config.devMode) {
delete require.cache[require.resolve(toRunPath!)];
Expand Down
4 changes: 0 additions & 4 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import DTelClient from "../internals/client";
import Commands from "../config/commands";
import CallClient from "../internals/callClient";
import config from "../config/config";
import discoin, { Bot, Currency } from "@discoin/scambio";
import auth from "../config/auth";
import { ApplicationCommandStringOption, ApplicationCommandSubCommand } from "discord.js";
import { winston } from "../dtel";

export default async(client: DTelClient): Promise<void> => {
client.winston.info(`Ready!`);
Expand Down
1 change: 0 additions & 1 deletion src/events/sharderMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default async(client: DTelClient, msg: Record<string, unknown>): Promise<
switch (msg.msg) {
case "callInitiated": {
const callObject = JSON.parse(msg.callDBObject as string) as CallsWithNumbers;
console.log(callObject);
let channel: TextBasedChannel;
try {
channel = (await client.channels.fetch(callObject.to.channelID)) as TextBasedChannel;
Expand Down
4 changes: 2 additions & 2 deletions src/interactions/call/233-open.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ActionRowBuilder, SelectMenuBuilder, SelectMenuOptionBuilder } from "discord.js";
import { ActionRowBuilder, ButtonInteraction, SelectMenuBuilder, SelectMenuOptionBuilder } from "discord.js";
import { t } from "i18next";
import MessageComponentProcessor from "../../internals/componentProcessor";

export default class TwoThreeThreeOpenModalButton extends MessageComponentProcessor {
export default class TwoThreeThreeOpenModalButton extends MessageComponentProcessor<ButtonInteraction> {
async run(): Promise<void> {
const monthSelectorOptions: SelectMenuOptionBuilder[] = [];
// For up to 11 months
Expand Down
5 changes: 2 additions & 3 deletions src/interactions/call/233-renew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import dayjs from "dayjs";
import { SelectMenuInteraction } from "discord.js";
import MessageComponentProcessor from "../../internals/componentProcessor";

export default class TwoThreeThreeRenewModal extends MessageComponentProcessor {
export default class TwoThreeThreeRenewModal extends MessageComponentProcessor<SelectMenuInteraction> {
async run() {
const interaction = this.interaction as SelectMenuInteraction;
const selected = interaction.values[0]; // eg m-1, m-2, y-1
const selected = this.interaction.values[0]; // eg m-1, m-2, y-1

const split = selected.split("-");
const monthYear = split[0];
Expand Down
9 changes: 9 additions & 0 deletions src/interactions/call/411-search-exit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ButtonInteraction } from "discord.js";
import ComponentProcessor from "../../internals/componentProcessor";
import { FourOneOneSearch } from "./411-selector";

export default class Call411SearchNext extends ComponentProcessor<ButtonInteraction> {
async run(): Promise<void> {
FourOneOneSearch.exit(this.interaction);
}
}
8 changes: 8 additions & 0 deletions src/interactions/call/411-search-modal-submit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ModalProcessor from "../../internals/modalProcessor";
import { FourOneOneSearch } from "./411-selector";

export default class Call411SearchModalSubmit extends ModalProcessor {
async run(): Promise<void> {
FourOneOneSearch.handleSearchInteraction(this.interaction);
}
}
6 changes: 2 additions & 4 deletions src/interactions/call/411-search-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { ButtonInteraction } from "discord.js";
import ComponentProcessor from "../../internals/componentProcessor";
import { FourOneOneSearch } from "./411-selector";

export default class Call411SearchNext extends ComponentProcessor {
export default class Call411SearchNext extends ComponentProcessor<ButtonInteraction> {
async run(): Promise<void> {
console.log(this.commandData.params);

FourOneOneSearch.page(this.interaction as ButtonInteraction, Number(this.commandData.params[0]), this.commandData.params[1], true);
FourOneOneSearch.page(this.interaction as ButtonInteraction, Number(this.commandData.params![0]), this.commandData.params![1], true);
}
}
6 changes: 2 additions & 4 deletions src/interactions/call/411-search-prev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { ButtonInteraction } from "discord.js";
import ComponentProcessor from "../../internals/componentProcessor";
import { FourOneOneSearch } from "./411-selector";

export default class Call411SearchNext extends ComponentProcessor {
export default class Call411SearchNext extends ComponentProcessor<ButtonInteraction> {
async run(): Promise<void> {
console.log(this.commandData.params);

FourOneOneSearch.page(this.interaction as ButtonInteraction, Number(this.commandData.params[0]), this.commandData.params[1], false);
FourOneOneSearch.page(this.interaction as ButtonInteraction, Number(this.commandData.params![0]), this.commandData.params![1], false);
}
}
25 changes: 25 additions & 0 deletions src/interactions/call/411-search-search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ActionRowBuilder, ButtonInteraction, ModalBuilder, TextInputBuilder, TextInputStyle } from "discord.js";
import ComponentProcessor from "../../internals/componentProcessor";

export default class Call411SearchNext extends ComponentProcessor<ButtonInteraction> {
async run(): Promise<void> {
const modal = new ModalBuilder()
.setCustomId("call-411-search-modal-submit")
.setTitle("🔎 Yellowbook Search");

modal.addComponents(
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setLabel("Search")
.setPlaceholder("Search for something in the Yellowbook")
.setMinLength(3)
.setMaxLength(100)
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setCustomId("search-query"),
),
);

this.interaction.showModal(modal);
}
}
Empty file.
Loading

0 comments on commit 3f799ed

Please sign in to comment.