so im making a discord bot and i want it to send an embed when i type hy!help
so i used the format on discord.js documantion and so far my code looks like this
const client = new Discord.Client({
token: "pls-no-hack-ty"
});
client.on("ready", () => {
//do stuff lol
});
client.on("message", message => {
if(message.content === "hy!help") {
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);
} } )
But i get an channel dot defined error.
What am i doing wrong?
Thanks -rly907
In your last line instead of channel.send(exampleEmbed); do
message.channel.send(exampleEmbed);
You should to user message.channel.send it can't use channel.send
message.channel.send(Embed);
Related
if (interaction.commandName === 'embed') {
const embed = new EmbedBuilder()
.setTitle('Embed title')
.setDescription('This is an embed description')
.setColor('Random')
.addFields(
{
name: 'Field title',
value: 'Some random value',
inline: true,
},
{
name: '2nd Field title',
value: 'Some random value',
inline: true,
}
);
interaction.reply({ embeds: [embed] });
}
That is my new Discord bot, with slash commands. That's the only issue for now. Anyone know why it says "SyntaxError: unexpected token 'if'"?
I wanted it to send an embed based on a slash command.
My code is simple, it should send a dm embed at the person choose by the user using the command (it is a test for a ban command but it is easier to no ban my alt to after dean him and rejoin with him).
There is my code :
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('test')
.setDescription('Send a private message to a selected user')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to send the message to')
.setRequired(true))
.addStringOption(option =>
option
.setName('reason')
.setDescription('The reason for sending the message'))
.setDMPermission(false),
async execute(interaction) {
const target = interaction.options.getUser('target');
if (!target) return interaction.reply({ content: "You must provide a target user with the 'target' option.", ephemeral: true });
const reason = interaction.options.getString('reason') || 'No reason provided';
const owner = await interaction.guild.fetchOwner();
if (!owner) return interaction.reply({ content: "I couldn't fetch the guild owner.", ephemeral: true });
if (target.user === interaction.client.user) return interaction.reply({ content: "I can't send a message to myself.", ephemeral: true });
const banEmbed = new EmbedBuilder()
.setColor(0x0099FF)
.setTitle('Hello! 😃')
.setAuthor({ name: `request by ${interaction.user.username}`, iconURL: interaction.guild.iconURL({ format: 'png', dynamic: true }) })
.setDescription(`You have been banned from ${interaction.guild}.`)
.addFields(
{ name: '\u200B', value: `If you think there's an error, please contact ${owner.user.username}#${owner.user.discriminator}.` },
{ name: '\u200B', value: `Reason: ${reason}` },
)
.setTimestamp()
.setFooter({ text: 'Have a good day !' });
try {
await target.send({ content: "hello", embed: banEmbed });
interaction.reply({ content: "The message has been sent successfully.", ephemeral: true });
} catch (error) {
console.error(error);
interaction.reply({ content: "I couldn't send the message to the target user.", ephemeral: true });
}
},
};
The code is working only if I add content not embedded, in that case it won't send the embed and if I don't put the content, it will just send the error :
DiscordAPIError[50006]: Cannot send an empty message
at SequentialHandler.runRequest (/Users/theophile/Desktop/test bot copie/node_modules/#discordjs/rest/dist/index.js:667:15)
at processTicksAndRejections (/Users/theophile/Desktop/test bot copie/lib/internal/process/task_queues.js:96:5)
at async SequentialHandler.queueRequest (/Users/theophile/Desktop/test bot copie/node_modules/#discordjs/rest/dist/index.js:464:14)
at async REST.request (/Users/theophile/Desktop/test bot copie/node_modules/#discordjs/rest/dist/index.js:910:22)
at async DMChannel.send (/Users/theophile/Desktop/test bot copie/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:176:15)
at async Object.execute (/Users/theophile/Desktop/test bot copie/commands/test.js:41:7)
at async Client.<anonymous> (/Users/theophile/Desktop/test bot copie/index.js:71:3) {requestBody: {…}, rawError: {…}, code: 50006, status: 400, method: 'POST', …}
The code below is my sendgrid helper function
presently, this is what comes up as the sender name "info" which is conned from infogmail.com. . i would like to add a custom name. i have read the docs but i have not seen a solution to my problem. all the solutions i saw was for python
const msg = {
to: email,
from: `${SENDER_EMAIL}`,
fromname: FROM_NAME,
subject: "Thanks for the Gift",
html: `${html(name, SENDER_EMAIL, SENDER_NAME)}`,
};
sgMail
.send(msg)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
};```
You can in this way:
sendgrid.send({
to: 'you#yourdomain.com',
from: {
email: 'example#example.com',
name: 'Sender Name'
},
subject: 'Hello World',
text: 'My first email through SendGrid'
});
So i follow worn off keys tutorial to discord bot and i don't know what the problem is, here is the error
/home/container/node_modules/discord.js/src/rest/RequestHandler.js:349
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Access
at RequestHandler.execute (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (/home/container/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
at async GuildApplicationCommandManager.create (/home/container/node_modules/discord.js/src/managers/ApplicationCommandManager.js:117:18) {
method: 'post',
path: '/applications/901999677011005472/guilds/905266476573950023/commands',
code: 50001,
httpStatus: 403,
requestData: {
json: {
name: 'ping',
description: 'Bot uptime/latency checker.',
type: undefined,
options: undefined,
default_permission: undefined
},
files: []
}
}
I also try looking at my code but I didn't see something wrong.
This is my code, I really think something is wrong in the code.
const DiscordJS = require('discord.js')
const { Intents } = require('discord.js')
const dotenv = require('dotenv')
dotenv.config()
const client = new DiscordJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
})
client.on('ready', () => {
console.log("The bot is online")
// Carlos: 883425101389914152
const guildId = '905266476573950023'
const guild = client.guilds.cache.get(guildId)
let commands
if (guild) {
commands = guild.commands
} else {
commands = client.application.commands
}
commands.create({
name: 'ping',
description: 'Bot uptime/latency checker.',
})
commands.create({
name: 'add',
description: 'Adds two numbers given by user.',
options: [
{
name: 'number1',
description: 'The first number',
required: true,
type: DiscordJS.Constants.ApplicationCommandOptionTypes.NUMBER,
},
{
name: 'number2',
description: 'The second number',
required: true,
type: DiscordJS.Constants.ApplicationCommandOptionTypes.NUMBER,
},
]
})
})
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) {
return
}
const { commandName, Options } = interaction
if (commandName === 'ping') {
interaction.reply({
content: 'Pong! **60ms**',
// If anyone can see = True, Only command user can see = False
ephemeral: true,
})
} else if (commandName === 'add') {
interaction.reply({
content: 'The sum is ${number1 + number2}'
})
}
})
client.login(process.env.KEY)
For anyone that having the same problem. I fix this by simply going to bot developer portal then go to OAuth2 > URL generator. And for the scope select both "bot" and "applications.commands". Then scroll down choose whatever permission your bot need copy the URL.
You did not select the right permissions when creating bot login link for your discord server.
Goto developers / oauth again, click bot and select all the required permissions that you're using in this bot.
Then copy the link generated and use it to login with your bot into your server.
don't forget to change this:
content: 'The sum is ${number1 + number2}'
to this:
content: `The sum is ${number1 + number2}`
It is necessary to have the message sent to Embed (so it could be the specified parameters (color, author, title, description, addField, content message)
Example: https://embedbuilder.nadekobot.me/
const arg = message.content.slice().trim().split(/ +/g);
let name = arg[1];
if (!name) return message.channel.send(channelEmbed);
let anonce = args.slice(1).join();
if(!anonce) return message.channel.send(anonceEmbed);
let anoncechannel = message.guild.channels.find(`name`, name);
anoncechannel.sendEmbed(anonce)
Take a look at RichEmbeds.
You can build a new RichEmbed and then send it to the channel you want. You can set every value, title, description, color, footer..
Example:
const exampleEmbed = new Discord.RichEmbed()
.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')
.addField('Regular field title', 'Some value here')
.addField('Inline field title', 'Some value here', true)
.addField('Inline field title', 'Some value here', 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');
message.channel.send({ embed: exampleEmbed });
source: discordjs.guide
This would be the output: