Skip to content

Commit

Permalink
feat: added created thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafasegf committed Apr 13, 2024
1 parent b781b88 commit 6d98bc2
Showing 1 changed file with 66 additions and 20 deletions.
86 changes: 66 additions & 20 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ Here are available Server commands:

await interaction.editReply("Configuration has been saved.");
},

},
{
name: "settings",
Expand Down Expand Up @@ -380,11 +379,16 @@ Here are available Server commands:
return;
}
question = data.activeDailyCodingChallengeQuestion.question;
await interaction.reply(await createEmbed(question));
const embed = await createEmbed(question);
await interaction.reply(embed);

if ("content" in embed) {
return;
}

const collector = interaction.channel!.createMessageComponentCollector({
filter: (i) => i.customId === "thread",
time: 60 * 1000,
time: 5 * 60 * 1000,
});

collector.on("collect", async (i) => {
Expand All @@ -397,7 +401,7 @@ Here are available Server commands:

collector.on("end", async (collected) => {
console.log(`Collected ${collected.size} threads`);
interaction.editReply({ components: [] });
interaction.editReply({ components: embed.editedComponents });
});
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -461,7 +465,30 @@ Here are available Server commands:
}
const question = data.randomQuestion;

await interaction.reply(await createEmbed(question));
const embed = await createEmbed(question);
await interaction.reply(embed);

if ("content" in embed) {
return;
}

const collector = interaction.channel!.createMessageComponentCollector({
filter: (i) => i.customId === "thread",
time: 5 * 60 * 1000,
});

collector.on("collect", async (i) => {
await i.deferUpdate();
await i.message.startThread({
name: question.title,
autoArchiveDuration: 60 * 24 * 3, // 3 days
});
});

collector.on("end", async (collected) => {
console.log(`Collected ${collected.size} threads`);
interaction.editReply({ components: embed.editedComponents });
});
} catch (e) {
console.error(e);
if (e instanceof Error) {
Expand Down Expand Up @@ -612,7 +639,7 @@ Here are available Server commands:

const collector = interaction.channel!.createMessageComponentCollector({
filter: (i) => i.customId === "prev" || i.customId === "next",
time: 15000,
time: 15 * 1000,
});

collector.on("collect", async (i) => {
Expand Down Expand Up @@ -676,21 +703,40 @@ export async function createEmbed(question: Question) {
{ name: "Dislikes", value: String(question.dislikes), inline: true },
]);

const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel("View")
.setURL(`https://leetcode.com/problems/${question.titleSlug}`)
.setStyle(ButtonStyle.Link),
)
.addComponents(
new ButtonBuilder()
.setLabel("Create Thread")
.setCustomId("thread")
.setStyle(ButtonStyle.Primary),
);
const view = new ButtonBuilder()
.setLabel("View")
.setURL(`https://leetcode.com/problems/${question.titleSlug}`)
.setStyle(ButtonStyle.Link);

return { embeds: [embed], components: [row] };
const editedView = new ButtonBuilder()
.setLabel("View")
.setURL(`https://leetcode.com/problems/${question.titleSlug}`)
.setStyle(ButtonStyle.Link);

const thread = new ButtonBuilder()
.setLabel("Create Thread")
.setCustomId("thread")
.setStyle(ButtonStyle.Primary);

const threadDisable = new ButtonBuilder()
.setLabel("Create Thread")
.setCustomId("thread")
.setStyle(ButtonStyle.Primary)
.setDisabled(true);

const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(view)
.addComponents(thread);

const editedRow = new ActionRowBuilder<ButtonBuilder>()
.addComponents(editedView)
.addComponents(threadDisable);

return {
embeds: [embed],
components: [row],
editedComponents: [editedRow],
};
} catch (e) {
console.error(e);
return { content: "Something went wrong" };
Expand Down

0 comments on commit 6d98bc2

Please sign in to comment.