CMD sending multiple embeds instead of one - node.js

I made a leaderboard command for my discord bot (v13) but it sends multiple embeds with each user's info individually instead of one whole message. I'm not sure how else to structure this so any help is appreciated.
const profileModel = require("../models/profileSchema");
module.exports = {
name: "leaderboard",
description: "Checks the leaderboard",
aliases: ['lb'],
async execute(client, message, args, cmd, Discord, profileData){
const lbData = await profileModel.find({}).sort({
reputation: -1,
}).limit(5);
for (let counter = 0; counter < lbData.length; ++counter) {
const { userID, health = 0 } = lbData[counter]
const Embed = new Discord.MessageEmbed()
.setColor('#fffffa')
.setTitle('Challenge Leaderboard')
.addFields(
{ name: '\u200b', value: `**${counter + 1}.** <#${userID}> - ${reputation} reputation\n` })
message.channel.send({ embeds: [Embed] });
}
}
}

The reason is the way you send the embed
Inside your for loop you create a new ebed every time and add the fields, where you need to create the embed outisde the for loop, add the field in the for loop and then send the embed
It would look something like:
const Embed = new Discord.MessageEmbed()
.setColor('#fffffa')
.setTitle('Challenge Leaderboard')
for (let counter = 0; counter < lbData.length; ++counter) {
const { userID, health = 0 } = lbData[counter]
Embed.addField({ name: '\u200b', value: `**${counter + 1}.** <#${userID}> - ${reputation} reputation\n` })
}
message.channel.send({ embeds: [Embed] });

Related

Trying to catch response but why wont it send?

I am currently working on a command right now that I would like the server owner to use. When done it sends the embed and will wait for the server owner to paste we will say the channel id. I want to label this to call in another command but at the moment it sends the embed but when I send a channel ID nothing happens. No message is sent but it also does not break the bot. I am still able to send the command again and again.
Please do not mind the slop, I am new to all this and trying to learn as I go any tips would be appreciated!
EDIT: I am using version 12 (12.5.3)
const { prefix } = require('../config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'setchannel',
description: 'command to assign bot to channel',
execute(message, args) {
if(message.channel.type == "dm") {
const keepinservercommandembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('**Im Sorry!**')
.addFields(
{ name: `**Can` + `'t accept: ${prefix}setchannel**`, value: 'Please keep this command to the server!' },
)
message.channel.send(keepinservercommandembed);
}
else {
const messagesender = message.member.id;
const serverowner = message.guild.owner.id;
const setchannelembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Let's get searching!**`, value: `Hello ${message.guild.owner}, what channel would you like me to post group finding results?` },
)
const notownerembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Whoops!**`, value: `Sorry only ${message.guild.owner} can use this command.` },
)
if(messagesender == serverowner) {
message.delete ();
const filter = m => m.content.message;
const collector = message.channel.createMessageCollector(filter, { time: 15000 });
message.channel.send(setchannelembed).then(() => {
collector.on('collect', m => {
message.channel.send(`${m.content}`)
});
});
}
else {
message.delete ();
message.channel.send(notownerembed);
}
}
}
}
After comparing your code to my similiar discord.js v13 code and checking out the v12 docs, I think I found the problem.
I don't think m.content.message is a function. If your aim is for someone to paste the channel ID, the filter code would be:
const filter = m => m.author.id === message.guild.owner.id;
To filter out other people who may be entering an ID (assuming the input is valid).
Use the filter as follows:
message.channel.createMessageCollector({filter: filter, time: 15000 });
To filter out invalid inputs:
collector.on('collect', m => {
if (isNaN(m)) return; //all channel id's in discord are integers (numbers)
else message.channel.send(`${m.content}`);
});

Discord.js Bot doesn't ping a user or role when mentioned

Have this simple slash command which includes pinging/mentioning <#!${interaction.member.id}> but it doesn't ping the mentioned user/role at all. I did enabled Mention #everyone, #here, and All Roles and tried to mention different users and roles still, it's not working.
This is the code:
run: async (client, interaction, args) => {
if(!interaction.member.permissions.has('MANAGE_CHANNELS')) {
return interaction.followUp({content: 'You don\'t have the required permission!'}).then(msg => {
setTimeout(() => {
msg.delete()
}, 3000)
})
}
const [subcommand] = args;
const embedevent = new MessageEmbed()
if(subcommand === 'create'){
const eventname = args[1]
const prize = args[2]
const sponsor = args[3]
embedevent.setDescription(`__**Event**__ <a:w_right_arrow:945334672987127869> ${eventname}\n__**Prize**__ <a:w_right_arrow:945334672987127869> ${prize}\n__**Donor**__ <a:w_right_arrow:945334672987127869> ${sponsor}`)
embedevent.setFooter(`${interaction.guild.name}`, `${interaction.guild.iconURL({ format: 'png', dynamic: true })}`)
embedevent.setTimestamp()
}
return interaction.followUp({content: `<#!${interaction.member.id}>`, embeds: [embedevent]})
}
}
The command is working just fine but the only problem is the pinging/mentioning.
I'm not into interactions, but based on my experience doing bot, you should add the code first
const pingmember = interaction.mentions.members.first()
then you can do now
return interaction.followUp({content: `${pingmember}`, embeds: [embedevent]})
or
const pingmember = interaction.mentions.members.first()
if(pingmember) {
//your code here
}

I just created a snipe command for my bot but it doesn't quite work

