MessageEmbed is not a constructor - discord

I'm getting an error "MessageEmbed is not a constructor" it works with only sending a message, but not with an embed. What could be this issue?
const MessageEmbed = require('discord.js');
const { Manager } = require('erela.js');
const nodes = require('./Nodes');
module.exports = (client) => {
client.manager = new Manager({
nodes,
send(id, payload) {
const guild = client.guilds.cache.get(id);
if (guild) guild.shard.send(payload);
}
})
.on('nodeConnect', node => console.log(`Node ${node.options.identifier} connected.`))
.on('nodeError', (node, error) => console.log(`Node ${node.options.identifier} had an error: ${error.message}.`))
.on('trackStart', (player, track) => {
client.channels.cache
.get(player.textChannel)
.send(new MessageEmbed().setDescription(`Now playing: \`${track.title}\``).setColor('ORANGE'));
})
.on('queueEnd', (player) => {
client.channels.cache
.get(player.textChannel)
.send(new MessageEmbed().setDescription('The queue has ended. Leaving the channel.').setColor('ORANGE'));
player.destroy();
});
};

const MessageEmbed = require("discord.js")
is incorrect. The correct way is:
const { MessageEmbed } = require("discord.js")
Note the {}. The curly braces are a destructuring assignment. When you import or require discord.js, it is importing an object with everything in discordjs. By using destructuring assignment, you are only picking out the parts you require.

Related

Why is data not being saved in the array? | discord.js v13

Whenever I'm attempting to push data into an array, then log it after the for loop it just prints an empty array. Why is that?
const discord = require('discord.js');
const db = require('quick.db');
const fs = require('fs');
module.exports = {
name: 'XYZ',
run: async (client, message) => {
var array_V = ['ID1', 'ID2'];
var snowflakes = [];
var i = 0;
message.guild.channels.cache.forEach(channel => {
if (!channel.isText()) return;
for (const channel of message.guild.channels.cache.values()) {
let messages = await channel.messages.fetch()
messages.each((msg) => {
if (msg.author.id === array_V[i]) {
snowflakes.push(msg.createdTimestamp)
}
})
}
});
}
}
Now outputs SyntaxError: await is only valid in async functions and the top level bodies of modules although it is already declared as an async function in the header.
You are pushing it in a .then. Use await and for loops to have it update and then run the next code
for (const channel of message.guild.channels.cache.filter(c => c.type === "GUILD_TEXT").values()) {
let messages = await channel.messages.fetch()
messages.each((msg) => {
if (msg.author.id === array_V[i]) {
snowflakes.push(msg.createdTimestamp)
}
})
}
This should work since everything asynchronous is being awaited in the for loop

How can I fix this error "Cannot set properties of undefined"

