Check if user has perms? - discord.js

I tried doing this:
if(!msg.author.hasPermission('MANAGE_MESSAGES')) return msg.channel.send(":x: You don't have permissions. :x:")
but it gave me this error:
TypeError: msg.author.hasPermission is not a function
at Client.bot.on.msg (D:\Development\Jaycobe [DISCORD]\index.js:28:28)
at Client.emit (events.js:189:13)
at MessageCreateHandler.handle (D:\Development\Jaycobe [DISCORD]\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (D:\Development\Jaycobe [DISCORD]\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
at WebSocketConnection.onPacket (D:\Development\Jaycobe [DISCORD]\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (D:\Development\Jaycobe [DISCORD]\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (D:\Development\Jaycobe [DISCORD]\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:189:13)
at Receiver.receiverOnMessage (D:\Development\Jaycobe [DISCORD]\node_modules\ws\lib\websocket.js:789:20)
at Receiver.emit (events.js:189:13)
Help? (Tring to check if the user has permission to manage messages when a specific code is executed.)

If you use discordjs, v13 you can do like below:
if(message.member.permissions.has(Discord.Permissions.FLAGS.ADMINISTRATOR)) {
/* logic here */
}
All Permission Flags are under this property Discord.Permissions.FLAGS

Users don't have permissions, guild members do
you need to get the user as a guild member with
const member = message.guild.member(message.author);
so do this
if(!message.guild.member(message.author).hasPermission('MANAGE_MESSAGES')) {
return msg.channel.send(":x: You don't have permissions. :x:")
}

Related

DİSCORD give me a data problems

throw new TypeError(this.message["errors"]["blankData"]);
Help me plss
https://cdn.discordapp.com/attachments/1016841414434369556/1016841479173447691/unknown.png
Andd
node:events:491
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of undefined (reading 'rol')
at Client.<anonymous> (C:\Users\Administrator\Desktop\v14-moderasyon-main\index.js:220:51)
at Client.emit (node:events:525:35)
at module.exports [as GUILD_MEMBER_ADD] (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\discord.js\src\client\websocket\handlers\GUILD_MEMBER_ADD.js:17:14)
at WebSocketManager.handlePacket (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
at WebSocketShard.onPacket (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22)
at WebSocketShard.onMessage (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10)
at WebSocket.onMessage (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:513:28)
at Receiver.receiverOnMessage (C:\Users\Administrator\Desktop\v14-moderasyon-main\node_modules\ws\lib\websocket.js:1178:20)
at Receiver.emit (node:events:513:28)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
Node.js v18.8.0

(discord.js) TypeError: Cannot read property 'send' when trying to send a direct message

I was trying to use the following code to send a welcome message, but it seems that it can't read the property 'send'
Here's the code:
client.on("guildMemberAdd", (member) => {
member.guild.channels.cache.get(welcomeChannelId).send(`<#${member.id}> Welcome to the server!`)
})
Error message:
member.guild.channels.cache.get(welcomeChannelId).send(`<#${member.id}> Welcome to the server!`)
^
TypeError: Cannot read properties of undefined (reading 'send')
at Client.<anonymous> (/Users/admin/Desktop/Discord Bot/index.js:25:54)
at Client.emit (node:events:520:28)
at Object.module.exports [as GUILD_MEMBER_ADD] (/Users/admin/Desktop/Discord Bot/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBER_ADD.js:16:14)
at WebSocketManager.handlePacket (/Users/admin/Desktop/Discord Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:351:31)
at WebSocketShard.onPacket (/Users/admin/Desktop/Discord Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/Users/admin/Desktop/Discord Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/Users/admin/Desktop/Discord Bot/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:520:28)
at Receiver.receiverOnMessage (/Users/admin/Desktop/Discord Bot/node_modules/ws/lib/websocket.js:1137:20)
at Receiver.emit (node:events:520:28)
Please help me.
The channel might not be stored in the cache yet. You can do
member.guild.channels.fetch(welcomeChannelId).then(chan=>
member.guild.channels
.fetch(welcomeChannelId)
.then((chan) => chan.send(`<#${member.id}> Welcome to the server!`))
.catch(() => null);
Fetch will check if the channel is in the cache and if it isn't it will fetch it.

"TypeError: Cannot read property 'setName' of undefined" for channelCreate and channelDelete

I'm trying to trigger this event when a channel is added or removed it changes the name of the voice channel but for some reason .setName is undefined for trigger events "channelDelete" & "channelCreate" while for "guildMemberAdd" & "guildMemberRemove" it works perfectly fine.
Broken Code:
client.on('channelCreate', channelX => {
client.channels.cache.get(vcID).setName(`Channels Count: ${allChannels}`);
});
client.on('channelDelete', channelY => {
client.channels.cache.get(vcID).setName(`Channels Count: ${allChannels}`);
});
Working Code:
client.on('guildMemberAdd', memberX => {
client.channels.cache.get(MembersVCID).setName(`Member Count: ${totalCount}`);
});
client.on('guildMemberRemove', memberY => {
client.channels.cache.get(MembersVCID).setName(`Member Count: ${totalCount}`);
});
As far as I can see, there is no difference and It's broken for no reason.
Console Output:
D:\Discord Bots\SampleBot\commands\setup.js:144
client.channels.cache.get(vcID).setName(`Channels Count: ${allChannels}`);
^
TypeError: Cannot read property 'setName' of undefined
at Client.<anonymous> (D:\Discord Bots\SampleBot\commands\setup.js:144:51)
at Client.emit (node:events:394:28)
at ChannelCreateAction.handle (D:\Discord Bots\SampleBot\node_modules\discord.js\src\client\actions\ChannelCreate.js:17:14)
at Object.module.exports [as CHANNEL_CREATE] (D:\Discord Bots\SampleBot\node_modules\discord.js\src\client\websocket\handlers\CHANNEL_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Discord Bots\SampleBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (D:\Discord Bots\SampleBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:\Discord Bots\SampleBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (D:\Discord Bots\SampleBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (D:\Discord Bots\SampleBot\node_modules\ws\lib\websocket.js:970:20)

"ReferenceError: message is not defined" when trying to create a Discord.js bot

Just a heads up, I'm not good with code and I'm new to the coding scene. I'm trying to create a Discord bot for our server.
Right now the command is ping and I'm having some issues.
client.on('message', msg => {
if (msg.content === 'o!ping') {
ping = msg.createdTimestamp - message.createdTimestamp
msg.reply("Pong!\n\n🏓 The latency is" + ping + "ms.");
}
});
The error occurs at the "ping = msg.." line here's the error:
ReferenceError: message is not defined
at Client.<anonymous> (D:\Bot\bot.js:55:35)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (D:\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (D:\Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (D:\Bot\node_modules\ws\lib\websocket.js:825:20)
Line 55 is "ping = msg.createdTimestamp - message.createdTimestamp". Please help!
You passed msg in the callback function, so replace message with msg.
client.on('message', msg => {
if (msg.content === 'o!ping') {
ping = msg.createdTimestamp - msg.createdTimestamp
msg.reply("Pong!\n\n🏓 The latency is" + ping + "ms.");
}
});

Discord.js | TypeError: Cannot read property '0' of undefined

Okay so I'm trying to create a discord bot command (broadcast) and I want to get the args[0] as an ID to the broadcast channel, but when I try it this is what it logs:
TypeError: Cannot read property '0' of undefined
at Object.execute (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\commands\broadcast.js:8:40)
at Client.<anonymous> (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\index.js:69:11)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
at WebSocketShard.onPacket (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
at WebSocketShard.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
at WebSocket.onMessage (C:\Users\Jakub Sokol\Documents\Coding\Discord Bots\WaterDrop\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
Here is my code in the command:
module.exports = {
name: 'broadcast',
aliases: ["bc"],
usage: "[channel id] [message]",
description: "Broadcasts a message in a channel!",
execute(client, message, args){
const bcchannel = parseInt(args[0]);
const bcchannelsend = client.channels.cache.get(bcchannel);
if (isNaN(bcchannel)) {
return message.reply('that doesn\'t seem to be a valid channel ID!');
} else if(args){
bcchannelsend.send(args.join(' '));
}
}
}
Cannot read property '0' of undefined means that args is undefined.
You need to define the args variable before using it.
You can do something like this: const args = message.content.slice(PREFIX.length).trim().split(" ");
Next time you see this kind of error, make sure to check which variable is affected by the undefined issue. ^
For your index.js file, I should replace:
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
By:
try {
command.execute(client, message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
Since your execute() method has 3 parameters and not two.

Resources