const Discord = require ("Discord.js")
exports.exec = async (client, message, args) => {
const bug = args.slice().join(" ");
if (!args[0]) return message.channel.send(`${message.author}\` Please right, in as much detail about the bug\``);
const channel = client.channels.get('498750658569306123')
const embed = new Discord.RichEmbed()
.setAuthor(message.author.tag, message.author.avatarURL)
.setColor(0)
.setDescription(bug)
.setTimestamp()
.setFooter(`Suggestion by ${message.author.tag} from ${message.guild.name}`)
channel.send(embed)
Error thrown back is:
TypeError: Cannot read property 'send' of undefined
at Object.exports.exec (C:\Users\Cake\Peepo\modules\help\bugreport.js:14:11)
Everything seems to work fine.. I'm unsure how "send" is undefined? Someone care to explain?
Try doing this to find your report channel.
let channel = message.guild.channels.find(c => c.name === "report-channel-name-here");
channel.send(embed);
if that fails, make sure that your bot can access the channel.
Related
I'm trying to make an event that when someone added my bot it will send a message embed on a channel that my bot can send the message embed.
discord.js: v13.6.0
Node: v17.7.2
events/guildCreate.js
const client = require("../index");
const discord = require("discord.js")
client.on("guildCreate", async (client, guild) => {
let channel = guild.channels.cache.find(
channel =>
channel.type === "text" &&
channel.permissionsFor(guild.me).has("SEND_MESSAGES")
);
channel.send( new discord.MessageEmbed()
.setDescription("**Thank you for adding me!**")
.setColor("#fcba03"))
})
edit:
I kinda of fix my problem with the reading channels:
const client = require("../index");
const { MessageEmbed } = require("discord.js")
client.on("guildCreate", async (guild, message) => {
let channel = guild.channels.cache.find(
channel =>
channel.type === "text" &&
channel.permissionsFor(guild.me).has("SEND_MESSAGES")
);
const embed = new MessageEmbed()
embed.setDescription("Thank you for adding me!")
embed.setColor("#2f3136")
message.guild.channels.cache.get(channel).send(embed)
})
but then again got new error:
TypeError: Cannot read properties of undefined (reading 'guild')
I think your “channel” is an array instead of a channel object, because you're filtering all text channels. Make sure to select the first channel of the array/collection.
Try to show what shows up when you log the “channel”
I am trying to get the bot to add roles in one discord and also add a role in another discord. but I keep getting the "roles" is not defined error. I have little to no coding knowledge and most of my code is a combination of things I find on google or friends teach me, so please excuse me if it is a dumb problem with a simple solution.
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.GUILD_ROLES] });
module.exports = {
name: 'accept',
description: 'Adds A Player To The Whitelist',
permissions: `ADMINISTRATOR`,
execute(message, args, add, roles, addRole, guild) {
message.delete()
let rMember = guild.roles.cache
message.mentions.members.first() || // `.first()` is a function.
message.guild.members.cache.find((m) => m.user.tag === args[0]) ||
message.guild.members;
let role1 =
message.guild.roles.cache.find((r) => r.id == roleID)
let role2 =
message.guild.roles.cache.find((r) => r.id == roleID)
let server = client.guilds.cache.get('guild ID')
let memberRole = server.guild.roles.get("role ID");
rMember.roles.add(role1).catch((e) => console.log(e));
rMember.roles.add(role2).catch((e) => console.log(e));
rMember.roles.add(memberRole).catch((e) => console.log(e));
rMember.send(`message content`);
message.channel.send('message content')
}};
The error occurs in this line:
let memberRole = server.guild.roles.get("872432775330955264");
let server = client.guilds.cache.get('guild ID')
let memberRole = server.guild.roles.get("role ID");
These particular lines justify your error, in the first case you are assigning the property of a guild object to server by getting the guild from the cache, now you see a guild object does not have a property further named guild to it so your error actually rests in the next line, where you are trying to get from server.guild it's same as saying guild.guild which makes no actual sense.
Only correction you would want to make with your code would be something of this sort:
let server = client.guilds.cache.get('guild ID')
let memberRole = server.roles.cache.get("role ID");
Im trying to make a command with in my Discord bot to make unban a person. When I finnished the code I got an error. The error is Cannot read properties of undefined (reading 'fetchBans'). The code I use to make it is
message.guild.cache.fetchBans().then(async bans => {
if (bans.size === 0) return message.channel.send("No one is banned on the server!")
let bannedUser = bans.find(ban => ban.user.id == userID)
if (!bannedUser) return message.channel.send("This user isn't banned!")
await message.guild.members.unban(bannedUser.user, reason).catch(err => {
return message.channel.send("Something went wrong!")
}).then(() => {
let embed = new MessageEmbed()
.setColor("GREEN")
.setTitle(`Member unbanned!`)
.setDescription("")
.setTimestamp()
.setFooter(this.client.user.username)
embed.addField(`member = ${target.username}`, `reason = ${reason}`, false)
message.channel.send({embeds: [embed]});
if (row[0].logs_channel != null) this.client.channels.cache.get(row[0].logs_channel).send({embeds: [embed]})
message.delete()
})
The line where it goes wrong is this line:
message.guild.cache.fetchBans().then(async bans => {
When I run this command it will give the error. I also tried to do something else. The code i tried else for that line is:
message.guild.fetchBans().then(async bans => {
This line also gave an error that says: TypeError: message.guild.fetchBans is not a function.
I don't know what i can do about this. I hope somebody can help me.
Node.js version: 16.9.1
discord.js version: 13.1.0
According to the documentation there is no fetchBans on Guild. Maybe you're looking for message.guild.bans.fetch().
It's also worth noting that message.guild is nullable - if the message was a direct message (not sent in a guild) it will be null.
I'm making a discord welcomer bot and there's a problem where when someone joins the server it sends this error:
TypeError: Cannot read property 'toString' of undefined
Here is the source code:
module.exports = (client) => {
const channelid = "865471665168580628";
client.on("guildMemberAdd", (member) => {
const serverid = member.guild.id
const guild = client.guilds.cache.get(serverid);
console.log("member");
const ruleschannel = guild.channels.cache.find(channel => channel.name === "rules");
const message = `welcome <#${member.id}> to music and chill! please read the ${member.guild.channels.cache.get(ruleschannel).toString()} before you start chatting.`;
const channel = member.guild.channels.cache.get(channelid);
channel.send(message);
})
}
Can someone please help me?
It means member.guild.channels.cache.get(ruleschannel) is undefined. As ruleschannel is a channel object, and the Collection#get() method needs a snowflake, you need to use its id property.
So member.guild.channels.cache.get(ruleschannel.id) should work.
A better way would be to check if the ruleschannel exists though. Also, you can just simply add rulesChannel inside the backticks and it gets converted to a channel link. Check out the code below:
client.on('guildMemberAdd', (member) => {
// not sure why not just: const { guild } = member
const guild = client.guilds.cache.get(member.guild.id);
const rulesChannel = guild.channels.cache.find((channel) => channel.name === 'rules');
if (!rulesChannel)
return console.log(`Can't find a channel named "rules"`);
const channel = guild.channels.cache.get(channelid);
const message = `Welcome <#${member.id}> to music and chill! Please, read the ${rulesChannel}.`;
channel.send(message);
});
The code won't work perfectly, i find couple of issues:
When I'm connected to a VC and trying to attempt the command the bot leaves the VC but triggers two functions at a time i.e., General leave funtion and author not in the same channel.
When i give command being outside the VC(bot connected to the VC) the bot responds with function we are not in a same channel funtion., rather i want it to respond with you're not connected to VC
When i give command while being outside VC and bot not connected to VC, it responds nothing and at console i get error:
(node:37) UnhandledPromiseRejectionWarning: TypeError: cannot read property "id" of undefined
Code:
client.on('message', async (message) => {
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'leave') {
let authorVoice = message.member.voice;
const embed1 = new discord.MessageEmbed()
.setTitle('🚫`You are not in a Voice Channel!`')
.setColor('RED')
.setTimestamp();
if (!authorVoice.channel) return message.channel.send(embed1);
const embed2 = new discord.MessageEmbed()
.setTitle('⛔`We are not in a same Voice Channel`')
.setColor('YELLOW')
.setTimestamp();
if (authorVoice.channel.id !== client.voice.channel.id)
return message.channel.send(embed2);
const embed = new discord.MessageEmbed()
.setTitle('`Successfully left the Voice Channel`✔')
.setColor('RED')
.setTimestamp();
authorVoice.channel.leave()
message.channel.send(embed);
}
});
Can someone help me fix it?
client.voice doesn't have a channel property. It means that client.voice.channel is undefined. When you try to read the id of it, you receive the error that you "cannot read property "id" of undefined".
You probably want the connections that is a collection of VoiceConnections. A connection does have a channel property but as client.voice.connections is a collection, you need to find the one you need first.
As you only want to check if a channel with the same ID exists, you can use the .some() method. In its callback function you check if any of the connections has the same channel ID as the message author's channel ID. So, instead of if(authorVoice.channel.id !== client.voice.channel.id) you could create a variable that returns a boolean. It returns true if the bot is in the same channel and false otherwise:
const botIsInSameChannel = client.voice.connections.some(
(c) => c.channel.id === authorVoice.channel.id,
);
// bot is not in the same channel
if (!botIsInSameChannel)
return message.channel.send(embed2);
Here is the updated code if anyone watching for:
client.on('message', async message => {
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'leave') {
let authorVoice = message.member.voice;
const botIsInSameChannel = client.voice.connections.some(
(c) => c.channel.id === authorVoice.channel.id,
);
const embed1 = new discord.MessageEmbed()
.setTitle(':no_entry_sign: You are not in a Voice Channel!')
.setColor('RED')
.setTimestamp();
if(!authorVoice.channel) return message.channel.send(embed1);
const embed2 = new discord.MessageEmbed()
.setTitle(':no_entry: We are not in a same Voice Channel')
.setColor('YELLOW')
.setTimestamp();
if (!botIsInSameChannel) return message.channel.send(embed2)
const embed = new discord.MessageEmbed()
.setTitle('Successfully left the Voice Channel✔')
.setColor('RED')
.setTimestamp();
authorVoice.channel.leave();
message.channel.send(embed);
}
});