Skip to content

Commit

Permalink
better logic for duel
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Aug 31, 2023
1 parent d264ea5 commit 4d52d69
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 143 deletions.
70 changes: 1 addition & 69 deletions bot.js
Expand Up @@ -13,7 +13,6 @@ import { main } from "./commands/dalle/aart.js";
import { invocationWorkflow, preWorkflow } from "./invocation.js";
import { quosLogic } from "./commands/quoordinates/quos.js";
import { lookupBook } from "./books.js";
import { complete } from "./openai_helper.js";

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
import config from "./config.json" assert { "type": "json" };
Expand Down Expand Up @@ -49,71 +48,7 @@ client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand() && !interaction.isButton()) return;

if (interaction.isButton()) {
if (interaction.customId === "duel__topic_1_x") {
console.log(interaction);
await interaction.deferReply();
await preWorkflow(interaction);
// get topic from button label
const topic = interaction.message.components[0].components[0].label;
console.log(topic);
// await interaction.editReply({
// content: `You chose ${topic}! ephemeral`,
// ephemeral: true,
// });

// given topic create two questions from the complete function
const questionsRes = await complete(
`Create two different short questions (less than 80 characters) about ${topic} that you would ask someone else to get them to talk about it.`
);

// create two buttons with the questions as labels
const question1 = questionsRes.split("\n")[0];
const question2 = questionsRes.split("\n")[1];

const message = await interaction.followUp({
content: `Which question do you prefer?\n${question1}\n${question2}\nRespond with 1 or 2 emoji.`,
fetchReply: true,
});

message.react("1️⃣");
message.react("2️⃣");

const filter = (reaction, user) => {
return (
["1️⃣", "2️⃣"].includes(reaction.emoji.name) &&
user.id === interaction.user.id
);
};

console.log(message);

message
.awaitReactions({ filter, max: 1, time: 60000, errors: ["time"] })
.then((collected) => {
const reaction = collected.first();

if (reaction.emoji.name === "1️⃣") {
interaction.followUp("you chose 1");
} else {
interaction.followUp("you chose 2");
}

interaction.followUp(`you chose ${reaction.emoji.name}`);
})
.catch((collected) => {
interaction.followUp("you did not react with a 1️⃣ or 2️⃣ emoji.");
});
} else if (interaction.customId === "duel__topic_2_x") {
await interaction.deferReply();
await preWorkflow(interaction);
// get topic from button label
const topic = interaction.message.components[0].components[1].label;
console.log(topic);
await interaction.followUp({
content: `You chose ${topic} (ephemeral)!`,
ephemeral: true,
});
} else if (interaction.customId === "button_id") {
if (interaction.customId === "button_id") {
await interaction.deferReply();
await preWorkflow(interaction);
const { prompt, imageUrl } = await main(interaction.message.content);
Expand Down Expand Up @@ -176,9 +111,6 @@ client.on(Events.InteractionCreate, async (interaction) => {
return;
} else {
const command = client.commands.get(interaction.commandName);

console.log("called command", command);

if (!command) return;

try {
Expand Down
159 changes: 85 additions & 74 deletions commands/quoordinates/duel.js
Expand Up @@ -11,7 +11,6 @@ import { complete } from "../../openai_helper.js";
import { quosLogic } from "./quos.js";
import { lookupBook } from "../../books.js";


const duelCommand = new SlashCommandBuilder()
.setName("duel")
.setDescription("Find quotes based on your responses to two prompts.");
Expand Down Expand Up @@ -49,11 +48,19 @@ export async function execute(interaction) {
time: 60_000,
});

if (confirmation.customId === "duel__topic_1") {
// get topic from button label
const topic = confirmation.message.components[0].components[0].label;
if (
confirmation.customId === "duel__topic_1" ||
confirmation.customId === "duel__topic_2"
) {
let topic = null;
if (confirmation.customId === "duel__topic_1") {
topic = confirmation.message.components[0].components[0].label;
} else if (confirmation.customId === "duel__topic_2") {
topic = confirmation.message.components[0].components[1].label;
}

const questionsRes = await complete(
`Create two different short questions (less than 80 characters) about ${topic} that you would ask someone else to get them to talk about it.`
`Create two different short questions (less than 80 chars) about ${topic} that you would ask someone else to get them to talk about it.`
);

// create two buttons with the questions as labels
Expand All @@ -72,84 +79,88 @@ export async function execute(interaction) {
.setStyle(ButtonStyle.Primary)
);

console.log("waiting for second confirmation as " + confirmation.customId);
await confirmation.update({
content: `Which question do you prefer?\n${question1}\n${question2}\nRespond with 1 or 2 emoji.`,
content: `Which question do you prefer?\n${question1}\n${question2}`,
components: [row],
ephemeral: true,
});

const collectorFilter = (i) => i.user.id === interaction.user.id;
try {
const confirmation = await response.awaitMessageComponent({
filter: collectorFilter,
time: 60_000,
});
// const collectorFilter = (i) => i.user.id === interaction.user.id;
console.log("above awaitMessageComponent for second confirmation");
const confirmation2 = await response.awaitMessageComponent({
filter: collectorFilter,
time: 60_000,
});
console.log("got second confirmation " + confirmation2.customId);

if (confirmation.customId === "duel__question_1") {
// run quosLogic with question1
try {
await confirmation.deferReply();
const quoordinate = await quosLogic(question1);
// console.log(quoordinate);
const quotes = quoordinate
.map(
(q) =>
`> ${q.text}\n\n-- ${
lookupBook(q.title) ? `[${q.title}](${lookupBook(q.title)})` : q.title
}\n\n`
)
.filter((q) => q.length < 2000);



const thread = await interaction.channel.threads.create({
name: question1,
autoArchiveDuration: 60,
startMessage: interaction.channel.lastMessage,
type: ChannelType.GUILD_PUBLIC_THREAD,
reason: "Sending quotes as separate messages in one thread",
});

// console.log(thread);

const makeAart = new ButtonBuilder()
.setCustomId("button_id")
.setLabel("Make Aart (+1 aart)")
.setStyle(ButtonStyle.Primary);

const learnMore = new ButtonBuilder()
.setCustomId("quos_learn_more")
.setLabel("Learn More (+1 quos)")
.setStyle(ButtonStyle.Primary);

const row = new ActionRowBuilder().addComponents(makeAart, learnMore);

for (const quote of quotes) {
await thread.send({
content: quote,
components: [row],
});
}
} catch (err) {
console.log(err);
}

await confirmation.update({
content: `Results: ${quoordinate.length} quotes`,
components: [],
ephemeral: true,
});


}
} catch (e) {
console.log(e);
let embeddedSearches = [];
let chosenQuestion = null;

confirmation2.deferUpdate();

if (confirmation2.customId === "duel__question_1") {
embeddedSearches = await quosLogic(question1);
chosenQuestion = question1;
} else if (confirmation2.customId === "duel__question_2") {
embeddedSearches = await quosLogic(question2);
chosenQuestion = question2;
}
} else if (confirmation.customId === "cancel") {
await confirmation.update({
content: "Action cancelled",

const quotes = embeddedSearches
.map(
(q) =>
`> ${q.text}\n\n-- ${
lookupBook(q.title)
? `[${q.title}](${lookupBook(q.title)})`
: q.title
}\n\n`
)
.filter((q) => q.length < 2000);

const thread = await interaction.channel.threads.create({
name: chosenQuestion,
autoArchiveDuration: 60,
startMessage: interaction.channel.lastMessage,
type: ChannelType.GUILD_PUBLIC_THREAD,
reason: "Sending quotes as separate messages in one thread",
});

const makeAart = new ButtonBuilder()
.setCustomId("button_id")
.setLabel("Make Aart (+1 aart)")
.setStyle(ButtonStyle.Primary);

const learnMore = new ButtonBuilder()
.setCustomId("quos_learn_more")
.setLabel("Learn More (+1 quos)")
.setStyle(ButtonStyle.Primary);

const summarize = new ButtonBuilder()
.setCustomId("summarize")
.setLabel("Summarize (+1 quos)")
.setStyle(ButtonStyle.Primary);

const row2 = new ActionRowBuilder().addComponents(
makeAart,
learnMore,
summarize
);

for (const quote of quotes) {
await thread.send({
content: quote,
components: [row2],
});
}

await confirmation.editReply({
content: `Generating quotes based on your responses to two prompts. Topic: **${topic}** Question: **${chosenQuestion}**`,
components: [],
ephemeral: true,
});
} else {
console.log("not a valid customId");
}
} catch (e) {
console.log(e);
Expand Down

0 comments on commit 4d52d69

Please sign in to comment.