discord bot greeting message - bots

hey im trying to get my discord bot make a greeting message when a new user is joinng the server.
here is my code.
bot.on('guildMemberAdd', member => {
const channel = member.guild.channels.find(channel => channel.name === "tvedes-gaming-room");
if(!channel) return;
channel.send(`Velkommen til, ${member}, Læs reglerne`)});
but i get this error.
Cannot read property 'channels' of undefined
at DiscordClient.<anonymous> (C:\Users\Wind\Desktop\TvedeBot\bot.js:42:31)
at DiscordClient.emit (events.js:305:20)
at emit (C:\Users\Wind\Desktop\TvedeBot\node_modules\discord.io\lib\index.js:1580:14)
at DiscordClient.handleWSMessage (C:\Users\Wind\Desktop\TvedeBot\node_modules\discord.io\lib\index.js:1904:11)
at WebSocket.emit (events.js:305:20)
at Receiver.ontext (C:\Users\Wind\Desktop\TvedeBot\node_modules\ws\lib\WebSocket.js:841:10)
at C:\Users\Wind\Desktop\TvedeBot\node_modules\ws\lib\Receiver.js:536:18
at Receiver.applyExtensions (C:\Users\Wind\Desktop\TvedeBot\node_modules\ws\lib\Receiver.js:371:5)
at C:\Users\Wind\Desktop\TvedeBot\node_modules\ws\lib\Receiver.js:508:14
at Receiver.flush (C:\Users\Wind\Desktop\TvedeBot\node_modules\ws\lib\Receiver.js:347:3)
plz help me :-)

You are using discord.io ( can be seen in the Error StackTrace ) you posted, the code you are using is for discord.js. You either need to find the discord.io equivalent code, or switch to discord.js. (Wich I would recommend due to the bigger community, wich makes it easier for beginners. )
If you want to start with discord.js: OpenSource Guide by the Community of discord.js

Related

Weird error 'UnhandledPromiseRejectionWarning: ReferenceError: message is not defined'

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

How do I get a server ID from a message in Discord.js?

I'm sorry for asking a relatively simple question, but I can't seem to find the answer online
You can get the guild of a message using someMessage.guild.
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=guild
That gives an instance of guild which has an id property. You can get it using someMessage.guild.id
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=id
Easy! <message>.guild.id
client.on('message', msg => {
msg.channel.send(msg.guild.id);
})

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.

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