discord.js Buttons to accept an request - discord

const { MessageActionRow, MessageButtons } = require('discord.js')
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('accept')
.setLabel('Aceitar')
.setStyle('SUCESS')
)
.addComponents(
new MessageButton()
.setCustomId('deny')
.setLabel('Recusar')
.setStyle('DANGER')
)
interaction.reply({ content: `a`, components: [row] })
I wan't to make the bot reply with two buttons to people accept and it's getting this error
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Invalid Form Body
data.components[0].components[0].style: This field is required

The proper success style is SUCCESS, not SUCESS. Changing that should result in a valid message component:
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('accept')
.setLabel('Aceitar')
.setStyle('SUCCESS')
)
...

Related

DiscordAPIError: Cannot send an empty message (discord.js v13)

I am coding a bot with the below command and it throws an error, but i don't know where is the error
i tried my best
const Discord = require("discord.js");
const client = new Discord.Client({
allowedMentions: {
parse: ["roles", "users", "everyone"],
},
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.DIRECT_MESSAGES
]
});
const config = require("../config.json");
module.exports = {
name: "quar",
run: async (client, message, args) => {
if (!message.member.permissions.has("ADMINISTRATOR")) return;
let role = message.guild.roles.cache.find(role => role.name === "Quarantined" || role.name === "Quarantine")
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0])
let reason = message.content.split(" ").slice(2).join(" ")
if(!reason) reason = "No reason given."
if(!role) return message.channel.send("❌ This server doesn't have a quarantine role!")
if(!member) return message.channel.send("❌ You didn't mention a member!")
if(member.roles.cache.has(role.id)) return message.channel.send(`❌ That user already quarantined!`)
member.roles.add(role)
.then (() => {
const embed = new Discord.MessageEmbed()
.setTitle(`✅ ${member.user.tag} was quarantined!`)
.setColor("RANDOM")
.setTimestamp()
embed.setFooter(`Requested by ${message.author.username}`)
message.channel.send(embed)
member.send(`You were quarantined in \`${message.guild.name}\`. Reason: ${reason} `).catch(error => {
message.channel.send(`❌ Can't send DM to ${member.user.tag}!`);
});
})
}
}
If I have made a mistake, please help me out
Sorry I don't speak English well
Try the below code out, I made notes along the way to explain as well as improved a small bit of code. I assume this is a collection of 2 separate files which is why they are separated. If they aren't 2 separate files, you do not need the top section, just the bottom.
const {
Client,
Intents,
MessageEmbed
} = require("discord.js");
const client = new Client({
allowedMentions: {
parse: ["roles", "users", "everyone"],
},
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGES
]
});
const config = require("../config.json");
const {
MessageEmbed,
Permissions
} = require("discord.js");
module.exports = {
name: "quar",
run: async (client, message, args) => {
if (!message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return;
let role = message.guild.roles.cache.find(role => role.name === "Quarantined" || role.name === "Quarantine")
let member = message.mentions.members.first() || message.guild.members.cache.get(args[0])
let reason = message.content.split(" ").slice(2).join(" ")
if (!reason) reason = "No reason given."
if (!role) return message.channel.send("❌ This server doesn't have a quarantine role!")
if (!member) return message.channel.send("❌ You didn't mention a member!")
if (member.roles.cache.has(role.id)) return message.channel.send(`❌ That user already quarantined!`)
member.roles.add(role)
.then(() => {
const embed = new MessageEmbed()
.setTitle(`✅ ${member.user.tag} was quarantined!`)
.setColor("RANDOM")
.setTimestamp()
.setFooter({
text: `Requested by ${message.author.username}`
})
// author and footer are now structured like this
// .setAuthor({
// name: 'Some name',
// icon_url: 'https://i.imgur.com/AfFp7pu.png',
// url: 'https://discord.js.org',
// })
// .setFooter({
// text: 'Some footer text here',
// icon_url: 'https://i.imgur.com/AfFp7pu.png',
// })
message.channel.send({
embeds: [embed]
}) //embeds must be sent as an object and this was the actual line that was causing the error.
member.send(`You were quarantined in \`${message.guild.name}\`. Reason: ${reason} `).catch(() => { // if error is not used, it is not required to be stated
message.channel.send(`❌ Can't send DM to ${member.user.tag}!`);
});
})
}
}

Discord.js [not responding when message is sent after a button was clicked]

So I'm making a command that starts with a slash command, then you need to press a button, the bot send a emebed when you press the button, and then it waits for a message, and when the user sends a message the bot responds.
So here is how it goes:
slahs command
emebed with a button
chek if button is pressed and then send another embed
wait for users message check it and then sends something
Here is the code for the button message:
`client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'reactor') {
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('Start')
.setLabel('Start')
.setStyle('PRIMARY'),
);
const exampleEmbed = new MessageEmbed()
.setTitle('Now you will the to grab the materials')
.setDescription("Type out the words that will be given bt don't be too slow")
await interaction.reply({ embeds: [exampleEmbed], components: [row] });
}
});`
Here is the code that cheks if the button is pressed sends a message and then waits for a spesific users message:
`client.on("message", message => {
if (!message.isButton()) return; // cheking if is button
if (message.customId == "Start") { //if button clicked is start do this
const exampleEmbed = new MessageEmbed() //creating a message
.setDescription('Type out the words that will be given')
.setThumbnail('https://cdn.discordapp.com/attachments/844145839898492929/953741341186228354/Hk86.png')
await interaction.reply({embeds: [exampleEmbed]}); //print out the embed
message.channel.awaitMessages(m => m.author.id == message.author.id, //cheking the next message
{ max: 1, time: 30000 }).then(collected => { //sets timer and then collect info
if (collected.first().content.toLowerCase() == 'Hk86') { //chek if message is yes
message.reply('it works!');
}
});
}
});`
And here are the imports:
const { Client, Collection, Intents, MessageActionRow, MessageButton, MessageEmbed} = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: \[Intents.FLAGS.GUILDS\] });
client.commands = new Collection();
I think the reason the bot doesn't send a message is because you didn't include the GUILD_MESSAGES intent.
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS", "DIRECT_MESSAGES", "GUILD_BANS", "GUILD_MEMBERS", "GUILD_VOICE_STATES"], //must use intents
allowedMentions: ["users"] //stops people from using +say #everyone
});
That's my code for my client.

