Discord.js Avatar command - javascript

Hello i am trying to make command to show pfp of mentioned person but i am stuck with that
The code that i am using is this
and has no errors
case 'avatar1':
const user = message.mentions.users.first() || message.author;
const avatarEmbed = new Discord.MessageEmbed()
.setColor("RANDOM")
.setAuthor(user.username)
.setImage(user.avatarURL);
message.channel.send(avatarEmbed);
break;

<User>.avatarURL is a method, therefore it would be <User>.avatarURL()
Documentation: https://discord.js.org/#/docs/main/stable/class/User?scrollTo=avatarURL

Related

Bot sending [object, Object] when trying to send a random embed

I am trying to make it so when I say a command (ex. "!embedzay") the bot will send a random embed
I have tried pretty much all solutions from other posts I found on how to send random embeds or how to fix the [object Object] thing but they just give errors and the bot crashesenter image description here.
I have managed to get sending a single embed and sending random texts to work but I can't get sending random embeds to work.
A heads up that I am very new to JavaScript so if this is a stupid question I am very sorry.
I am using JavaScript and node.js. If you need more information that what is below, please just tell me.
here is my code for random text that works:
//rtest - random test +
if (command === 'rtest'){
const options = [
"message1",
"message2",
"message3",
]
const random = options[Math.floor(Math.random() * options.length)]
message.channel.send(`${random}`)
}
my code for embeds that works:
//embed thingy - send and embed +
if (command === 'embedz'){
message.channel.send({
embeds: [new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
});
}
...And my code for random embeds that won't work:
attempt one - gives [object Object] error
if (command === 'rtest'){
const options = [
{embeds: new EmbedBuilder().setTitle('rtfhgfh')},
"message2",
"message3",
]
const random = options[Math.floor(Math.random() * options.length)]
message.channel.send(`${random}`)
}
attempt two - gives cannot send empty message error
// earlier embed attempt - cannot send empty message
if (command === 'embedzay'){
const embed3 = new EmbedBuilder()
.setTitle('rtfhgfh')
.setDescription('yhhfggf')
const embed4 = new EmbedBuilder()
.setTitle('haha')
.setDescription('bunny')
var embedArr = [embed3, embed4];
let randomEmbed = embedArr[Math.floor(Math.random() * embedArr.length)];
message.channel.send(randomEmbed);
}
attempt three - back to [object Object] error
if (command === 'embedzy'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send(`${random}`)
}
///
attempt four - kinda works?
Using "const myJSON = JSON.stringify();" the correct way this time
it kinda works but stuff shows up as "{"title":"Some titleuhhhhhh","image":{"url":"https://i.imgur.com/64l7KFc.png%22%7D%7D"
if (command === 'embedzye'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send(`${JSON.stringify(random)}`)
}
attempt five - yes it works!!
if (command === 'embedzyel'){
const embed1 = new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')
const embed2 = new EmbedBuilder()
.setTitle('Some titleuhhhhhh')
.setImage('https://i.imgur.com/64l7KFc.png');
const embeds = [embed1, embed2]
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send({embeds: [random],});
}
message.channel.send({
embeds: [new EmbedBuilder()
.setTitle('Some title')
.setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
});
This works because you send your embed packed in "embeds" object.
Edit your code and add "embeds" object.
const random = embeds[Math.floor(Math.random() * embeds.length)]
message.channel.send({
embeds: [random],
});

Discord Bot - MessageEmbed

I am very new to javascript and I don't understand a lot of the actual code.
However, I have one query, how can I make the "cahbResponses" appear in a MessageEmbed?
I've looked at the literature on MessageEmbeds from the Discord JS guides but I still have no clue.
The code I am working on is:
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
var args = message.content.substring('!'.length).split(' ');
switch (args[0].toLowerCase()) {
case 'cahb':
var response =
cahbResponses[Math.floor(Math.random() * cahbResponses.length)];
message.channel
.send(response)
.then()
.catch(console.error);
break;
default:
break;
}
});
First, make sure you are importing Discord
// you probably have this on the beginning of your file
const Discord = require("discord.js");
Then
var response = cahbResponses [Math.floor(Math.random()*cahbResponses .length)];
// Create a MessageEmbed object and set the description
const embed = new Discord.MessageEmbed().setDescription(response);
// Then send the embed
message.channel.send(embed);
Read other MessageEmbed methods and properties here: https://discord.js.org/#/docs/main/stable/class/MessageEmbed

Discord.js - how do I edit message.embed() statements?

