DiscordJs v14 create a message error includes - discord.js

I don't understand why i have this error i want create a bot for add role if a people write link in description but the code don't want get my includes
`
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const config = require('./config');
const bot = new Client({
intents: [3276799]
});
bot.commands = new Collection()
require('./src/Structure//Handler/Event')(bot);
require('./src/Structure//Handler/Command')(bot);
bot.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get("1048286446345261137");
const member = newPresence.member
const activities = newPresence.activities[0];
if (activities && (activities.state.includes(".gg/slayde" ) || activities.state.includes(".gg/slayde"))) {
return newPresence.member.roles.add(role)
} else {
if(member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role)
}
}
})
bot.login(config.clients.token);
`
The error
TypeError: Cannot read properties of null (reading 'includes')
I try to fix but i'm blocked

According to the docs, the state property of Activity is optional (?string), which means it doesn't always have a value. You have to make sure it exists before you call includes() on it.
if (activities && activities.state && (activities.state.includes(".gg/slayde" ) || activities.state.includes(".gg/slayde"))) {
// etc
}

Related

How to get active member count Discord JS

I have a function which counts all active members on active server
I've tried to get into client.guilds.cache then filter users by their presence.status to check if the user is online. As far as I am aware what's happening .size should return a number of active members on the server, but instead it returns 0, no matter what I try.
Here's the code I've tried
I call function in client.on here and pass client as an argument
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers],
});
client.on("ready", () => {
activeMembersCounter(client)
});
Then in activeMembersCounter.ts
function activeMembersCounter(client: Client<boolean>): void {
const guild = client.guilds.cache.get(config.GUILD_ID);
const onlineMembers = guild.members.cache.filter((m) => m.presence?.status === "online"&& !member.user.bot).size;
console.log(onlineMembers) // logs 0
}
I've also tried async version with fetch
async function activeMembersCounter(client: Client<boolean>): Promise<void> {
const guild = client.guilds.cache.get(config.GUILD_ID);
const onlineMembers = (await guild.members.fetch()).filter((member) => member.presence?.status === "online" && !member.user.bot);
console.log(onlineMembers.size);
}
I'm not 100% sure if my approach is correct here. I would love to get some advice :)
Two things you need to change.
Use the GatewayIntentBits.GuildPresences flag, m.presence returns null if you don't use it.
member is undefined in your online members variable. Use m.user.bot instead of member.user.bot.
Working code:
const { GatewayIntentBits, Client} = require('discord.js');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences],
});
function activeMembersCounter(c) {
const guild = c.guilds.cache.get("GUILD_ID");
console.log(guild);
const onlineMembers = guild.members.cache.filter((m) => m.presence?.status == "online" && !m.user.bot).size;
console.log(onlineMembers);
}
client.once('ready', () => {
activeMembersCounter(client);
})
Edit: If you're looking for active members, this could include DND members and Idle members. You might want to consider using m.presence?.status != "offline" instead of m.presence?.status == "online" to include these other statuses in your count.

TypeError: Cannot read properties of undefined (reading 'ban')

client.on("channelCreate", async function(channel) {
const logs = await channel.guild.fetchAuditLogs({ limit: 1, type: 'CHANNEL_CREATE' });
const log = logs.entries.first();
if (!log) return;
const kanal = channel.guild.channels.cache.find(ch => ch.name === 'ticket')
const embed = new MessageEmbed()
.setTitle("🆘Protection System🆘")
.setDescription(`ο/η ${log.executor} έγινε banned επειδή έκανε αρκετά κανάλια`)
.setColor("RED")
const get = db.get(`channel2_${log.executor.id}`)
if(get === true)return channel.delete(),kanal.send({embeds: [embed]}),logs.executor.ban('Protection : Channel Created')
});
When I run this code, it returns this error:
TypeError: Cannot read properties of undefined (reading 'ban')
In your current code, the logs variable refers to GuildAuditLogs which is the collection of audit logs. It doesn't have an executor property. Instead, what you are looking for is the log variable (without an 's'). It contains the executor property. But using the executor property gives you an User object, so it still wouldn't have an .ban() property. So you would have to fetch the GuildMember who did the action. So, your fixed code would look like this:
client.on("channelCreate", async function(channel) {
const logs = await channel.guild.fetchAuditLogs({ limit: 1, type: 'CHANNEL_CREATE' });
const log = logs.entries.first();
const logExecutorMember = await channel.guild.members.fetch(log.executor.id)
if (!log) return;
const kanal = channel.guild.channels.cache.find(ch => ch.name === 'ticket')
const embed = new MessageEmbed()
.setTitle("🆘Protection System🆘")
.setDescription(`ο/η ${log.executor} έγινε banned επειδή έκανε αρκετά κανάλια`)
.setColor("RED")
const get = db.get(`channel2_${log.executor.id}`)
if(get === true)return channel.delete(),kanal.send({embeds: [embed]}),logExecutorMember.ban('Protection : Channel Created')
});

