Weird error 'UnhandledPromiseRejectionWarning: ReferenceError: message is not defined' - node.js

I'm trying to make a discord bot and it works fine but there's one thing that doesn't work. For some reason, it gives me this error when I try to do something with the bot. UnhandledPromiseRejectionWarning: ReferenceError: message is not defined I have no idea why. Heres the code that has the error: const collector = reactionMessage.createReactionCollector((reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("MANAGE_NICKNAMES"), { dispose: true }
Heres's some context of what the bot is if that helps: It's a ticket bot, where members can report problems to mods in a private channel. It creates a channel, and sends a message saying "staff will be with you shortly" and puts reactions so that you can close the ticket. When I try to close the ticket, it gives the error. The error happens around user.id).haspermission("MANAGE_NICKNAMES") That code is supposed to make people without the MANAGE_NICKNAMES permission unable to use the reactions, and theres no place in the code that i'm supposed to put 'message' so why is this happening?

Try replacing places message to reaction.message and if it doesn't work try using partials

Related

How to add reaction to a specific message using the ID? (discord.py)

I've been trying for hours a command that react to a message using the ID.
If someone writes !react (the message ID) the bot reacts to the message.
Can someone help me? I have no clue how to do this.
Use a converter to get the discord.Message instance of the message:
#client.command()
async def react(ctx, message: discord.Message):
...
Then use Message.add_reaction to add a reaction to it, which I'm sure you can figure out by yourself.
Keep in mind that in case the message ID is invalid, can't be found, or is not in the same channel as where the command gets called, the converter will fail & throw you an exception. If you pass in the message's URL instead of the ID, Discord will be able to figure out what channel the message was sent in so you can call the command from wherever you want.
You can get a message's URL by selecting Copy Message Link, which might be better for your users as getting the id requires Developer Mode to be on, which most people don't have enabled. The MessageConverter mentioned in the beginning can parse both id's and URL's so you don't have to worry about that part.

TypeError: Cannot read property 'name' of undefined discord.js

Sorry if i sound dumb saying this im new to Node.js
I tried looking other places and couldent find an answer and the docs didnt make sense to me but im getting errors when i want my bot to send a embed that automatticly says the guilds name in it so im using message.guild.name and i defined message but now im getting the error =
TypeError: Cannot read property 'name' of undefined
So im not sure what to do, my main code is
exports.run = (message) => {
const embed = new RichEmbed()
.setDescription(`Hello! Welcome to ${message.guild.name}!`)
.setColor(0xdd9323)
.setFooter(`This bot was made by Fortnitewinner21#1076 and Hextanium#5890`);
message.channel.send(embed).then(m => m.react('✅')).catch(console.error);
};
BTW- im v11
You don't have a message to refer to in this case. I suggest putting this inside an event or making this into a command. But since you said you wanted it to be sent automatically you will need to use an Event or an Interval.

DiscordAPIError: Invalid Form Body when trying to send embeds

This is one of the first commands I wrote whilst I've been learning, and recently it stopped working. I fiddled with it a little, but can't see where the problem comes from. When I run ~userinfo the following error comes up in the console:
Unhandled Rejection at: DiscordAPIError: Invalid Form Body
embed.footer.icon_url: Not a well formed URL.
This is the code for userinfo:
if (command === 'userinfo') {
var embed = new Discord.RichEmbed()
.setTitle('User Info')
.addField('Username', message.author.tag)
.addField('Server', message.guild.name)
.setColor(0xFF8AFF)
.setThumbnail(message.author.avatarURL)
.setFooter('Akasuki', version, client.user.avatarURL);
message.channel.send(embed);
}
Changing message.channel.send() to message.channel.sendEmbed() brings up errors, and there's been nothing wrong with using send() so far.
Also, this isn't necessary but if anyone knows how to add when the users account was created, to this embed it would be very helpful. Or even a resource or couple? Thanks for reading.
Used Cursed's solution (in comments):
"Change .setFooter('Akasuki', version, client.user.avatarURL); to .setFooter(`Akasuki ${version}`, client.user.avatarURL);"
Works perfectly! Thank you so much!

How to fix 'Cannot read property 'channel' of undefined' while using Node.JS and Discord.JS?

I'm building my first Discord bot, and I seem to have run into a snag. While making a ping command, I have run into a TypeError, which is in the title. My problem is, is that both the command, and the 'undefined' object are defined. The problem code is below. I am a new programmer to Node.JS so I am looking for help on how to fix this for the future as well.
I've tried to change the order in which the commands are read from, and have asked other more senior programmers for help. Again, very new to Node.JS, so not good at debugging it.
//Ping Command
function pingCommand(receivedMessage) {
const pingmessage = receivedMessage.channel.send('Pinging...')
pingmessage.edit(`Estimated ping: ${receivedMessage.createdTimestamp - pingmessage.createdTimestamp}ms, bot ping is ${Math.round(client.ping)}ms `)
}
I am expecting the bot to paste inside of my designated command channel the ping of the bot, which at this point is irrelevant, because I get an error every time the code is run.
TypeError: Cannot read property 'channel' of undefined

Discord.js sending a message to a specific channel

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

Resources