Ive tries some different things to make this work but it's not wanting to work
so am basicly trying to check someones custom status for a specific thing if there is for example .gg/test in it and give them a role than
My code
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get("927284722059595828");
const member = newPresence.member
const activities = member.user.presence.activities[0];
if (activities && (activities.state.includes( ".gg/test" ) || activities.state.includes("discord.gg/test" ))) {
return newPresence.member.roles.add(role)
} else {
if(member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role)
}
}
})
so can anyone correct the code to the correct format
and if possable make this multi server usable with a command thanks
the error i get
TypeError: Cannot read properties of undefined (reading 'id')
at Client.<anonymous> (/root/raegay2/boy.js:19:36)
at Client.emit (node:events:390:28)
at PresenceUpdateAction.handle (/root/raegay2/node_modules/discord.js/src/client/actions/PresenceUpdate.js:39:19)
at Object.module.exports [as PRESENCE_UPDATE] (/root/raegay2/node_modules/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js:4:33)
at WebSocketManager.handlePacket (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/root/raegay2/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/root/raegay2/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:390:28)
at Receiver.receiverOnMessage (/root/raegay2/node_modules/ws/lib/websocket.js:825:20)
I just tested your code and it worked fine. Did you enable Presence Intent on your bots discord developer page? If you are using Gateway Intents, you must specify the GUILD_PRESENCES intent in order to receive Presence Update events.
Related
I started reworking my bot to V14 and when I wanted to create the Ping Command I came to a slight issue. When I try to call the client.ws.ping it says TypeError: Cannot read properties of undefined (reading 'ws').
Code:
const { SlashCommandBuilder } = require('../node_modules/#discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with the Bots Ping!'),
async execute(interaction, client) {
await interaction.reply({
content: `Pong! The current API Ping is: ${client.ws.ping}ms and the Bot Ping is: ${message.createdTimestamp - interaction.createdTimestamp}ms.`,
ephemeral: false
});
},
};
Error:
max#MacBook-Pro Blax-Bot % node .
I'm currently logged in in [REDACTED], on 9 Servers with 7 people.
TypeError: Cannot read properties of undefined (reading 'ws')
at Object.execute (/Users/max/Documents/Blax-Bot/commands/ping.js:8:79)
at Client.<anonymous> (/Users/max/Documents/Blax-Bot/main.js:79:17)
at Client.emit (node:events:513:28)
at InteractionCreateAction.handle (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/actions/InteractionCreate.js:81:12)
at module.exports [as INTERACTION_CREATE] (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:480:22)
at WebSocketShard.onMessage (/Users/max/Documents/Blax-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:320:10)
at WebSocket.onMessage (/Users/max/Documents/Blax-Bot/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:513:28)
Thanks in advance.
Change the order of your execute function from this:
async execute(interaction, client) {
To this:
async execute(client, interaction) {
Putting it in a different order fails, that's what I found investigating. I hope it has been helpful for you.
CODE IS BELOW
I have a simple discord bot and added a message file under events--guild
module.exports = (Discord, client, message) => {
const prefix = '!';
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd);
if(command) commmand.execute(client, message, args, Discord);
}
```
Just Incase You need it here is my main file
```
const Discord = require('discord.js');
const client = new Discord.Client();
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
['command_handler', 'event_handler'].forEach(handler =>{;
require(`./handlers/${handler}`)(client, Discord)
})
client.login('My Token');
full error below
TypeError: Cannot read property 'content' of undefined
at Object.module.exports (E:\Software\Discord Bots\BombBot\events\guild\message.js:3:17)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (E:\Software\Discord Bots\BombBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (E:\Software\Discord Bots\BombBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
About: this discord bot is a simple bot that I am trying do with a command and event handler as I am new to discord js and not to javascript, I tried to see if there was a few issues, but couldn't find any. The debug window can't help either because its a value/variable that shows as undefined but is defined above.
as I am a bit new I used the tutorial: https://www.youtube.com/watch?v=Sihf7B8D4Y8 and rewrote the code multiple times. If you have an idea please tell me before I rewrite it again.
The Debugger is below
E:\Software\node JS\node.exe .\index.js
BOT Is ONLINE
events/client/ready.js:4
Uncaught TypeError: Cannot read property 'content' of undefined
No debugger available, can not send 'variables'
Process exited with code 1
It's hard to determine the origin of the error based on the info you provided.
The message content is not guaranteed. Messages from other bots that are embeds have no content, thus it's undefined. To fix this, add Optional Chaining in your condition:
if(!message.content?.startsWith(prefix) || message.author.bot) return;
If that doesn't work, that probably means your message object is not Discord Message at all. Make sure you pass the arguments to your function correctly.
This question already has an answer here:
Set Discord Bot Activity to the number of Online users. Discord.js
(1 answer)
Closed 2 years ago.
I am making my discord Admin bot (only for my server) and I am trying to do online members counter. I tried this:
client.guilds.cache.get(ids.serverID).members
.filter(m => m.presence.status === 'online').size;
and it gave this error:
var online = client.guilds.cache.get(ids.serverID).members.filter(m => m.presence.status === 'online').size;
^
TypeError: client.guilds.cache.get(...).members.filter is not a function
at Client.<anonymous> (/home/cyril/Documents/ALPHA-Admin/main.js:40:62)
at Client.emit (events.js:315:20)
at PresenceUpdateAction.handle (/home/cyril/Documents/ALPHA-Admin/node_modules/discord.js/src/client/actions/PresenceUpdate.js:39:19)
at Object.module.exports [as PRESENCE_UPDATE] (/home/cyril/Documents/ALPHA-Admin/node_modules/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js:4:33)
at WebSocketManager.handlePacket (/home/cyril/Documents/ALPHA-Admin/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/cyril/Documents/ALPHA-Admin/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/cyril/Documents/ALPHA-Admin/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/cyril/Documents/ALPHA-Admin/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (/home/cyril/Documents/ALPHA-Admin/node_modules/ws/lib/websocket.js:825:20)
Can anyone please help me out? Thank you.
The members property is not an array, it is a GuildMemberManager which doesn't have a .filter() method as part of its prototype. Try accessing the cache of your members list, which will return you a collection of the type Collection<Snowflake, GuildMember>, this type, unlike the GuildMemberManager, does have a filter method, which will return a Collection that you can then get the size of:
client.guilds.cache.get(ids.serverID).members.cache
.filter(m => m.presence.status === 'online').size;
I really don't know what's going on here. My code is inside the Akairo Framework but the error points in every direction to discord.js itself. Here's the error:
/home/runner/guard/Listeners/automod/nicknames.js:14
if (message.member.displayName.includes(word)) {
^
TypeError: Cannot read property 'displayName' of null
at module.exports.exec (/home/runner/guard/Listeners/automod/nicknames.js:14:32)
at Client.emit (events.js:327:22)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/guard/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/guard/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/guard/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/runner/guard/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
This is my code: (bannedWords is an array of strings that I run through to check against the member's nickname) (it's inside a message event listener)
const bannedWords = require('./badwords.json').words;
for (const word of bannedWords) {
if (message.member.displayName.includes(word)) {
message.member.setNickname(`Moderated Nickname`)
}
}
There is a bit of a logical flaw in your code: you do not check if the message was sent in DMs. If the message was indeed a DM, then message.member would be null (as DM channels are not part of any server), which pretty much explains your error.
You should fix this by either explicitly checking (and returning) if the channel is a DM, or if it is a falsy value
//some code ...
if(!message.member) return;
for (const word of bannedWords) {
if (message.member.displayName.includes(word)) {
message.member.setNickname(`Moderated Nickname`)
}
}
TypeError: Cannot read property 'displayName' of null
This means "message.member" is null and it cannot find the property "displayName", which then makes sense.
I think we need to see more of your code in order to figure out where it went wrong. I'm guessing you are trying to run that if statement on a member object that doesn't exist.
I am making a Discord bot using JavaScript and discord.js. There, I want to send a RichEmbed/MessageEmbed (I don't know how it's called) to a channel. Instead of sending an Embed though, it threw an error inside discord.js.
TypeError: fields.flat is not a function
at Function.normalizeFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:436:8)
at MessageEmbed.addFields (D:\discord-bot\node_modules\discord.js\src\structures\MessageEmbed.js:252:42)
at commands.forEach.command (D:\discord-bot\src\js\core\commands\commandManager.js:55:19)
at Array.forEach (<anonymous>)
at helloWorldEmbed (D:\discord-bot\src\js\core\commands\commandManager.js:54:18)
at Object.call (D:\discord-bot\src\js\core\commands\commandManager.js:29:13)
at Client.client.on (D:\discord-bot\src\js\core\bot.js:16:49)
at Client.emit (events.js:182:13)
at MessageCreateAction.handle (D:\discord-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\discord-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
I searched already for an answer, but it seems like I'm the only person having trouble with it.
Here's the code I used:
const embed = new MessageEmbed()
.setTitle('Hello World')
.setDescription('This is a test.')
.setColor('#3498db')
quotes.forEach(quote => {
embed.addField(quote.name, quote.description, true)
})
message.channel.send('Hello world.', embed)
As discussed in comments, updating Node.js solves the issue. Discord.js v12 requires 12.0.0 or newer because of the methods (like Array#flat() in the error) it uses for efficiency which don't exist in older versions.