我不明白为什么我有这个错误,我想创建一个机器人添加角色,如果一个人在描述中写链接,但代码不想得到我的包括'
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);
误差
TypeError: Cannot read properties of null (reading 'includes')
我试图修复,但我被阻止
根据文档,Activity
的state
属性是可选的(?string
),这意味着它并不总是有一个值。在调用includes()
之前,您必须确保它存在。
if (activities && activities.state && (activities.state.includes(".gg/slayde" ) || activities.state.includes(".gg/slayde"))) {
// etc
}