discordjs problems logging whenever a dm is sent - discord.js

I have a whole logging system that logs whenever various things are done in the server or with the bot, one being a log of whenever a dm is sent to the bot. the log would contain the author ID and the message content. the problem is with logging the message content. the bot crashes whenever a second DM is sent but works fine if its a first DM. the bot crash message is the following: https://gyazo.com/5ad0b41648f83a855ac8c49fb220a612
but the strange thing is, my fields aren't empty:
bot.on('message', msg=>{
const LogChannel = bot.channels.cache.get('712811824826941460');
const LogEmbed = new Discord.MessageEmbed()
.setColor('#606060')
.setAuthor(msg.author.tag)
.setDescription('DM Sent')
.addField('Message', msg.content)
.setTimestamp()
if(msg.channel.type === 'dm')
LogChannel.send(LogEmbed)
});
the console thinks that the .addField('Message', msg.content) is empty but as you can see, it's not. keep in mind it only gives me the error message after a second DM is sent following a first one.
any ideas?

There is no Message.content properties. I guess you are looking for Message.cleanContent
bot.on('message', msg=>{
const LogChannel = bot.channels.cache.get('712811824826941460');
const LogEmbed = new Discord.MessageEmbed()
.setColor('#606060')
.setAuthor(msg.author.tag)
.setDescription('DM Sent')
.addField('Message', msg.cleanContent)
.setTimestamp()
if(msg.channel.type === 'dm')
LogChannel.send(LogEmbed)
});

Related

Discord.js bot command to take users with a specific role and to return an embedded message with all the users in that role

I've been working on this and I cannot figure out why the bot will not enact the command. Essentially this is how the situation should work:
In the channel you send a message: "!listroletest"
After that, the bot should send an embedded with every user who has the role "test" with their userID's
client.on("message", message => {
if(message.content == "!listroletest") {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Users with the test role:')
.setDescription(message.guild.roles.get('901592912171765850').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
});

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

how to make it so that if a message is dm'ed a bot the message is shown in a channel in server discord.js

I need help making a discord bot so that if a message is dm'ed a bot the message is shown in a channel in server discord.js
A very basic way to do this is to just see if the message channel type is a DM (if (message.channel.type === 'dm')), and if it is, we will send the message content to a certain channel (client.channels.cache.get('channel_id').send(message.content)).
Put together, it is like this:
if (message.channel.type === 'dm') {
client.channels.cache.get('channel_id').send(message.content)
}
That will only send the message content.If you want to see who sent the message, you can do something like this:
client.channels.cache.get('channel_id').send(`New message by ${message.author.tag}: ${message.content}`)
This well send you something like New message by Username#0000: Hello world!.(This is a good place to stop if you want a simple if statement that gets who sent it and the message content.)
We can then go on to create this as an embed:
var dmMessage = new Discord.MessageEmbed() // This creates an empty embed
.setTitle("New DM!") // This sets the title of the embed to 'New DM!'
.setDescription(`**New message from ${message.author.tag}**(${message.author.id})\n**Message content**: ${message.content}`) // This is the main text of the embed
.setColor("GREEN") // This sets the color of the embed. It can be RED, PURPLE, etc. or it can be a hex code
.setTimestamp() // This shows the time when the embed was sent. ( it would also be the time the message was sent )
.setFooter(client.user.tag, client.user.displayAvatarURL()) // This sets the bots tag and profile picture to the footer
.setAuthor(message.author.tag, message.author.displayAvatarURL({dynamic: true})) // This sets the 'author' of the embed. It will show up at the top of the embed. The {dynamic: true} just makes it animate the profile photo if it is animated
if (message.channel.type === 'dm') {
client.channels.cache.get('channel_id').send(dmMessage) // Sends the embed to the specified channel ID
}
You can always create a variable like var dmID = "your_channel_id" and replace the 'channel_id' in the client.channels.cache.get with dmID so you can change the variable and it will change everywhere.

My discord.js embed message isnt sending to a specific channel

I don't know what the problem is, here is the code I am using, including the line that is meant to send it to the specified channel:
const Discord = require('discord.js')
const client = new Discord.Client();
const Discord = require('discord.js')
const client = new Discord.Client();
client.on("message", message => {
const embed = new Discord.messageEmbed()
embed.setAuthor(`Phaze Bot`)
embed.setTitle(`Commands List`)
embed.setDescription(`$kick: kicks a member \n $ban: bans a member \n $help music:
displays music commands \n $help: displays the help screen`)
client.guild.channels.cache.get(`801193981115367496`).send(embed)
})
client.login('login in here');
It is not sending this embed to the channel (by ID). Can anyone see where I am wrong?
update
it is now working, but still does not send the code to the channel when i message in it. the Terminal also shows this:
TypeError: Cannot read property 'channels' of undefined
This bot would send an embed every time a user sent a message in any channel.
You also have a typo at your client.on("message", (_message) => {
This needs to be:
client.on("message", message => {
Also, you need the client#channels#cache#get to be:
message.guild.channels.cache.get('801193981115367496').send(embed);
Since the ID is a string, it needs to be held in inverted commas or quotation marks.
As mentioned by Rémy Shyked, quoting the linter from VSCode:
Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers
The ID value is too large to be treated as an integer accurately
As mentioned above, your bot is going to send this embed every time a message is sent. Here is how you would make it respond to a basic help command (without using a handler):
const Discord = require('discord.js')
const client = new Discord.Client();
client.on("message", message => {
if (message.content.toLowerCase() === '!help') {
const embed = new Discord.messageEmbed()
embed.setAuthor(`Phaze Bot`)
embed.setTitle(`Commands List`)
embed.setDescription(`$kick: kicks a member \n $ban: bans a member \n $help music:
displays music commands \n $help: displays the help screen`)
message.guild.channels.cache.get('801193981115367496').send(embed);
};
});
client.login('client login here');
Also, please use semi-colons, and stop using backticks (`) when they aren't necessary - it saves a lot of errors later.

Discord bot sends replay when a specific user sends a message

userID = "7645876345"
client.on("message", function(message) {
if (message.author.id === userID) {
message.react('test');
}
});
I'm trying to get the discord bot to reply to a SPECIFIC user every time he writes something.
I changed the userID to random numbers for this post.
I figured it out! I replaced message.react with message.channel.send()

Resources