Zer0
Zer0
DIAdiscord.js - Imagine a boo! 👻
Created by Zer0 on 12/1/2023 in #djs-questions
Bot will not .editReply() it's message.
No description
96 replies
DIAdiscord.js - Imagine a boo! 👻
Created by Zer0 on 5/2/2023 in #djs-questions
Cannot read properties of undefined (reading 'length') discord.js
Attached below is my code, you enter a roblox username and it fetches the ID, however it only works once. Then gives the properties error.
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
const axios = require('axios');
const PREFIX = "!";


client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (message) => {
if (message.content.startsWith(PREFIX)) {
const [command, username] = message.content
.substring(PREFIX.length)
.split(' ');

if (command === 'id') {
try {
// Clear the cache before making the API request
client.users.cache.clear();

const lowercaseUsername = username.toLowerCase();
const response = await fetch(`https://users.roblox.com/v1/users/search? keyword=${encodeURIComponent(lowercaseUsername)}`);
const { data } = await response.json();
if (data.length === 0) {
message.channel.send(`Could not find a user with the name "${username}".`);
} else {
const { id } = data[0];
console.log(`The user "${username}" has the Roblox ID ${id}.`);
message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
}
} catch (error) {
console.error(error);
message.channel.send(`There was an error searching for the user "${username}".`);
}
}
}
});
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
const axios = require('axios');
const PREFIX = "!";


client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});


client.on('messageCreate', async (message) => {
if (message.content.startsWith(PREFIX)) {
const [command, username] = message.content
.substring(PREFIX.length)
.split(' ');

if (command === 'id') {
try {
// Clear the cache before making the API request
client.users.cache.clear();

const lowercaseUsername = username.toLowerCase();
const response = await fetch(`https://users.roblox.com/v1/users/search? keyword=${encodeURIComponent(lowercaseUsername)}`);
const { data } = await response.json();
if (data.length === 0) {
message.channel.send(`Could not find a user with the name "${username}".`);
} else {
const { id } = data[0];
console.log(`The user "${username}" has the Roblox ID ${id}.`);
message.channel.send(`The user "${username}" has the Roblox ID ${id}.`);
}
} catch (error) {
console.error(error);
message.channel.send(`There was an error searching for the user "${username}".`);
}
}
}
});
9 replies