Discord.js Sharding, How do you send an embed message with broadcastEval? - discord.js

I'm trying to send an embed message to an specific channel with a sharded bot.
I've achieved sending a simple message successfully with this code:
client.shard.broadcastEval(`
(async () => {
let channel = await this.channels.get("683353482748756047");
channel.send("Hello")
})()
`)
The problem starts when I want to send an embed message. I've tried passing the variable like this:
//exampleEmbed is created
client.shard.broadcastEval(`
(async () => {
let channel = await this.channels.get("683353482748756047");
channel.send('${exampleEmbed}')
})()
`)
but the message is sent like "[object Object]".
I thought about returning the channel object back outside of broadcastEval and then sending my variable, but I've read this is not possible because you can't return full discord objects.
How should I send the embed message? Thank you for your time.

Okay, I solved it by creating the embed message inside the broadcastEval, and using the '${}' syntax to poblate it.
Example:
client.shard.broadcastEval(`
(async () => {
const Discord = require('discord.js');
let channel = await this.channels.get("683353482748756047");
if(channel){
//if shard has this server, then continue.
let message = new Discord.RichEmbed()
.setThumbnail(this.user.displayAvatarURL)
.setTitle('Title')
.addField("Something useful:", '${useful}')
.addField("Another useful thing:", '${useful2}')
.setTimestamp()
channel.send(message)
}
})()

Related

How to send an embed message to a different channel in interactions?

How to send a separate embed message to a channel different from where the slash command was executed? My current code is the following block.
const { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder, Guild } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('arrest')
.setDescription('puts the member to jail')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to arrest')
.setRequired(true))
.addStringOption(option =>
option
.setName('reason')
.setDescription('The reason for arrest'))
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
const messageEmbed = new EmbedBuilder()
.setTitle('ARRESTED!')
.setDescription(`***${interaction.options.getUser('target').tag}*** *broke a leg. Oops.*`)
.setColor(0xBD7A21)
.setImage('https://eca.astrookai.repl.co/media/arrest.gif')
await interaction.reply({embeds: [ messageEmbed ]});
},
};
Sending a separate embed object to a different channel is intended for channel logging for moderation actions in the server and I would like to know how to achieve this, thank you.
You can use <channel>.send() for this. In which <channel> is your channel object. You can get the channel in a multiple of different ways but this is probably the easiest:
logChannel = <interaction>.guild.channels.fetch(SNOWFLAKE_ID)
Then, you can send your embed to the logchannel like so:
logChannel.send({ embeds: [YOUR_EMBED] });
To account for every command, the easiest way is to do this in your interactionCreate event
If you need more context, you can check out this example which "logs" an embed to the logChannel.

how to make a discord bot delete what you wrote and send what you originally sent in javascript?

it should delete my message and type it as if it has said it itself, im a begginer in discord bot coding pls help
It can be done by storing the message in a variable, deleting the message, then sending the variable. An example would be:
client.on('message', async (message) => {
const content = message.content;
await message.delete();
await message.channel.send(content);
});

How do you make some bot commands only work in some discord channels

This is my first time using js. I need to ban a certain word in a certain channel. I don't want the message to be deleted if it is not in the specific channel.
For Example:
I want to ban the word "goodbye" in the #greeting channel
BUT
I don't want to ban the word "goodbye" In the #farewell channel
How would I go about this?
Thanks.
By the way I wanted to use example code, but none of it made any sense.
I wrote a simple example for you on how to achieve this:
const Discord = require("discord.js");
const Client = new Discord.Client();
const ForbiddenWords = {
"ChannelID": ["goodbaye", "bye"] // Greeting channel
};
Client.on("ready", () => {
console.log(`${Client.user.tag} is ready!`);
});
Client.on("message", (message) => { // Fired when the bot detects a new message.
if (message.author.bot) {return false}; // Checking if the message author is a bot.
if (ForbiddenWords[message.channel.id]) { // Checking if the current channel has any forbidden words in the ForbiddenWords Object
if (ForbiddenWords[message.channel.id].some(word => message.content.toString().toLowerCase().includes(word))) { // Checking if the message contains any forbidden words
message.delete(); // Deleting the message.
};
};
});
Client.login(process.env.DISCORD_AUTH_TOKEN);

Discord.js: Says there are no messages in a channel which does have messages

Here is my code
import Discord from 'discord.js'
const bot = new Discord.Client()
const channelID = 'the channel's id'
bot.login('my token')
bot.on('ready', async () => {
const channel = (await bot.channels.fetch(channelID)) as Discord.TextChannel //im using typescript
console.log(channel.messages.cache.array())
})
For some reason the array is empty, even though there are tons of messages in the channel. Any reason this might be? I know that it is getting the right channel, as I've tested that and it shows the right server and name. Thanks.
Use channel.messages.fetch().array() instead. Your array is most likely empty because the messages are not cached, optionally you can also cache the messages by doing channel.messages.fetch({}, true).array()

Cannot send message to just created channel

I'm trying to send a message to a channel just after I created it, but it doesn't seem to work. I'm using discord.js#v12
This is the code:
message.guild.channels.create(cpl, 'text').then(ma => {
ma.setParent(cat);
ma.lockPermissions();
}).catch(err => console.log(err))
let nChannel = bot.channels.cache.find(ch => ch.name === cpl)
console.log(nChannel)
let embed = new Discord.MessageEmbed()
.setTitle("New Ticket")
nChannel.send(embed)
This is what is logged to the console:
Cannot read property 'send' of undefined
this is because you're not waiting for the channel to be created before trying to send the message. In the same way you wait for it to be ready before using .setParent() and .lockPermissions(), you should wait before using .send().
Here's how I would do it:
message.guild.channels.create(cpl, 'text').then(async (newChannel) => {
// You're inside .then, so all of this is happening AFTER the channel is created
// Wait for .setParent and .lockPermissions to fulfill
await newChannel.setParent(cat)
await newChannel.lockPermissions()
// You can now send your message
let embed = new Discord.MessageEmbed()
.setTitle("New Ticket")
newChannel.send(embed)
})

Resources