Skylife
Skylife
DIAdiscord.js - Imagine an app
Created by Skylife on 10/20/2024 in #djs-questions
value of autocomplete gets lost after switching to other channel and back
I have encountered an issue where the value of my autoselect gets lost in the execute method after i switch to another channel and then execute the command Autocomplete code
if (focusedOption.name === optionNames.date) {
for (let i = 0; i < 14; i++) {
const date = dayjs().utc().add(i, 'day')
choices.push({name: `${date.format('dddd DD.MM.YYYY')}`, value: `${date.toDate()}`});
}
}
if (focusedOption.name === optionNames.date) {
for (let i = 0; i < 14; i++) {
const date = dayjs().utc().add(i, 'day')
choices.push({name: `${date.format('dddd DD.MM.YYYY')}`, value: `${date.toDate()}`});
}
}
result when i switch the channel while the command is in autocomplete mode selectedDate: Sunday 20.10.2024 (this is basically my name: property) result when i dont switch the channel which should be always my expected result selectedDate: Mon Oct 21 2024 14:48:56 GMT+0200 (Mitteleuropäische Sommerzeit) can someone help me here?
6 replies
DIAdiscord.js - Imagine an app
Created by Skylife on 6/16/2024 in #djs-questions
pagination with button handler
Hello i want to refactor my current embed pagination. How do I update the embed in a separate button handler? A user starts a slashcommand where I create n embeds where n: n >= 1. the customId of the two buttons are in this format RESERVATION-NEXT-' /*spotId*/, RESERVATION-BACK-' /*spotId*/, In my button handler (interactionCreate.ts) i dont know how I access the embeds from my slashcommand which I have created to edit the current embed
} else if (interaction.isButton()) {
const button = interaction.client.buttons.get(interaction.customId);
if (!button) {
if (interaction.customId.startsWith('RESERVATION-NEXT-' /*spotId*/) && interaction.customId.split('-').length === 3) {
return await handleSpotNextButton(interaction);
}
if (interaction.customId.startsWith('RESERVATION-BACK-' /*spotId*/) && interaction.customId.split('-').length === 3) {
return await handleSpotBackButton(interaction);
}
console.error(`No interaction matching ${interaction.customId} was found.`);
return;
}
try {
if (!button.execute) return;
button.execute(interaction, userState);
} catch (error) {
console.error(error);
}
} else if (interaction.isButton()) {
const button = interaction.client.buttons.get(interaction.customId);
if (!button) {
if (interaction.customId.startsWith('RESERVATION-NEXT-' /*spotId*/) && interaction.customId.split('-').length === 3) {
return await handleSpotNextButton(interaction);
}
if (interaction.customId.startsWith('RESERVATION-BACK-' /*spotId*/) && interaction.customId.split('-').length === 3) {
return await handleSpotBackButton(interaction);
}
console.error(`No interaction matching ${interaction.customId} was found.`);
return;
}
try {
if (!button.execute) return;
button.execute(interaction, userState);
} catch (error) {
console.error(error);
}
9 replies
DIAdiscord.js - Imagine an app
Created by Skylife on 5/26/2024 in #djs-questions
registration stepper/wizard
I want to make a registration stepper/wizard how do i create this using Discord.js i want to have a init reply where a user need to accept the rules after he has clicked the yes button i want to follow up with a modal
try {
let acceptedRules = false;
const firstButton = new ButtonBuilder()
.setLabel("Yes")
.setStyle(ButtonStyle.Success)
.setCustomId("yes-button")
const secondButton = new ButtonBuilder()
.setLabel("No")
.setStyle(ButtonStyle.Danger)
.setCustomId("no-button")
const buttonRow = new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(firstButton, secondButton)
const reply = await interaction.reply({
content: "Have you read the rules?",
components: [buttonRow],
ephemeral: true
});
const collector = reply.createMessageComponentCollector({componentType: ComponentType.Button, time: 5000})
collector.on("collect", async buttonInteraction => {
if (buttonInteraction.customId === "yes-button") {
await buttonInteraction.update({content: "You have accepted the rules."})
acceptedRules = true
collector.stop()
}
if (buttonInteraction.customId === "no-button") {
collector.stop()
}

})
collector.on("end", async () => {
if (!acceptedRules) {
await interaction.followUp({
content: "You must accept the rules to register!",
ephemeral: true
})
}
await interaction.deleteReply("@original")
})
try {
let acceptedRules = false;
const firstButton = new ButtonBuilder()
.setLabel("Yes")
.setStyle(ButtonStyle.Success)
.setCustomId("yes-button")
const secondButton = new ButtonBuilder()
.setLabel("No")
.setStyle(ButtonStyle.Danger)
.setCustomId("no-button")
const buttonRow = new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(firstButton, secondButton)
const reply = await interaction.reply({
content: "Have you read the rules?",
components: [buttonRow],
ephemeral: true
});
const collector = reply.createMessageComponentCollector({componentType: ComponentType.Button, time: 5000})
collector.on("collect", async buttonInteraction => {
if (buttonInteraction.customId === "yes-button") {
await buttonInteraction.update({content: "You have accepted the rules."})
acceptedRules = true
collector.stop()
}
if (buttonInteraction.customId === "no-button") {
collector.stop()
}

})
collector.on("end", async () => {
if (!acceptedRules) {
await interaction.followUp({
content: "You must accept the rules to register!",
ephemeral: true
})
}
await interaction.deleteReply("@original")
})
16 replies
DIAdiscord.js - Imagine an app
Created by Skylife on 2/22/2024 in #djs-questions
getting guild object
anyone knows how can I check if any kind of event happened and get the guild object? my bot only works on guilds so direct messages can be ignored.
import { Client } from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { color } from "../functions";
import { BotEvent } from "../types";

module.exports = (client: Client) => {
let eventsDir = join(__dirname, "../events")

readdirSync(eventsDir).forEach(file => {
if (!file.endsWith(".js")) return;
let event: BotEvent = require(`${eventsDir}/${file}`).default
event.once ?
client.once(event.name, (...args) => event.execute(...args))
:
client.on(event.name, (...args) => {
console.log(...args);
const isApiOffline = checkApiAvailability();
if (isApiOffline) {
//pseudo code
//smthg like this but I dont want to make a switch case over all event types to get the guild object.
const guildId = args[0].getEvent().guild;
const guild = client.guilds.cache.get(guildId);
const owner = guild?.fetchOwner();
owner.send("API is offline");
}

});
})
}
function checkApiAvailability() {
return false;
}
import { Client } from "discord.js";
import { readdirSync } from "fs";
import { join } from "path";
import { color } from "../functions";
import { BotEvent } from "../types";

module.exports = (client: Client) => {
let eventsDir = join(__dirname, "../events")

readdirSync(eventsDir).forEach(file => {
if (!file.endsWith(".js")) return;
let event: BotEvent = require(`${eventsDir}/${file}`).default
event.once ?
client.once(event.name, (...args) => event.execute(...args))
:
client.on(event.name, (...args) => {
console.log(...args);
const isApiOffline = checkApiAvailability();
if (isApiOffline) {
//pseudo code
//smthg like this but I dont want to make a switch case over all event types to get the guild object.
const guildId = args[0].getEvent().guild;
const guild = client.guilds.cache.get(guildId);
const owner = guild?.fetchOwner();
owner.send("API is offline");
}

});
})
}
function checkApiAvailability() {
return false;
}
6 replies