Bot crashing immediately after sending an embed - discord

Bot was working fine, i was updating every now and then to make some new changes, i Redo'd everything i did back when it was working perfectly yet it kept crashing.
Code:
module.exports = {
name: 'vouch',
description: "this is a vouch command!",
execute(message, args, Discord) {
const client = message.client;
const newEmbed = new Discord.MessageEmbed()
message.channel.send(newEmbed);
if (message.member.roles.cache.has('768903864782553108')) {
const targetID = message.mentions.users.first();
return message.channel.send(new Discord.MessageEmbed()
//.setTitle
.setDescription(`👍You have vouched for **${targetID}**.`)
.setColor('GREEN')
.setFooter(client.user.username, client.user.displayAvatarURL())
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setTimestamp()
.setFooter('Bot was programmed by ~', 'https://i.gyazo.com/04b40914f14d5dba8aebb532ed3e80f3.png')
)
}
}
}
The error:
throw new DiscordAPIError(request.path, data, request.method, res.status);
^
DiscordAPIError: Invalid Form Body
embed.description: This field is required
at RequestHandler.execute (C:\Users~\Desktop\Discordbot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users~\Desktop\Discordbot\node_modules\discord.js\src\rest\RequestHandler.js:39:14) {
method: 'post',
path: '/channels/683675132677718107/messages',
code: 50035,
httpStatus: 400
}

you can just simply do this:
module.exports = {
name: 'vouch',
description: "this is a vouch command!",
execute(message, args, Discord) {
if (message.member.roles.cache.has('768903864782553108')) {
const targetID = message.mentions.users.first();
const newEmbed = new Discord.MessageEmbed()
//.setTitle
.setDescription(`👍You have vouched for **${targetID}**.`)
.setColor('GREEN')
.setFooter(client.user.username, client.user.displayAvatarURL())
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setTimestamp()
.setFooter('Bot was programmed by ~', 'https://i.gyazo.com/04b40914f14d5dba8aebb532ed3e80f3.png')
message.channel.send(newEmbed)
}
}
}
EDIT: and as a side note the error originated where you called a new embed but didn't give any values for it.
const newEmbed = new Discord.MessageEmbed()
message.channel.send(newEmbed);

Related

Embed stated not being sent to channel

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const { EmbedBuilder } = require('discord.js');
// All partials are loaded automatically
//const Discord = require('discord.js');
client.on('ready', async () => {
console.log(`${client.user.username} is ready!`);
})
client.on('guildCreate', (g) => {
const channel = g.channels.cache.find(channel => channel.type === 'GUILD_TEXT' && channel.permissionsFor(g.me).has('SEND_MESSAGES'))
channel.send("Thanks for inviting flappy dank! Please run the command !run to get started!");
})
client.on("messageCreate", async (message) => {
if (message.content == '!testcmd') {
const illla = new EmbedBuilder()
.setColor(FF0000)
.setTitle('Members Generator 2.0!')
.setDescription('testing testing 123 123')
.setTimestamp()
.setFooter({ text: 'wow a footer'});
message.channel.send(illla)
}
})
I have tried the code above, when I run the command ‘!testcmd’, it does not output any embed. i have searched online for solutions, but none working. I expect and embed to be outputted, yet it doesn’t return any errors. Any advice would be appreciated!
You're using discord.js v14. There's a changes about sending Embeds
const illla = new EmbedBuilder()
.setColor(FF0000)
.setTitle('Members Generator 2.0!')
.setDescription('testing testing 123 123')
.setTimestamp()
.setFooter({ text: 'wow a footer'});
message.channel.send(illla) //The Changes is in here
The comment line should change as:
message.channel.send({embeds: [illla] });
Click here to see more about EmbedBuilder

Log when an invite is created (discord.js)

