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;
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.
I used replit database to save channel id
This is my code:
client.channels.cache.get(db.get(args[1])).send(args[2]);
and i get this error:
client.channels.cache.get(db.get(args[1])).send(args[2]);
^
TypeError: Cannot read properties of undefined (reading 'send')
at Client.<anonymous> (/home/runner/anonymesssendbot/index.js:44:49)
at Client.emit (node:events:390:28)
at Client.emit (node:domain:475:12)
at MessageCreateAction.handle (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/actions/MessageCreate.js:34:18)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:346:31)
at WebSocketShard.onPacket (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:478:22)
at WebSocketShard.onMessage (/home/runner/anonymesssendbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:317:10)
at WebSocket.onMessage (/home/runner/anonymesssendbot/node_modules/ws/lib/event-target.js:199:18)
repl process died unexpectedly: exit status 1
I'm guessing that db.get() returns a promise which you cannot pass as a value. Try retrieving the value first and then get the channel. Something like
const id = await db.get(args[1]);
const channel = client.channels.cache.get(id);
channel.send(args[2]);
There is also a possibility that the id is stored incorrectly in your server. So have a look at that. You could log the id to check if that is the case.
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.
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.