DiscordJS v14 guildCreate send message error - discord

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)

Related

TypeError: Cannot read properties of undefined (reading 'FLAGS') discord.js

so this is my bot code
const Discord = require("discord.js")
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES] });
client.on("ready", () => {
console.log('Logged in as ${client.user.tag}!')
console.log("Bot is online!")
})
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("no1se");
}
})
client.login("my token")
The error i get is this:
TypeError: Cannot read properties of undefined (reading 'FLAGS')
at Object.<anonymous> (/home/runner/no1seAlerts-1/index.js:4:47)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
Hint: hit control+c anytime to enter REPL.
(node:2004) ExperimentalWarning: stream/web is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
yea so im trying to make a discor dbot everytime when i try to run it and make it online i get this error code i have no idea what to do and i would really appreciate any help thank you!
Assuming you are using discord.js v13, you can try and change your client to:
const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })
You should replace Intents with IntentsBitField
In your example it would be:
const client = new Client({ intents: [IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages] });
This was changed with version 14 of discord.js I believe.
Alternatively you can use GatewayIntentBits:
const client = new Client({ intents: [GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages] });
More information here
Hope this helps!

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 + ">")
}
}
});

Why isn't embeds index not working when sending a message

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)

Making discord.js send a message to channel on command

I am trying to make my bot send a message to a channel in all servers that it is in when updated. My idea is that I will trigger this manually whenever I update my bot. For example, I would like to have it send a message such as "Updated bot to version 2.03. Update log: (changes made in the update)". This is the code I currently have:
const Discord = require('discord.js');
const client = new Discord.Client();
var Long = require("long");
const getChannel = (guild) => {
// get "original" default channel
if(guild.channels.cache.has(guild.id))
return guild.channels.cache.get(guild.id)
const jimmyChannel = guild.channels.cache.find(channel => channel.name === "jimmybot");
if (jimmyChannel)
return jimmyChannel;
return guild.channels.cache
.filter(c => c.type === "text" &&
c.permissionsFor(guild.client.user).has("SEND_MESSAGES"))
.sort((a, b) => a.position - b.position ||
Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber())
.first();
}
// I found this as an example but I'm not sure if it will work
client.on("guildMemberAdd", member => {
const channel = getChannel(member.guild);
channel.send(`text`);
});
client.login('token');
My question is how do I call this manually instead of it sending the message whenever a user joins the server?
If I'm understanding this properly...
Client is actually just a variation of a Node.js EventEmitter, as stated here.
This means that you can trigger the events of your own accord, and so here you could do your own 'custom' EventEmitter and trigger it of your own accord.
Ex code paired with message event emitter. I know that it's generally frowned upon to put an event emitter inside of another, but it was the only solution I could find.
const Discord = require('discord.js');
const client = new Discord.Client();
token = 'XXX'
client.once('ready', () => {
console.log('Fully functional and initialized!');
});
// Attach a listener function
client.on('test', console.log);
client.on('message', msg => {
if (msg.author.bot) return;
if (msg.content === 'emit event') {
// Emit the event
client.emit('test', 'This will be printed on the console');
}
})
client.login(token);

I want to get System Messages Channel in discord.js v12

I want to get SystemChannel. I am using discord.js v12, this is what I have so far:
bot.on('guildMemberAdd', member => {
// I want to get this channel, not by name
[System Messages Channel][1]
const channel = (getDefaultChannel)
if(!channel) return;
channel.send(`hello ${member}!`);
});
You can get the SystemChannel by using member.guild.systemChannel, or member.guild.systemChannelID in case you want the channel's ID, in your guildMemberAdd event.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
// Get the "System Messages Channel"
const systemMessagesChannel = client.guilds.cache.first().systemChannel;
// Send a message to the channel
systemMessagesChannel.send('Hello from the bot!');
});
client.login('YOUR_BOT_TOKEN_HERE');

Resources