I am looking to send a message to my logging channel when a user types something important (for a game server) what I have now doesn't work and just spams messages for some reason. if you know how to fix this and make it work and not spam messages, that would be good. (no errors in console)
client.on('message', (message) => {
if (message.content.startsWith("####")) return; {}
const logChannel = client.channels.cache.get('783009285709496371');
logChannel.send(`User: <#${message.author.id}> | Message: ${message.content}`);
})
Invert the return condition. Right now you are sensing all messages that dont start with #####.
Put a ! in front of the content check.
Related
Well i need to do this:
on a channel a bot announces "btc" price (not real)
im trying to get the price and send the price to a specificed channel
My code
sa.on("message", (message) => {
if (message.content.startsWith("🟠")) {
if (message.author.id == "974187708933083157") {
client.channels.get('955536998989447249').send(`${message.content}`);
} else message.channel.send(``);
}
})
So if a message that starts with 🟠the bot needs to get the price and send it to the channel (955536998989447249)
But it not working my bot is working fine but not getting price and sendimg it
Firstly don't try sending empty message message.channel.send(``);, it will just throw an error.
The main problem, is that client.channels (ChannelManager) doesn't have a "get" method.
Assuming you are using djs v13, to get a channel, you can get it from cache with:
client.channels.cache.get('955536998989447249').send("sth")
however this might not always work if the channel is not currently cached.
Other option is to fetch it, though it is an asynchronous operation, so it looks a bit different:
client.channels.fetch('955536998989447249').then(channel => channel.send("sth"))
I'm currently developing a bot that pins messages in a text file to bypass the message pin limit in discord servers, and my system works by having a user reply to a message with -pin and it will save that message and the message author in a text file. The problem is that I don't know how to get the content of the replied to message and the author of said message. Any help would be appreciated.
if(command === 'pin'){
message.channel.send('Ok, pinning that. Use -seepins to see all the pins.'),
//i have no clue how to get the message that was replied to please help let pinned =
//im begging you please i have no idea let pinnedauthor = message.
fs.writeFile('messages.txt', pinned + ' written by ' + pinnedauthor + '\n', (err) => {
if (err) throw err;
});
}
this is what I currently have. I just need to find out how to get the two things mentioned in the first paragraph and it should work.
Any help is appreciated, and thank you for reading this through.
When you receive the message object, you have a variable named reference. If your message is a reply reference should not be null and contains the ids of channelID, guildID, messageID.
With this you can then get the message content of the previous message with a line that looks like
client.on('message', async message => {
const repliedTo = await message.channel.messages.fetch(message.reference.messageID);
console.log(repliedTo.content);
});
Just be careful with the message.reference.messageID you have to add error handling to avoid crash when looking for an undefined variable.
I have a message edit log but I want to stop sending the log if a mobs message was updated, I tried a few codes like
if(bot.oldMessage.content.edit()){
return;
}
It showed and error
cannot read property 'edit' of undefined
I then removed edit then content was undefined. The code for the message update is below.
The Code
module.exports = async (bot, oldMessage, newMessage) => {
let channels = JSON.parse(
fs.readFileSync('././database/messageChannel.json', 'utf8')
);
let channelId = channels[oldMessage.guild.id].channel;
let msgChannel = bot.channels.cache.get(channelId);
if (!msgChannel) {
return console.log(`No message channel found with ID ${channelId}`);
}
if (oldMessage.content === newMessage.content){
return;
}
let mEmbed = new MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL({dynamic: true}))
.setColor(cyan)
.setDescription(`**Message Editied in <#${oldMessage.channel.id}>**`)
.addField(`Before`, `${oldMessage.content}`)
.addField(`After`, `${newMessage.content}`)
.setFooter(`UserID: ${oldMessage.author.id}`)
.setTimestamp()
msgChannel.send(mEmbed)
}
How would I stop it from sending the embed if a bots message was updated.
Making a really simple check will resolve this issue. In Discord.js there is a user field that tells you if the user is a bot or not.
In fact, it is really recommended you add this in the "onMessage" part of your code as it stops other bots from using your bot, this is to make sure things are safe and no loopbacks/feedbacks happen, either way, you don't want a malicious bot taking advantage of your bot, which can get your bot in trouble too.
Here is what you want to do;
if (message.author.bot) return;
What this code specifically does is check if the message's author is a bot, if it returns true, it will break the code from running, if it returns a false, the code continues running.
You can do the same if you want to listen to bots ONLY by simply adding a exclamation mark before the message.author.bot like this;
if (!message.author.bot) return;
It is also possible to see what other kinds of information something holds, you can print anything to your console. For example, if you want to view what a message object contains, you can print it into your console with;
console.log(message) // This will show everything within that object.
console.log(message.author) // This will show everything within the author object (like ID's, name, discriminators, avatars, etc.)
Go ahead and explore what you can do!
Happy developing! ^ -^
That is really easy to do. All you need to do is check if the author of the message ist a bot and then return if true. You do that like this
if (oldMessage.author.bot) return;
I made a code in which he sends a message to people with a specific role, however, for some reason it is sending only to me, code below
canal.send(`||#everyone||`, embed).then(message => {
message.react('748309940795473980')
})
servidor.roles.cache.get("771492474594000928").members.forEach((membro) => {
console.log(membro.user.username)
membro.send(`${membro}`, embed)
})
I would like you to help me with this, this command does the following functions:
Send Message to One Channel (Working)
Send Message to People with a Specific Position (Not Working, Just Send to Me)
As You Can See, I tried to put console.log, however, he was supposed to appear the name of everyone who received the message, but no, only my name appears, and only I get the message
I Need to Get This Working!
I’m trying to make my bot send a message to a channel whenever a user deletes his/her message, sorta like the bot Dyno, but I do not know how to do this. I think the method is .deleted() but I can’t seem to make it work. Can anyone tell me how? Sorry for lack of detail, there’s nothing else to add. Thank you in advance.
The Client (or Bot) has an event called messageDelete which is fired everytime a message is deleted. The given parameter with this event is the message that has been deleted. Take a look at the sample code below for an example.
// Create an event listener for deleted messages
client.on('messageDelete', message => {
// Fetch the designated channel on a server
const channel = message.guild.channels.cache.find(ch => ch.name === 'deleted-messages-log');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send a message in the log channel
channel.send(`A message has been deleted. The message was: ${message.content}`);
});