Why isn't embeds index not working when sending a message - discord.js

I've attempted for a while now to try and get msg.channel.send({ embeds: [embed] }) to work however I keep getting DiscordAPIError: Cannot send an empty message
I should note that all code is in a module however all the proper resources are required so I'm at a total loss here, we are also running Discord.JS v12.2.0.
Here is what we have:
const Discord = require("discord.js")
exports.run = async (client, msg, args) => {
let embed = new Discord.MessageEmbed()
.setTitle("Test")
.setColor("#00F0FF")
.setTimestamp();
client.log(JSON.stringify(embed.toJSON()), 1)
msg.channel.send({ embeds: [embed] }).then(() => {
}).catch(err => {
client.log(err, 2)
});
}
Just another note, even if I do provide the content index it posts the message just without the embed.
I can also do msg.channel.send(embed); and it posts the embed, however I am needing to post multiple embeds without spamming discords api.

You are using v12, but { embeds: [] } is only in v13. You should either update to v13 or use the old way
msg.channel.send(embed)

Related

DiscordJS v14 guildCreate send message error

I'm making an event on discordjs v14 that when the bot added it send a message to a channel where the bot can send message/open channel but I'm getting this error after it got added...
Cannot read properties of undefined (reading 'has')
My code:
const embed = new EmbedBuilder()
.setDescription('Hi')
.setColor('Blue')
let channel = guild.channels.cache.find(
channel =>
channel.type === ChannelType.GuildText &&
channel.permissionsFor(guild.me.has(PermissionsBitField.Flag.SendMessages))
);
channel.send({ embeds: [embed] })
I've tried:
const embed = new EmbedBuilder()
.setDescription('Hi')
.setColor('Blue')
let channel = guild.channels.cache.find(
channel =>
channel.type === ChannelType.GuildText &&
channel.permissionsFor(guild.me).has(PermissionsBitField.Flag.SendMessages)
);
channel.send({ embeds: [embed] })
To make the bot send a message to a channel/opn channel whenever it got added to the server
In v14, the Guild#me property has been moved to the GuildMemberManager.
channel.permissionsFor(guild.members.me)

How can a bot read embed messages sent by another bot and post a message if it contains a specific content?

I'm trying to code a Discord bot that would read embed messages sent by another bot and ping a specific role if the embed contains '(FREE)' in the title, as displayed here:
https://i.stack.imgur.com/kPsR1.png
Unfortunately, my code produces nothing. I don't run into any error, the bot simply doesn't post a message when conditions are matched to do so — yet it is online and has the permissions to post messages in the channel.
Any help would be really appreciated. I went through all the questions related to this topic on SO, but I couldn't find a working solution to my issue. I'd like to mention that I'm a beginner in JS.
Thank you so much.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', (message) => {
if (message.embeds) {
const embed = message.embeds[0]
if (embed.title === '(FREE)') {
return message.channel.send('#Free mint')
}
}
})
Edit: I believe my question is different from the one suggested in the comment as I'm looking for specific content in the embed.
I managed to make the bot work with the following code, thanks #Gavin for suggesting it:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', message => {
for (let embed of message.embeds) {
if (embed.title.includes('FREE')) {
return message.channel.send("<#&" + roleId + ">")
}
}
});

Cannot get channels moving event in Discord audit log

I'm trying to log the Discord event when someone is moving any channel.
The channelUpdate event works almost like a charm and returning 2 events (two, because of when you moving one channel the rawPosition property is changing in the target's channel and in the channel you swiped the place). Except strange cases when you moved one channel and getting tone of events as if you moved all the server channels at ones. Weird thing, yep.
The problem is the "channelUpdate" event returns only oldChannel and newChannel but not the executor data (who and when).
I've tried to use guild.fetchAuditLogs({ type: 14, limit: 1 }) but looks like it's not returning changing position events. One gets the feeling Discord's log engine is not logging this event OR I'm looking in wrong place. Also tried type 11 and even NULL with no luck on finding rawPosition prop changing.
Please, help me to find the way how to log WHO is changing the channels position or tell me it's not possible at the time.
Some code here:
const Discord = require('discord.js');
const client = new Discord.Client({ shards: 'auto'});
const { prefix, token, witaitoken , guildID, DBUser, DBPass } = require('./config.json');
client.on('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity(`Star Citizen`, { type: 'PLAYING' });
});
client.on('channelUpdate', (oldChannel, newChannel) => {
if (oldChannel.rawPosition !== newChannel.rawPosition) {
oldChannel.guild.fetchAuditLogs({ limit: 10 })
.then( (audit) => {
const log = audit.entries;
if (!log) return;
fs = require('fs');
fs.appendFile('audit.txt', JSON.stringify(log), function (err) {
if (err) return console.log(err);
});
})
}
});
client.login(token);
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));
client.on("debug", (e) => console.info(e));

Why will messageReactionAdd event not work

this.on("messageReactionAdd", async (reaction, user) => {
if(reaction.emoji.name === "✔️") {
console.log("User reacted")
}
})
This is my code and at the moment it will not even run the event
this is defined as client.
My tips: Try if the ready or the message event works. If it doesn't, try removing the if statement in your code. If it still doesn't work try using the starter code from the discord.js guide which is something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('messageReactionAdd', (reaction, user) => {
console.log('event works!');
});
client.login('your-token-here');
If it still doesn't work your bot is most likely not logging in to discord. In this case I'd suggest you to recheck if your bot's token is the right one.

Is there a way to react to every message ever sent with discord.js

I wanna use an bot to react to every single message in an channel using discord.js f.e. i got an emoji contest channel and i wanna ad an ✅ and an ✖ reaction on every post in there
ofc, all the unnecesary messages are cleaned up so that there are like 50 messages
Fetch the messages already sent in a channel with TextChannel.fetchMessages().
Iterate through the Collection.
Add reactions with Message.react().
When a new message is sent in the channel, you should also add the reactions.
const emojiChannelID = 'ChannelIDHere';
client.on('ready', async () => {
try {
const channel = client.channels.get(emojiChannelID);
if (!channel) return console.error('Invalid ID or missing channel.');
const messages = await channel.fetchMessages({ limit: 100 });
for (const [id, message] of messages) {
await message.react('✅');
await message.react('✖');
}
} catch(err) {
console.error(err);
}
});
client.on('message', async message => {
if (message.channel.id === emojiChannelID) {
try {
await message.react('✅');
await message.react('✖');
} catch(err) {
console.error(err);
}
}
});
In this code, you'll notice I'm using a for...of loop rather than Map.forEach(). The reasoning behind this is that the latter will simply call the methods and move on. This would cause any rejected promises not to be caught. I've also used async/await style rather than then() chains which could easily get messy.
According to https://discord.js.org/#/docs/main/stable/class/TextChannel
you can use fetchMessages
to get all messages from a specific channel, which then returns a collection of Message
Then you can use .react function to apply your reactions to this collection of message by iterating over it and calling .react on each.
Edit:
channelToFetch.fetchMessages()
.then(messages => {
messages.tap(message => {
message.react(`CHARACTER CODE OR EMOJI CODE`).then(() => {
// Do what ever or use async/await syntax if you don't care
about Promise handling
})
})
})

Resources