How to make Discord Bot Automatically Role Someone when they have certain word in status

trying to make it so if someone for example put my discord servers invite in their status ".gg/testing" it would give them a role and if they removed it would take their role away heres the code i have got right now off a similar stack overflow post heres my code right now but it doesnt give the user there role when they have .gg/testing in their status any tips?
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const roleID = "972823139119685712";
client.on("presenceUpdate", async (_, newPresence) => {
const role = newPresence.guild.roles.cache.get(roleID);
const status = ".gg/testing";
const member = newPresence.member;
if (member.presence.activities[0].state?.includes(status)) {
return newPresence.member.roles.add(role);
} else {
if (member.roles.cache.has(role)) {
newPresence.member.roles.remove(role);
}
}
});
client.login("");
This is the code you're looking for. Credit to HellCatVN whose code I edited a tiny bit to make this work.
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.get("941814488917766145");
const member = newPresence.member
const activities = member.presence.activities[0];
if (activities && (activities.state.includes( ".gg/testing" ) || activities.state.includes("discord.gg/testing" ))) {
return newPresence.member.roles.add(role)
} else {
if(member.roles.cache.get(role.id)) {
newPresence.member.roles.remove(role)
}
}
})
The role will be gone if the user turns on invisible status too.

How do I use #discordjs/voice AudioPlayer methods across modules?

I am adding a music-playing feature to my bot using #discordjs/voice, following the voice guide on discordjs.guide. My slash commands are all stored on different files. I want to use the pause method of the AudioPlayer class outside of play.js.
The relevant parts of play.js are:
const { SlashCommandBuilder } = require('#discordjs/builders');
const { StreamType, createAudioPlayer, createAudioResource, joinVoiceChannel } = require('#discordjs/voice');
module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('An example of the play command.'),
async execute(interaction) {
const connection = joinVoiceChannel({ channelId: interaction.member.voice.channel.id, guildId: interaction.guild.id, adapterCreator: interaction.guild.voiceAdapterCreator });
const stream = ytdl('https://www.youtube.com/watch?v=jNQXAC9IVRw', { filter: 'audioonly' });
const resource = createAudioResource(stream, { inputType: StreamType.Arbitrary });
const player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
await interaction.reply('Playing.');
},
};
and of pause.js are:
const { SlashCommandBuilder } = require('#discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('pause')
.setDescription('An example of the pause command.'),
async execute(interaction) {
player.pause();
await interaction.reply('Paused.');
},
};
My console got the following error when I ran the /pause command after I tried to export player from player.js and require it in pause.js:
TypeError: Cannot read properties of undefined (reading 'pause')
What do I need to do to use player.pause() outside of play.js?
You should take a look at this part of the Discord.js guide, in the Access part. The getVoiceConnection method serves this purpose.
It allows you to use the connection created in play.js anywhere in your code. Then, you just need to change the state of the player with connection.state.subscription.player.pause().
I did my pause.js by checking whether the member using the pause command is in a channel and if he is, whether the bot is in it or not:
if (!interaction.member.voice.channelId) {
return interaction.reply('not in a channel.');
}
const connection = getVoiceConnection(interaction.member.voice.channel.guildId);
if (!connection || (connection.joinConfig.channelId != interaction.member.voice.channelId)) {
return interaction.reply('The bot is not in this channel.');
}
connection.state.subscription.player.pause();
await interaction.reply('Paused.');
discord.js

Want a code that detects custom status and gives the person a role on discord

I am trying to make a code that searches a custom status for the phrase ".gg/RoundTable" and will then give the person a certain role I have in my server.
Here is my code so far , the code runs with no errors but it will not assign the role.
const Discord = require("discord.js")
const client = new Discord.Client()
const mySecret = process.env['TOKEN']
client.login(mySecret)
const roleID = 865801753462702090
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.find(role => role.name === 'Pic Perms (.gg/RoundTable)');
const status = ".gg/RoundTable"
const member = newPresence.member
console.log(member.user.presence.activities[0].state)
if(member.presence.activities[0].state.includes(status)){
return newPresence.member.roles.add(roleID)
} else {
if(member.roles.cache.has(roleID)) {
newPresence.member.roles.remove(roleID)
}
}
})
Try this:
const Discord = require("discord.js");
const client = new Discord.Client();
const roleID = "851563088314105867";
client.on("presenceUpdate", async (_, newPresence) => {
const role = newPresence.guild.roles.cache.get(roleID);
const status = ".gg/RoundTable";
const member = newPresence.member;
if (member.presence.activities[0].state?.includes(status)) {
return newPresence.member.roles.add(role);
} else {
if (member.roles.cache.has(role)) {
newPresence.member.roles.remove(role);
}
}
});
client.login("your-token");
I'd recommend finding your role in the RoleManager.cache using get() as you already have the roleID and then actually assign that role instead of the roleID. Note I added an optional chaining operator since if a user does not have a custom status .state will be null.

Resources