Cannot read property 'MessageButton' of undefined (discord.js)

I have a probleme, I don't know why but when I use the command "!button", I have an error, there is my code for check it:
const discord = require('discord.js');
const client = new discord.Client();
const disbut = require('discord-buttons')(client);
client.on("message", async (message) => {
if (message.content == "!button") { // Use this command only once and only on one channel.
let buttons = new disbut.MessageButton()
.setStyle('green') // Button Color
.setLabel('Test') // Button Name
.setID('Button') // Button ID
message.channel.send('Message Text.', { buttons: [buttons] });
}
if (message.content == "!urlbutton") { // Use this command only once and only on one channel.
let buttons2 = new disbut.MessageButton()
.setStyle('url') // Button Url
.setLabel('Discord') // Button Name
.setURL('https://discord.com') // URL for forwarding
.setDisabled()
message.channel.send('Message Text.', { buttons: [buttons2] });
}
});
the error:
let buttons = new disbut.MessageButton()
^
TypeError: Cannot read property 'MessageButton' of undefined
at Client.<anonymous> (C:\Users\Sans\Desktop\discord-button-main\index.js:8:30)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (C:\Users\Sans\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Sans\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
what's the probleme ? I don't really know ^^" (there is not all my code)
I would take a look at this guide to learn more about buttons and interactions.
This uses discord.js v13
To fix the immediate problem, it will require recoding, as follows:
const { Client, MessageActionRow, MessageButton } = require('discord.js');
const client = new Client();
client.on('messageCreate', async message => {
if (message.content === '!button') {
let buttons = new MessageActionRow()
.addComponents(
new MessageButton()
.setStyle('SUCCESS') //Choices are PRIMARY, a blurple button, SECONDARY, a grey button, SUCCESS, a green button, DANGER, a red button, LINK, a button that navigates to a URL.
.setLabel('Test')
.setCustomId('button')
message.channel.send({
content: 'Message Text',
components: [buttons]
})
} else if (message.content === '!urlbutton') {
let buttons2 = new MessageActionRow()
.addComponents(
new MessageButton()
.setStyle('LINK')
.setLabel('Test')
.setURL('https://discord.com')
message.channel.send({
content: 'Message Text',
components: [buttons2]
})
}
})
How the buttons are interacted with would be under a different listener:
client.on('interactionCreate', async interaction => {
if (interaction.isButton()) {
const buttonID = interaction.customId
if (buttonID === 'button') {
// do something
return interaction.reply({
content: 'Say something',
ephemeral: true //set false or remove if you want everyone to see the response
}
}
}

Message embed field values must be non-empty strings

https://pastebin.com/FWYFSdfD
const Discord = require('discord.js')
module.exports = {
name: 'warn',
description: 'warn a member',
async execute(message, args){
edited = message.content.slice(5);
const guildID = message.guild.Discord
const UserId = message.member.Discord
const Reason = args.splice(1).join('')
let User = message.mentions.users.first()
if(message.member.permissions.has('KICK_MEMBERS')){
try{
member = await message.guild.members.find(message.mentions.users.first())
}catch(err){
const warnEmbed = new Discord.MessageEmbed()
.setTitle("**Warn**")
.setColor("#FF0000")
.addField('text', 'value')
.addField("User:", User, true)
.addField("Reason:", Reason, true)
.setThumbnail("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRGagFW_bh2AfgI1BqAis81CXVg37j8_MKtwg&usqp=CAU")
.setTimestamp()
}
try{
User.send(warnEmbed)
message.channel.send(warnEmbed)
}
catch(err){
return(message.reply('You must format the command <#mention> <reason>'))
}
}
else{
message.channel.send('only staff can use this command')
}
}
}
This is the warn code. But when I try to use it, I get an error saying MessageEmbed field values must be non-empty strings. And how many words do I need to write to send this.

im trying to make a simple discord.js bot

i am trying to make it so as soon as you type: mememe it will react with: your nickname is now:
my current code is
const Discord = require("discord.js");
const client = new discord.client();
client.login(process.env.SECRET);
const embed = new Discord.MessageEmbed()
.setTitle("This is Embed Title")
.setDiscription("this is embed discription")
.setColor("RANDOM")
.SetFooter("This is Embed Footer");
const nicknames = ["dumbass", "idiot", "op", "man", "power", "docter"];
client.on("ready", () => {
client.user.setPresence({ activity: { name: "brave" }, status: "invisible" });
});
client.on("message", (message) => {
if (message.content === "ding") {
message.channel.send === "dong";
}
if (message.content === "embed") {
message.channel.send(embed);
}
});
if (message.content("mememe")) {
const index = Math.floor(Math.random() * nicknames.length);
message.channel.send(nicknames[index]);
}
but i dont know why it does not work it says as a error: Parsing error: Unexpected token
that is all and idk how to fix that
Edit : you guys were useless
I first want to say: Please fix your indents (I did it for you below here.
const Discord = require("discord.js")
const client = new Discord.Client()
client.login(process.env.SECRET)
// const embed = new Discord.MessageEmbed()
// .setTitle("This is Embed Title")
// .setDiscription("this is embed discription")
// .setColor("RANDOM")
// .SetFooter("This is Embed Footer");
const nicknames = ["dumbass", "idiot" , "op" , "man" , "power" , "docter"]
client.on("ready" , () => {
client.user.setPresence({ activity: { name: "brave"}, status: "invisible"})
})
client.on("message" , message => {
if(message.content === ("ding")) {
message.channel.send === ("dong")
}
if(message.content === ("embed")) {
message.channel.send(embed)
}
if(message.content === ("mememe")) {
const index = Math.floor(Math.random() * nicknames.length);
message.channel.send(nicknames[index])
}
})
The issue was you were calling the mememe command wrong. Above you used messega.content === "..."
In the mememe command you used message.content("mememe"). This does not work. Changes it (or copying the code above should fix the issue. Maybe an idea for you. You could add a feature where it changes the Users Nickname instead of sending a random one.

Resources