My messageDelete event handler:
client.on("messageDelete", message => {
snipe.set(message.channel.id, {
title: Date.now(),
content: message.content,
author: message.author,
image: message.attachments.first() ? message.attachments.first().proxyURL : null,
});
});
My snipe.js code:
require('discord-reply');
const Discord = require('discord.js')
const moment = require('moment')
module.exports = {
name: 'snipe',
aliases: [],
category: 'Fun',
utilisation: '{prefix}snipe',
description: 'Displays the last deleted message in the current channel!',
execute(client, message) {
const snipe = require('.././../index.js')
const msg = snipe.get(message.channel.id);
const timeAgo = moment(msg.title).fromNow();
if (!msg) return message.channel.send("Theres Nothing To Snipe here...");
if (msg.image) {
const embed1 = new Discord.MessageEmbed()
.setAuthor(msg.author.tag, msg.author.displayAvatarURL({ dynamic: true }))
.setTitle(`Message deleted by ${msg.author.tag}! (${timeAgo})`)
.setDescription(msg.content)
.setColor(0x3498DB)
.setTimestamp()
.setImage(msg.image)
.setFooter("Sniped by " + message.author.tag);
message.lineReply(embed1);
}
else {
const embed = new Discord.MessageEmbed()
.setAuthor(msg.author.tag, msg.author.displayAvatarURL({ dynamic: true }))
.setTitle(`Message deleted by ${msg.author.tag}! (${timeAgo})`)
.setDescription(msg.content)
.setColor(0x3498DB)
.setTimestamp()
.setFooter("Sniped by " + message.author.tag);
message.lineReply(embed);
}
},
}
I am getting the following error in my console when I try running the command
(node:6928) UnhandledPromiseRejectionWarning: TypeError: snipe.get is not a function
Can anyone help? I'm pretty sure my code is correct as I used the same thing in my old bot. Any help would be appreciated.
From what I assume your snipe is a Map/Collection since you are using get/set on it, you may want to export the map to use it in your command file by binding it to your client like so
const snipe = new Map();
Aliter
const { Client, Collection } = require("discord.js");
const snipe = new Collection()
const client = new Client();
// Binding to Client
client.snipe = snipe // aliter declare it directly as client.snipe = new Collection()
Then you may access your collection since you are already exporting client like so
client.snipe.set()
client.snipe.get()

Sending a newly created emoji after guild.emojis.create()

I have an addemoji command and I want it to send the new emoji created after the bot creates it. It sends like :emojiname: instead of the actual emoji added. How can I define the new emoji added? Somebody suggested that I use console.log but I have no idea how to use that information that it puts inside the log.
module.exports = {
name: 'addemoji',
description: 'ping pong',
execute(message, args) {
const Discord = require('discord.js');
const PREFIX = 'ly?';
const load = '<a:loading:824515478939762699>';
if (message.content.startsWith(PREFIX + 'addemoji')) {
if (message.guild.me.permissions.has('USE_EXTERNAL_EMOJIS')) {
if (message.guild.me.permissions.has('MANAGE_EMOJIS')) {
if (message.member.hasPermission('MANAGE_EMOJIS')) {
const match = /<(a?):(.+):(\d+)>/u.exec(message.content);
if (!match)
return message.reply(
'Please include a custom emoji in your message!',
);
// animated will be 'a' if it is animated or '' if it isn't
const [, animated, name, id] = match;
const url = `https://cdn.discordapp.com/emojis/${id}.${
animated ? 'gif' : 'png'
}`;
const user = message.mentions.users.first() || message.author;
const nameid = `<:${name}:${id}>`;
message.guild.emojis.create(url, name);
let newname = console.log(name);
let newid = console.log(id);
const embed = new Discord.MessageEmbed()
.setTitle(`Emoji added! <:${newname}:${newid}>`)
.setColor(0x7732a8);
message.channel.send(embed);
}
}
}
}
}
};
GuildEmojiManager#create() returns a Promise with the newly created emoji. You can access and display the new emoji by handling this promise, then using the <:name:id> wrapper for emoijs
message.guild.emojis.create(url, name).then(newEmoji => {
const embed = new Discord.MessageEmbed()
.setTitle(`Emoji added! <:${newEmoji.name}:${newEmoji.id}>`)
.setColor(0x7732a8)
message.channel.send(embed);
});

Discord.js - Message edit/delete logger error

I'm trying to add a code to my bot that logs whenever a user edits/deletes a message. The bot sends an embed to a specified channel with the information of the event. However, there's this error that I've been stuck on for a long while now, with no identified solution.
Here's the error that follows:
RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.
Here's the code:
client.on("messageUpdate", message => {
var messages = []
if(messages.includes(message.id)){return;}
channel = message.guild.channels.cache.get('channelID')
const channel9 = client.channels.cache.find(channel => channel.id === 'id');
const ediembed = new Discord.MessageEmbed()
.setColor(1752220)
.setTitle(":pencil: Message Edited")
.addFields (
{name: "__Channel:__", value: `<\#${message.channel.id}>`},
{name: "__Message Author:__", value: `${message.author.tag} - <\#${message.author.id}>`},
{name: "__Original Message:__", value: message.content}
)
.setTimestamp()
.setThumbnail(message.author.avatarURL())
.setFooter("super cool api")
channel9.send(ediembed)
}
)
2 fixes,
1: you dont need to get channel
2: try sending to channel a different way
client.on("messageUpdate", (message) => {
var messages = [];
if (messages.includes(message.id)) {
return;
}
const ediembed = new Discord.MessageEmbed()
.setColor(1752220)
.setTitle(":pencil: Message Edited")
.addFields(
{ name: "__Channel:__", value: `<\#${message.channel.id}>` },
{
name: "__Message Author:__",
value: `${message.author.tag} - <\#${message.author.id}>`,
},
{ name: "__Original Message:__", value: message.content }
)
.setTimestamp()
.setThumbnail(message.author.avatarURL())
.setFooter("super cool api");
client.channels.cache.get("CHANNEL ID").send(ediembed);
});

Resources