I am trying to log whenever a invite is created and want this to apply per guild (meaning that whenever a invite is created in guild a, it logs it. If a guild is created in guild b, it only logs it in that server.)
I am having a issue,
Issue: ReferenceError: maxAge is not defined
Here is my code:
const Discord = require("discord.js");
let config = require("../config.json");
const { MessageEmbed } = require("discord.js");
const client = new Discord.Client();
module.exports = {
name: `printinvs`,
description: `Prints invites created.`,
async execute(message, args){
message.guild.fetchInvites()
.then
(invites => {
const printinvites = new Discord.MessageEmbed()
.setTitle(`Invite Information`)
.addField(`Max Age: ${maxAge}`)
.addField(`Max Uses: ${maxUses}`)
.addField(`Temporary: ${temporary}`)
.addField(`Invite URL: ${inviteurl}`)
.addField(`Invite created by: ${inviter}`)
.addField(`Channel: ${invite.channel}`)
.addField(`Time invited created: ${invite.createdAT}`)
.setColor(`RANDOM`)
message.client.channels.cache.get(channelid).send(printinvites);
})
}
}
This error is occuring because you never declared the latest invite, and you're using its properties without the actual invite.
Try this code:
let config = require("../config.json");
const { MessageEmbed } = require("discord.js");
const client = new Discord.Client();
module.exports = {
name: `printinvs`,
description: `Prints invites created.`,
async execute(message, args){
message.guild.fetchInvites()
.then
(invites => {
let latestInvite = invites.last();
const printinvites = new Discord.MessageEmbed()
.setTitle(`Invite Information`)
.addField(`Max Age: ${latestInvite.maxAge}`)
.addField(`Max Uses: ${latestInvite.maxUses}`)
.addField(`Temporary: ${latestInvite.temporary}`)
.addField(`Invite URL: ${latestInvite.url}`)
.addField(`Invite created by: ${latestInvite.inviter}`)
.addField(`Channel: ${latestInvite.channel}`)
.addField(`Time invited created: ${latestInvite.createdAt}`)
.setColor(`RANDOM`)
message.client.channels.cache.get(channelid).send(printinvites);
})
}
}

discord.js image search with pages

is there a way to add pages for this command.
e.g | https://gyazo.com/e6782fc9386f9d15c7cc52dabeb8844e (it can be with reactions or buttons)
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "img",
description: "Search for an image!",
category: "utility",
cooldown: {type: "map", time: 10},
aliases: ["is", "imgsearch"],
run: async (client, message, args) => {
if (!args) client.err(message);
gis(args.join(" "), logResults);
async function logResults(error, results){
if (error)return client.err(message);
let random = Math.floor(Math.random() * results.length);
let image = results[random].url
const embed = new MessageEmbed()
.setImage(image)
.setColor("#2f3136");
return message.reply(embed);
}
}
}
The easiest solution would be to add two reactions '⬅️, ➡️', and await till user reacts with one of them. (working with reactions is nicely described here)
Your code would look like this:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "img",
description: "Search for an image!",
category: "utility",
cooldown: {type: "map", time: 10},
aliases: ["is", "imgsearch"],
run: async (client, message, args) => {
if (!args) client.err(message);
gis(args.join(" "), logResults);
async function logResults(error, results){
if (error)return client.err(message);
let random = Math.floor(Math.random() * results.length);
let image = results[random].url
const embed = new MessageEmbed()
.setImage(image)
.setColor("#2f3136");
// Awaiting till the message gets sent so we can add reactions & await users to react
const msg = await message.reply(embed);
// Adding reactions
await msg.react('⬅️');
await msg.react('➡️');
// Create a filter so only when original author reacts with ⬅️ or ➡️ the message is edited.
const filter = (reaction, user) => {
return ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === message.author.id;
};
// Await until user reacts
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '⬅️') {
await msg.reactions.removeAll();
let image = results[random-1].url
const embed = new MessageEmbed()
.setImage(image)
.setColor("#2f3136");
msg.edit(embed);
} else {
await msg.reactions.removeAll();
let image = results[random+1].url
const embed = new MessageEmbed()
.setImage(image)
.setColor("#2f3136");
msg.edit(embed);
}
})
.catch(collected => {
message.reply('Your time to open next or previous image expired!');
});
}
}
}

