Unable to get member permissions from messageCreate - discord

I've made an automod system which only moderates users who don't have Administrator. For some reason I keep getting the error 'TypeError: Cannot read properties of null (reading 'permissions')'. Here's the code:
client.on('messageCreate', async message => {
if (message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return;

you need to require PermissionFlagsBits from discord.js
then on your code change it from:
if (message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return;
to
if (message.member.permissions.has(PermissionFlagsBits.Administrator)) return;

Try updating your dependencies with npm update

Did you tried adding a Const?
const { Permissions } = require('discord.js');

Related

.setNickname cannot read properties

Hello all im getting the following error
`
memberTarget.setNickname("nickname");
^
TypeError: Cannot read properties of undefined (reading 'setNickname')
`
for code
`
const memberTarget = client.users.cache.get("930654978777948160");
memberTarget.setNickname("nickname");
`
I have searched and there's not much information on this error, Things i have double checked is the bot permissions and it has the correct permissions to manage other users nicknames i'm currently stumped with this one
UserManager#cache.get() returns a User, while you need to get a Member (a user in a server). To do this, you can use the following:
const serverID = "39883983933980"; // this is the ID of your server
// it can be message.guild.id for example
client.guilds.fetch(serverID).then((server) => {
server.members.fetch("930654978777948160").then((memberTarget) => {
memberTarget.setNickname("nickname");
});
});
Note: this has to be added in the ready event of your code

I have a problem with discord.js v12 when trying to run the bot

im running a discord.js v12 bot and when i try to run it
this error pops up
client.user.setActivity(newActivity);
^
TypeError: Cannot read properties of null (reading 'setActivity')
at Timeout._onTimeout (C:\Users\Alexu\Desktop\cookie checker\events\ready.js:19:19)
at listOnTimeout (node:internal/timers:568:17)
at processTimers (node:internal/timers:510:7)
This means the client is not ready at the time. Put this in the ready event
client.once("ready", () => {
client.user.setActivity(newActivity)
}
The code seems correct. That the value is null maybe means that no user was found. Maybe something goes wrong with login. If the rest of the functions also don't work, then I would advise checking if the credentials are correct.

Discord.Js add role

I want to do a mute command, but when I do the command, the console output is (node:67916) UnhandledPromiseRejectionWarning: TypeError: msg.guild.roles.get is not a function
Any ideas how i can fix that?
As the error message says, msg.guild.roles.get is not a function. Its quite hard to answer a question without seeing the code, you should always share your code, but the correct way to find and add a role is the following:
const role = message.guild.roles.cache.find(role => role.name === 'Muted');
const member = message.mentions.members.first();
if(!member) member = message.author;
member.roles.add(role);
Since the update to V12, it is important that when trying to get roles, members or guilds you must include the .cache bit.

Discord.js TypeError: Cannot read property 'get' of undefined

got a message "TypeError: Cannot read property 'get' of undefined" whatever channel id I put in which is very annoying here is my code (very simple atm)
bot.on('guildMemberAdd',user => {
user.guild.channels.get("[existing channel ID]").send("SomeText")
});
Am I doing it wrong? I tried with .find("name","channel_name") got the same error and tried with a console log as "cache" and got the awnser "undefined" as it is a key feature of my bot to be abble to post in a specific channel it's annoying... ^^'
TY in advance for your help gonna keep trying to make it work^... ^^'
You need to use .cache to access the guild channels so it'd be user.guild.channels.cache.get("[existing channel ID]").send("SomeText") in your case.
You can test this :
bot.on('guildMemberAdd',user => {
const channelWelcome = user.guild.channels.cache.get("[channel's ID]"
channelWelcome.send("Your text")
});

"Announce" command giving errors in console

I'm trying to write an announce command which when used sends an announcement to the announcements channel, but only if you have a certain role, that works, but it shows an error in my console.
The error is TypeError: client.fetchGuild is not a function
if (await client.fetchGuild(message.guild.id).fetchMember(message.author.id).hasPermission("MENTION_EVERYONE") && message.content.startsWith(adminPrefix + 'announce')) {
const trueMessage = message.content.substr(10)
client.channels.get('545343689216491541').send('#everyone, ' + trueMessage)
}
How do I make it send no errors
P.s. I'm new to this, very new.
fetchGuild
is not a function. Use
client.guilds.get(id)
or
message.guild
as you already have the guild attached to the message object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=guilds
In order to verify if a member has a role, you'd better use this:
if(message.member.roles.cache.some(role => role.name === 'role name'))
then send the message inside the if statement

Resources