I am trying to make my ban count appear in a embed and I am struggling with it. Here is the code:
const log = new Hyperz.MessageEmbed()
.setTitle('Ban Database')
.setColor('#FE2E2E')
.setDescription(`Member Tag: <#${memberbannedid}>\nMember ID: ${memberbannedid}\nTotal Bans: ${message.guild.fetchBans()}`)
.setFooter('Ban Database of DevGuard')
message.guild.channels.cache.get('818633337320767548').send(log)```
fetchBans() returns a collection (once resolved), You are trying to print out the entire collection. To get the count use Collection#size
As Cameron R pointed out, Guild#fetchBans() returns a promise. You will need to resolve the promise first inorder to get the size. Make sure you are in an async function if you plan to use the await method.
const fetchedBans = await message.guild.fetchBans();
//... Your Embed
.setDescription(`Member Tag: <#${memberbannedid}>\nMember ID: ${memberbannedid}\nTotal Bans: ${fetchedBans.size}`)
Related
I'm making a suggestion module on my bot, and I have everything else down, but am struggling on fetching the message the bot sent and editing it using a command that was sent in a different channel.
I tried doing await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' }) but it didn't work.
Your code, while in theory, should work, most likely doesnt due to unresolved promises.
In the line
await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' })
The await at the beginning is trying to resolve the promise of the edit method, where there is another promise to be resolved at the <MessagesManager>#fetch method. In other words you're ignoring the promise returned from the fetch which will resolve no messages. Consider the following change:
const messages = await message.guild.channels.cache.get("channel-id-here").messages;
await messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' });
Or if you really want a one-liner (I dont suggest this as it compromises on readability) you could use:
await (await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here")).edit({ content: 'Test'
So, I want to make a command for my bot which can fetch all reactions from a given message ID and store these three categories into arrays, for each reaction:
the name of the reaction (eg. :smile:)
the users who reacted (eg. RandomUser#4654, ILikePotatoes#1639)
and how many people reacted with this emoji (eg. Count: 2)
I've tried using the ReactionCollector, but it doesn't work for reactions added prior to the event being called. I've also tried using an external module called discord-fetch-all but all it does is either sending Promise { <Pending> } or undefined when I use .then().
PS: I've already set up a command handler that takes a message ID for argument.
Thank you in advance for helping.
You can get the specific message you want to be check for reactions by using:
const messageReacted = client.channels.cache.get('channelId').messages.fetch('messageId')
Then, you can go through each reaction in the message's cached reactions by using forEach
Then, in the .forEach, you can get the emoji name by using reaction._emoji.name, the number of users who used this reaction by reaction.count and all the users who reacted on the message by fetching them: await reaction.users.fetch().
Your final code might look something like:
const messageReacted = await client.channels.cache
.get('channelId')
.messages.fetch("messageId");
messageReacted.reactions.cache.forEach(async(reaction) => {
const emojiName = reaction._emoji.name
const emojiCount = reaction.count
const reactionUsers = await reaction.users.fetch();
});
Instead if fetching every user by itself, I am trying to fetch an array of users at once.
This is the code I am trying to use:
var idsArr= ['682999909523259483','234269235071811584','503303866016727050'];
const membersArr = await interaction.guild.members.fetch({
idsArr,
});
console.log(membersArr);
Even though I only give it an array of 3 IDs, it logs every member of the server.
Any suggestion on how to fix that?
I just searched through official discord.js examples, and found this:
// Fetch by an array of users including their presences
guild.members.fetch({ user: ['66564597481480192', '191615925336670208'], withPresences: true })
.then(console.log)
.catch(console.error);
Maybe you should do this instead:
var idsArr= ['682999909523259483','234269235071811584','503303866016727050'];
const membersArr = await interaction.guild.members.fetch({
user: idsArr,
});
console.log(membersArr);
(Notice field name user instead of idsArr)
if (message.content === "!test"){
client.users.cache.get('id').send('message');
console.log("message sent")
}
This method doesn't work and I wasn't able to find any other methods that worked
Is there a spelling mistake or this method is outdated?
I've actually encountered this error myself once. There are two problems you might be facing here:
The user isn't cached by discord.js yet
There isn't a DMChannel open for that user yet
To solve the first one, you have to fetch the user before doing anything with it. Remember this is a Promise, so you'll have to either await for it to complete or use .then(...).
const user = await client.users.fetch('id')
Once you've fetched your user by their ID, you can then create a DMChannel which, again, returns a promise.
const dmChannel = await user.createDM()
You can then use your channel like you normally would
dmChannel.send('Hello!')
Try to create a variable like this:
var user = client.users.cache.find(user => user.id === 'USER-ID')
And then do this:
user.send('your message')
I am creating a Discord bot that displays player statistics from Hypixel, a Minecraft server. I am trying to add their player's avatars as well, as a thumbnail in the embeds the bot sends. To get the avatars I am using Crafatar. This is the code I am using, however, the thumbnail doesn't show up on the embed. I believe this has something to do with the fact that I am using an URL with a variable in it, as I tried with just a regular URL and it worked fine.
.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)
The variable uuid is declared and assigned to a value further up in my code.
EDIT 1
This is the code where I get the uuid variable from. I can't see anything wrong with it, however, I am not particularly skilled in JavaScript.
var uuid = '';
getId(args[1]).then(id => {
uuid = id;
})
getId is a function defined below, which looks like this:
function getId(playername) {
return fetch(`https://api.mojang.com/users/profiles/minecraft/${playername}`)
.then(data => data.json())
.then(player => player.id);
}
This uses the Mojang API to convert the player's display name, which is entered as a command parameter, to their UUID.
From what you've posted, it seems like the problem is from the fact that you're correctly using Promises to set the value of uuid, but you're not waiting for it to be set before setting your embed thumbnail.
You should wait for it either using Promise.then() (as you did elsewhere), or async/await.
Here's an example:
// Using .then():
getId(args[1]).then(uuid => {
let embed = new MessageEmbed()
// You have to set the thumbnail after you get the actual ID:
embed.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)
// You can now send the embed
})
// Using async/await:
let uuid = await getId(args[1])
let embed = new MessageEmbed()
embed.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)