I am making a ping command -
It is very simple to code, but I haven't got the slightest idea how to edit the embed I'm using. Here is my code - I'm using a command handler explaining the exports.run statement.
const Discord = require('discord.js')
exports.run = (bot, message, args) => {
const pingUpdate = new Discord.MessageEmbed()
.setColor('#0099ff')
.setDescription('pinging...')
message.channel.send(pingUpdate);
}
exports.help = {
name: 'ping'
}
I need to edit the ping update embed to make the .description edit to perform this (simple ping calculation)
message.channel.send('pinging...').then((m) => m.edit(`${m.createdTimestamp - message.createdTimestamp}ms`))
This would make the description change from 'pinging...' to 'examplepingms'
Thank you in advance
You going right way. But to .setDescription you need create new Embed constructor and add description.
message.channel.send('pinging...').then(msg => {
let embed = new Discord.MessageEmbed() //For discord v11 Change to new Discord.RichEmbed()
.setDescription(`${msg.createdTimestamp - message.createdTimestamp}`)
msg.edit(embed)
})
also, instead of doing msg.createTimeStamp - message.createdTimestamp you could also do bot.ping.toFixed(2)
This should work (dont have time to test rn)
const Embed = new Discord.MessageEmbed()
.setDescription(":one:")
const newEmbed = new Discord.MessageEmbed()
.setDescription(":two:")
// Edit Part Below
var Msg = await message.channel.send(Embed); // sends message
Msg.edit(newEmbed) // edits message with newembed
Edit: realized that im using a older version of discord.js updated to make it work with newer version
Solution seems outdated again, now you should edit embed in message using
Message#edit({embeds:[MessageEmbed#]})
For example:
const oldEmbed = new MessageEmbed();
const messageHandle = await textChannel.send({embeds: [oldEmbed]});
const newEmbed = new MessageEmbed();
messageHandle.edit({embeds:[newEmbed]});
You don't actually have to create a new embed. You can edit the original:
UPDATE: Per the docs, it is recommended to create new embeds, but you use the original embed to pre-populate the new embed. Then, just update what you need and edit the message with the new embed:
// the original embed posted during collected.on('end')
const embed = new MessageEmbed()
.setColor('0xff4400')
.setTitle(`My Awesome Embed`)
.setDescription('\u200b')
.setAuthor(collected.first().user.username, collected.first().user.displayAvatarURL())
.setImage(`https://via.placeholder.com/400x300.png/808080/000000?text=Placeholder`)
.setTimestamp(new Date())
.setThumbnail('https://via.placeholder.com/200x200.png/808080/000000?text=Placeholder');
// In the original embed, I have a placeholder image that the user
// can replace by posting a new message with the image they want
client.on('messageCreate', async message => {
// adding image to original embed
if (message.attachments.size > 0 && !message.author.bot) {
// get all messages with attachments
const messages = await client.channels.cache.get('<CHANNEL>').messages.fetch();
// get the newest message by the user
const d = messages.filter(msg => msg.embeds.length > 0).filter(m => message.author.username === m.embeds[0].author.name).first();
// create new embed using original as starter
const tempEmbed = new MessageEmbed(d.embeds[0])
// update desired values
tempEmbed.setImage(message.attachments.first().url);
// edit/update the message
d.edit({ embeds: [tempEmbed] });
// delete the posted image
message.delete();
}
});

Membercounter won't shows members count

Hi guys I have a problem so when I use the command it should show the number of users but it shows 0. How can i fix it?
Code:
if(command === `${prefix}test`) {
let guild = new Discord.Guild(600811118667235339);
var memberCount = guild.members.filter(member => !member.user.bot).size;
let embed = new Discord.RichEmbed()
.addField("Liczba osob", `${memberCount}`)
message.channel.send(embed)
}
You cannot create a new instance of a Discord.Guild() like this. You need to use something like that:
let guild = message.client.guilds.get("600811118667235339");
It will get the guild which has for ID 600811118667235339.

"Rich is not defined" when trying to build an embed

I made so code to build an embed for my Discord bot, but it doesn't seem to work:
client.on('message', message => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
if (message.content.startsWith('$Commands')) {
let Embed = new Rich.Embed;
let Message = message.guild;
let botEmbed = new Discord.RichEmbed()
.setTitle("Commands From TTB")
.setDescription("**Commands :**")
.setColor("#4dff077")
.setAuthorName("Galak$y#3038")
.setAvatar("https://cdn.discordapp.com/avatars/563449221701959700/8386d5fe48d71898c40244e7a5a66d58.png")
.addField("1. *Ban* = *Bans User Mentioned After Command* ``$ban <Mention User>``")
.addField("2. Kick = Kicks User Mentioned After Command ``$kick <Mention User>``")
.addField("3. what is myavatar = ``$what is my avatar``")
.addFooter("New Commands Coming Soon...")
return;
message.channel.send(botEmbed)
}
});
Here is the error:
Embeds are created using the RichEmbed class constructor like so:
let embed = new Discord.RichEmbed()
When you declare Embed you're using Rich.Embed, and as it says, Rich is undefined.

Categories

Resources