Need Help for Fetch Ban CreatedAt

const { Events } = require('discord.js')

module.exports = async (client) => {


// UNBAN COMMAND
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isAutocomplete()) return;
if (interaction.commandName !== 'server') return;

const guild = interaction.guild;
if (!guild) return; // Make sure interaction is within a guild

try {
await guild.members.fetch(); // Make sure all guild members are cached
const bans = await guild.bans.fetch() // Fetch bans from the guild
const focusedValue = interaction.options.getFocused();

let filteredBans;
if (!isNaN(focusedValue)) {
// If the input is a number, search by ID
filteredBans = bans.filter(ban =>
ban.user.id.includes(focusedValue));
} else {
// Otherwise, search by username and reason
filteredBans = bans.filter(ban => {
const user = ban.user;
return user.username.toLowerCase().startsWith(focusedValue.toLowerCase()) ||
(ban.reason && ban.reason.toLowerCase().includes(focusedValue.toLowerCase()));
});
}

const results = filteredBans.map(ban => ({
name: `${ban.user.tag} - ${ban.user.id} - ${ban.reason ?? 'Kein Grund angegeben'} - ${new Date(ban.createdAt).toLocaleString()}`,
value: ban.user.id
}));

interaction.respond(results.slice(0, 25)).catch(() => {});
} catch (error) {
console.error('Error fetching bans:', error);
interaction.respond({ content: 'An error occurred while fetching bans.', ephemeral: true }).catch(() => {});
}
});


}
const { Events } = require('discord.js')

module.exports = async (client) => {


// UNBAN COMMAND
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isAutocomplete()) return;
if (interaction.commandName !== 'server') return;

const guild = interaction.guild;
if (!guild) return; // Make sure interaction is within a guild

try {
await guild.members.fetch(); // Make sure all guild members are cached
const bans = await guild.bans.fetch() // Fetch bans from the guild
const focusedValue = interaction.options.getFocused();

let filteredBans;
if (!isNaN(focusedValue)) {
// If the input is a number, search by ID
filteredBans = bans.filter(ban =>
ban.user.id.includes(focusedValue));
} else {
// Otherwise, search by username and reason
filteredBans = bans.filter(ban => {
const user = ban.user;
return user.username.toLowerCase().startsWith(focusedValue.toLowerCase()) ||
(ban.reason && ban.reason.toLowerCase().includes(focusedValue.toLowerCase()));
});
}

const results = filteredBans.map(ban => ({
name: `${ban.user.tag} - ${ban.user.id} - ${ban.reason ?? 'Kein Grund angegeben'} - ${new Date(ban.createdAt).toLocaleString()}`,
value: ban.user.id
}));

interaction.respond(results.slice(0, 25)).catch(() => {});
} catch (error) {
console.error('Error fetching bans:', error);
interaction.respond({ content: 'An error occurred while fetching bans.', ephemeral: true }).catch(() => {});
}
});


}
I have a problem with the
${new Date(ban.createdAt).toLocaleString()}
${new Date(ban.createdAt).toLocaleString()}
its respons with "Invalid Date" and i dont now how to get the ban date on other ways, can someone help me?
No description
7 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
𝑫𝒆𝒂𝒕𝒉
<GuildBan>.createdAt doesn't exists
No description
Marc-Anthony
Marc-AnthonyOP9mo ago
Ah okay, and what can i do then to get the date? Or is that even possible?
𝑫𝒆𝒂𝒕𝒉
I think you can't
Marc-Anthony
Marc-AnthonyOP9mo ago
ah okay, thank you for the help!
𝑫𝒆𝒂𝒕𝒉
No problem sir
Want results from more Discord servers?
Add your server