how to mention a user

I want to make a report command for my discord bot, so I want it to tell me who is doing the report. In other words, who executed the command. My current code is:
const Discord = require('discord.js');
const { User, ClientUser, GuildMember, TeamMember, Message} = require("discord.js");
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "USER", "REACTION"]});
const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
module.exports = {
name: 'report',
description: "report a naughty person",
execute(message, args){
message.channel.send(`#${String(GuildMember.User)} reported:`);
}}
But when you use the command it says:
#undefined reported:
How do I make it define who it is?
You can use message.author to get the user and
`${message.author}`
to mention them.
message.channel.send(`${message.author} reported:`)
This is pretty simple, you just have to create a new const, which will be the following:
const author = message.author.tag
After that, just replace this statement.
message.channel.send(`${author} reported:`);
And your code should end up looking like this:
const Discord = require('discord.js');
const { User, ClientUser, GuildMember, TeamMember, Message} = require("discord.js");
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "USER", "REACTION"]});
const guild = client.guilds.cache.get(process.env.DISCORD_SERVER_ID);
const author = message.author.tag
module.exports = {
name: 'report',
description: "report a naughty person",
execute(message, args){
message.channel.send(${author} reported:);
}
}
I hope this helped!

How to dm user before executing a ban in discord.js?

Can someone please tell me how I can send a user a dm before he gets banned? Everything works fine, but the dm. It seems, that if i put the dm code where it is now, the user gets banned first, then gets a direct message which obviously does not work because the bot and the user do not share a server.
My Code:
const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'ban',
description: "Allows you to ban a member from the server!",
execute(message, args) {
const member = message.mentions.users.first();
const notBanYourself = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("You cannot ban yourself!")
.setFooter(message.author.username)
.setTimestamp()
;
if(member.id === message.author.id) return message.channel.send(notBanYourself);
if(message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")){
const memberTarget = message.guild.members.cache.get(member.id);
const ban_dm = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
.setFooter(message.author.username)
.setTimestamp()
;
memberTarget.send(ban_dm)
if(member) {
memberTarget.ban({ days: 3, reason: args.slice(1).join(" ") });
const banned = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("User has been banned from the server successfully.")
.setFooter(message.author.username)
.setTimestamp()
;
message.channel.send(banned)
}
else {
const notBanned = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("There was an error: You need to mention the user, which you want to ban! \n If you did that, you probably do not have the permission to ban users.")
.setFooter(message.author.username)
.setTimestamp()
;
message.channel.send(notBanned)
}
}
}}
Try using .then(), to make it so that the ban only occurs after the message.channel.send() event has finished.
const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'ban',
description: "Allows you to ban a member from the server!",
execute(message, args) {
const member = message.mentions.users.first();
const notBanYourself = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("You cannot ban yourself!")
.setFooter(message.author.username)
.setTimestamp()
if (member.id === message.author.id) return message.channel.send(notBanYourself);
if (message.member.permissions.has("BAN_MEMBERS") || message.member.permissions.has("ADMINISTRATOR")) {
const memberTarget = message.guild.members.cache.get(member.id);
const ban_dm = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("You got banned from the " + message.guild.name + " Server by " + message.author.username)
.setFooter(message.author.username)
.setTimestamp()
memberTarget.send(ban_dm).then(() => {
if (member) {
memberTarget.ban({ days: 3, reason: args.slice(1).join(" ") });
const banned = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("User has been banned from the server successfully.")
.setFooter(message.author.username)
.setTimestamp()
message.channel.send(banned);
} else {
const notBanned = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Ban")
.setDescription("There was an error: You need to mention the user, which you want to ban! \n If you did that, you probably do not have the permission to ban users.")
.setFooter(message.author.username)
.setTimestamp()
message.channel.send(notBanned);
}
});
}
}
}

Resources