const { Client, Intents, MessageEmbed } = require('discord.js');
let client = new Client({ intents: [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES] });
const dotenv = require('dotenv');
const Keyv = require('keyv');
const keyv = new Keyv();
dotenv.config();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async (msg) => {
let number = msg.content.split(' ')[1];
if (msg.content === '!ping') {
msg.channel.send('ping!')
}
`
const getGuildPrefix = async () => {
const prefixMap = await keyv.get('prefix');
return prefixMap ?. [msg.guild.id] || "!"
}
// Sets the prefix to the current guild.
const setGuildPrefix = async (prefix) => {
const prefixMap = await keyv.get('prefix');
prefixMap[msg.guild.id] = prefix;
await keyv.set('prefix', `${prefixMap}`);
}
let prefix = await getGuildPrefix();
// Get prefix command.
if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${prefix}prefix`)) {
msg.channel.send(`Your server prefix is ${prefix}`)
}
})
client.login(process.env.token);
So what this code does is retreive prefix for a particular server in which different server have different prefix, so I used keyv package for storing the prefix, but I am getting an error and this is the error message
prefixMap[msg.guild.id] = prefix;
^
TypeError: Cannot set properties of undefined (setting '857122654976606239')
at setGuildPrefix
Here if the value in the key is empty, then it is supposed to return the value "!" by default. So this is almost the same code I did yesterday, but I edited the code a little bit to fix the error in getGuildPrefix, and now I am getting this error. Both the errors are caused because of undefined but I used optional chaining to fix the first one, and the getGuildPrefix is working fine, but there is now an error in the setGuildPrefix part. The thing is even if PrefixMap is undefined, the value is supposed to be "!", the code is in that way (I assume, but not very sure though). How can I fix this error?
I think your logic syntax might be wrong here, try
const getGuildPrefix = async () => {
const prefixMap = await keyv.get('prefix');
if(!prefixMap[msg.guild.id]) {
return prefixMap[msg.guild.id];
}
else {
return '!';
}
};
the value which is supposed to return is !, but we gave it only in the getGuildPrefix, but we cant do optional chaining in the setGuildPrefix because it gives an error like left hand, so if keyql doesn't return a value from db again, then we can iniatite it again within the if statement again but using another if statement. we cant use this technique in the getGuildPrefix idk why, but we can use this in setGuildPrefix and we can use optional chaining in getGuildPrefix and now the error is fixed, and the code is working functionally
client.on('messageCreate', async (msg) => {
let number = msg.content.split(' ')[1];
if (msg.content === '!ping') {
msg.channel.send('ping!')
}
// Use this function to get the prefix in other files.
// Use like `const prefix = await getGuildPrefix();`
const getGuildPrefix = async () => {
const prefixMap = await keyv.get('prefix');
return prefixMap ?. [msg.guild.id] || "!"
}
// Sets the prefix to the current guild.
const setGuildPrefix = async (prefix) => {
let prefixMap = await keyv.get('prefix');
if (!prefixMap)
{
prefixMap = "!";
}
prefixMap[msg.guild.id] = prefix;
await keyv.set('prefix', `${prefixMap}`);
}
let prefix = await getGuildPrefix();
// Get prefix command.
if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${prefix}prefix`)) {
msg.channel.send(`Your server prefix is ${prefix}`)
}
})
client.login(token)

How would I get all the members from a server using discord.js

I've been trying to get all the members from a server and list them into the console.
It's been about an hour of me searching around and tweaking code, but I just can't seem to
get it to work.
Here is my code:
const Discord = require('discord.js');
const client = new Discord.Client();
exports.run = async (bot,message,args) => {
const list = bot.guilds.cache.get(server);
list.members.forEach(member => console.log(member.user.username));
if (member) {
for (user of list.members) {
console.log(user[1].username);
}
}
}
exports.help = {
name: 'memberlog'
}
Any help would be appreciated!
Use this to fetch all the server members from the API, then log them.
const list = bot.guilds.cache.get('serverId')
const members = await list.members.fetch();
members.each((member) => {
console.log(member.user.username);
})

Trying to display JSON data from API as an array

So I've been trying to make a Discord Bot in Node.js, Im fetching data from an external api and using the BOT to display the data, problem is that when I call the function, its showing one by one instead of everything in one message.
I want to display everything in one message.
const token = 'my discord token is here';
const Discord = require('discord.js');
const axios = require('axios');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on('message', async msg => {
if (msg.content === '!inventario') {
let getInv = async () => {
let response = await axios.get('my api link where im getting the info is here')
let inventario = response.data
return inventario
}
let inventarioValue = await getInv ()
var inv = inventarioValue.total_inventory_count
msg.channel.send(`Total de Itens no inventário: ${inventarioValue.total_inventory_count} \nSkins:\n`);
for (var i=0;i<inv;i++)
{
var itens = inventarioValue.descriptions[i].market_name
msg.channel.send(itens);
}
}
});
client.login(token);
I want to execute this inventarioValue.descriptions[i].market_name, then after executing showing the full result instead of showing one by one.
Thanks
You can map object array element and add to message. For a better visual reflection, you can add them to embed.
const token = 'my discord token is here';
const Discord = require('discord.js');
const axios = require('axios');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on('message', async msg => {
if (msg.content === '!inventario') {
let getInv = async () => {
let response = await axios.get('my api link where im getting the info is here')
let inventario = response.data
return inventario
}
let inventarioValue = await getInv()
var inv = inventarioValue.total_inventory_count
let embed = new Discord.MessageEmbed()
embed.setDescription(`${inventarioValue.descriptions.map(val => val.market_name).join('\n')}`)
msg.channel.send(`Total de Itens no inventário: ${inventarioValue.total_inventory_count} \nSkins:\n`, embed);
}
});
client.login(token);

ReferenceError: message is not defined

const Discord = require ('discord.js');
const bot = new Discord.Client();
const token = 'TOKEN';
bot.on('ready', () =>{
console.log('Online');
if(msg.includes('hi')) {
message.delete();
message.author.send('hello')
}
})
bot.login(token);
You need to put your code into a .on() function. For example, to listen for messages:
const Discord = require ('discord.js');
const bot = new Discord.Client();
const token = 'TOKEN';
bot.on('ready', () =>{
console.log('Online');
})
bot.on('message', (message) => {
if(message.includes('hi')) {
message.delete();
message.author.send('hello')
}
})
bot.login(token);
Note you can't use msg and message, you must choose one variable name.

Resources