The part 'const exampleEmbed = new Discord.MessageEmbed()' and 'command(client, 'embed', (message) => {' get wrong but i fail debug
command(client, 'embed', (message) => {
^
ReferenceError: command is not defined
at Client. (D:\DC Bot\discord.js:278:9)
at Client.emit (node:events:532:35)
at MessageCreateAction.handle (D:\DC Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\DC Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\DC Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (D:\DC Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:\DC Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (D:\DC Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:520:28)
at Receiver.receiverOnMessage (D:\DC Bot\node_modules\ws\lib\websocket.js:1047:20)
`client.on('message', msg => {
if (msg.content === '-rhelp') {
command(client, 'embed', (message) => {
const exampleEmbed = new Discord.MessageEmbed()
.setTitle('MCMB 指令')
.addFields(
{ name: '【機器人加入語音】', value: '-rjoin', inline: true },
{ name: '【播放音樂(加入隊列)】', value: '-rplay <音樂網址>', inline: true },
{ name: '【暫停播放】', value: '-rpause', inline: true },
{ name: '【恢復播放】', value: '-rresume', inline: true },
{ name: '\u200B', value: '\u200B' },
{ name: '【跳過這首歌曲】', value: '-rskip', inline: true },
{ name: '【查看歌曲隊列】', value: '-rqueue', inline: true },
{ name: '【讓機器人離開語音頻道(會清空歌曲隊列)】', value: '-rleave', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setTimestamp()
message.channel.send(embed)
})
}
});
client.login(token);`
You have not defined "command". It is not a default feature of Discord.js, you have to make it yourself.
Related
I'm getting this error everytime i clicked the button i made in v14, i followed how the v14 buttons made and work but i don't know why this error is showing and my buttons isn't working.
Err:
DiscordAPIError[50035]: Invalid Form Body
name[BASE_TYPE_REQUIRED]: This field is required
at SequentialHandler.runRequest (/home/runner/skyanime-utilities/node_modules/#discordjs/rest/dist/index.js:748:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.queueRequest (/home/runner/skyanime-utilities/node_modules/#discordjs/rest/dist/index.js:560:14)
at async REST.request (/home/runner/skyanime-utilities/node_modules/#discordjs/rest/dist/index.js:1000:22)
at async GuildChannelManager.create (/home/runner/skyanime-utilities/node_modules/discord.js/src/managers/GuildChannelManager.js:145:18)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:393:10) {
rawError: {
code: 50035,
errors: {
name: {
_errors: [
{
code: 'BASE_TYPE_REQUIRED',
message: 'This field is required'
}
]
}
},
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'POST',
url: 'https://discord.com/api/v10/guilds/1000655001888235540/channels',
requestBody: {
files: undefined,
json: {
name: undefined,
topic: undefined,
type: undefined,
nsfw: undefined,
bitrate: undefined,
user_limit: undefined,
parent_id: undefined,
position: undefined,
permission_overwrites: undefined,
rate_limit_per_user: undefined,
rtc_region: undefined,
video_quality_mode: undefined
}
}
}
here's the handler of the buttons im working
https://sourceb.in/FepPx2RpCK
here is my interaction handler https://sourceb.in/h5zEL0I3oF
and the command where buttons and id https://sourceb.in/RstCfscZpY
The issue happens during channel creation. The first argument (channel name) has been moved to the name property.
Change
await guild.channels.create(`${customId + "-" + ID}`, {
type: ChannelType.GuildText,
parent: parent_ID,
permissionOverwrites: [
{
id: member.id,
allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ReadMessageHistory],
},
{
id: everyone_ID,
deny: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ReadMessageHistory],
},
],
})
To
await guild.channels.create({
name: `${customId + "-" + ID}`,
type: ChannelType.GuildText,
parent: parent_ID,
permissionOverwrites: [
{
id: member.id,
allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ReadMessageHistory],
},
{
id: everyone_ID,
deny: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ReadMessageHistory],
},
],
})
Make sure to send your command.data as JSON.
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
//convert the data to JSON as below
commands.push(command.data.toJSON());
}
{
body: commands
}
I've tried writing a discord bot, nearly finished it but this error just appeared and I don't know what's wrong with the code.
The Error:
DiscordAPIError: Cannot send an empty message
at RequestHandler.execute (C:\Users\user\OneDrive\Desktop\discordbot\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\user\OneDrive\Desktop\discordbot\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async TextChannel.send (C:\Users\user\OneDrive\Desktop\discordbot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:176:15) {
method: 'post',
path: '/channels/991019562625552466/messages',
code: 50006,
httpStatus: 400,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
The code:
module.exports = {
name: 'command',
description: "Embeds!",
execute(message, args, Discord) {
const newEmbed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('This is a title.')
.setURL('https://www.youtube.com')
.setDescription('This is an embed.')
.addFields(
{name: 'Field 1', value: 'Text 1'},
{name: 'Field 2', value: 'Text 2'},
{name: 'Field 3', value: 'Text 3'}
)
.setImage('https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg')
message.channel.send(newEmbed);
}
}
What's the problem?
As of discord.js v13 embeds are sent via the MessageOptions object passed to the send method.
Updated code would be:
message.channel.send({embeds: [newEmbed]});
Documentation:
BaseGuildTextChannel#send takes
<MessageOptions> extends <BaseMessageOptions>
I need to fix my discord comand "Whois". Can someone help me fix my code? Message me on twt: KiseeIsHere (not really necessary I just need to expand my message because stackflow wants me to).
module.exports.run = async (client, message, args) => {
const { MessageEmbed } = require('discord.js');
let member = message.mentions.users.first() || message.guild.members.cache.get(args[0]) || message.author || message.member;
let avatar = member.displayAvatarURL({ size: 1024, dynamic: true });
const statuses = {
online: "Online",
dnd: "Dnd",
idle: "Idle",
offline: "Offline"
};
let itstatus = statuses;
const exampleEmbed = new MessageEmbed()
.setTitle(member.username + "'s Profile")
.setColor('#2f3136')
.setThumbnail(avatar)
.addField("User Tag", member.tag, true)
.addField("ID", `${member.id}`, true)
.addField("Status", itstatus[member.presence.status], true)
.addField(
`Roles Count`,
message.guild.members.cache.get(member.user.id).roles.cache.size ||
"No Roles!",
true
)
.addField(`Avatar Url`, `[Link](${avatar})`, true)
.setFooter(`Requested by ${message.author.username}`)
.setTimestamp();
message.channel.send({ embeds: [exampleEmbed] });
}
module.exports.config = {
name: "whois",
aliases: ['ui']
}
Made a couple of changes, mostly to grab user stuff in the event someone runs it to get info on a bot and cleaned up the code a bit. I have tested and works as expected
const {
MessageEmbed,
} = require('discord.js');
module.exports.run = async (client, message, args) => {
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.member;
const statuses = {
online: "Online",
dnd: "Dnd",
idle: "Idle",
offline: "Offline",
};
let status;
if (!member.presence) {
status = 'Unknown;'
} else {
status = statuses[member.presence.status]
}
const exampleEmbed = new MessageEmbed()
.setTitle(`${member.user.username}'s Profile`)
.setColor('#2f3136')
.setThumbnail(member.user.avatarURL({
size: 1024,
dynamic: true,
}))
.addFields({
name: "User Tag",
value: `${member.user.tag}`,
inline: true,
}, {
name: "ID",
value: `${member.id}`,
inline: true,
}, {
name: "Status",
value: `${status}`,
inline: true,
}, {
name: `Roles Count`,
value: `${message.guild.members.cache.get(member.user.id).roles.cache.size}` || "No Roles!",
inline: true,
}, {
name: `Avatar Url`,
value: `[Link](${member.user.avatarURL()})`,
inline: true,
})
.setFooter({
text: `Requested by ${message.author.username}`,
})
.setTimestamp();
message.channel.send({
embeds: [exampleEmbed],
});
};
module.exports.config = {
name: "whois",
aliases: ['ui'],
};
I am trying to create an embed and I pasted the code straight from the discord.js guide right into the code but it's not working and I can't figure out why it's wrong. The code and error are below
module.exports = {
name: 'infotest',
description: 'infotest',
execute(message, args) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
channel.send(exampleEmbed);
},
};
ReferenceError: Discord is not defined
at Object.execute (/home/runner/BotNameHere/Commands/info2.js:5:24)
at Client.<anonymous> (/home/runner/BotNameHere/index.js:33:32)
at Client.emit (events.js:314:20)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/BotNameHere/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/BotNameHere/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/BotNameHere/node_modules/ws/lib/event-target.js:132:16)
Read your error, you have to define the variable Discord. Just use require() to use the discord.js module.
I want to validate a discord embed before sending it.
Which means, I want to know if an embed has nothing wrong with it before actually sending it on a channel.
var embed = {
title: 'some title',
description: 'some description',
color: 'foo bar'
}
// embed.color is not a valid color, so it will trigger an error when I send it.
// Here I want some way to know that there's something wrong with the embed object.
message.channel.send({embed});
I have made a function to validate embeds. However, it's incomplete and may have false positives or false negatives, but it should meet your needs.
function validURL(url) {
try {
new URL(url);
} catch (err) {
return false;
}
return true;
}
function validEmbed(embed) {
return Object.entries({
title: (title) => typeof title === 'string',
description: (description) => typeof description === 'string',
url: (url) => validURL(url),
timestamp: (timestamp) => (new Date(timestamp)).getTime() > 0,
color: (color) => typeof color === 'number',
fields: (fields) => fields.every(field => typeof field.name === 'string' && typeof field.value === 'string' && typeof field.inline === 'boolean'),
thumbnail: (thumbnail) => validURL(thumbnail.url),
image: (image) => validURL(image.url),
author: (author) => typeof author.name === 'string' && validURL(author.url) && validURL(author.icon_url),
footer: (footer) => typeof footer.text === 'string' && validURL(footer.icon_url)
}).every(field => {
if (!(field[0] in embed)) return true;
return embed[field[0]] && field[1](embed[field[0]]);
});
}
Here is an exaple snippet:
function validURL(e){try{new URL(e)}catch(e){return!1}return!0}function validEmbed(e){return Object.entries({title:e=>"string"==typeof e,description:e=>"string"==typeof e,url:e=>validURL(e),timestamp:e=>new Date(e).getTime()>0,color:e=>"number"==typeof e,fields:e=>e.every(e=>"string"==typeof e.name&&"string"==typeof e.value&&"boolean"==typeof e.inline),thumbnail:e=>validURL(e.url),image:e=>validURL(e.url),author:e=>"string"==typeof e.name&&validURL(e.url)&&validURL(e.icon_url),footer:e=>"string"==typeof e.text&&validURL(e.icon_url)}).every(t=>!(t[0]in e)||e[t[0]]&&t[1](e[t[0]]))}
/* returns true */
console.log(validEmbed({
title: 'The title'
}));
/* returns true */
console.log(validEmbed({
title: 'The title',
description: 'Sample Description.',
url: 'https://i.imgur.com/ttHwmCl.jpg',
timestamp: '2020-07-30T14:44:05.876Z',
color: 3447003,
fields: [{
name: 'name',
value: 'value',
inline: false
},
{
name: 'name2',
value: 'value2',
inline: true
}
],
thumbnail: {
url: 'attachment://stackoverflow2.jpg'
},
image: {
url: 'attachment://stackoverflow.jpg'
},
author: {
name: 'Daemon Beast',
url: 'https://i.imgur.com/P4L260P.jpg',
icon_url: 'https://i.imgur.com/P4L260P.jpg'
},
footer: {
text: 'footer text',
icon_url: 'https://i.imgur.com/ttHwmCl.jpg'
}
}));
/* returns false */
console.log(validEmbed({
author: 4567876567
}));