'TypeError: Cannot read properties of null (reading 'id'). AntiScam system
I wrote an anti-scam system but after launching I got an error
AntiScam.js
onst { Message, MessageEmbed } = require("discord.js");
const config = require("../../Structures/config.json");
const DB = require("../../Structures/Schemas/AntiScamDB");
module.exports = {
name: "messageCreate",
/\*\*
\*
\* @param {Message} message
\*/
async execute(message) {
DB.findOne({ Guild: message.guild.id }, async (err, data) =\> {
if (!data) return;
if(err) throw err;
const array = require(\`../../Structures/Valdiation/ScamLink.json\`);
if (array.some((word) =\> message.content.toLowerCase().includes(word))) {
message.delete();
const Ex = new MessageEmbed()
.setTitle("Scam detected")
.setColor(config.Warna)
.setThumbnail(\`${message.author.displayAvatarURL({ dynamic: true })}\`)
.setDescription(\`Please don't send any scam messages. Thank you.\`)
.addField(
"User:",
\`\\\`\\\`\\\`${message.author.tag} (${message.author.id})\\\`\\\`\\\`\`
)
.addField("Message Content:", \`\\\`\\\`\\\`${message.content}\\\`\\\`\\\`\`)
.setTimestamp();
await message.guild.channels.cache.get(data.Channel).send({embeds: \[Ex\]});
}
});
},
};
Error:
C:\Users\ekmrr\Desktop\squeak\Events\Client\AntiScam.js:12
DB.findOne({ Guild: message.guild.id }, async (err, data) => {
^
TypeError: Cannot read properties of null (reading 'id')
at Object.execute (C:\Users\ekmrr\Desktop\squeak\Events\Client\AntiScam.js:12:39)
at Client.<anonymous> (C:\Users\ekmrr\Desktop\squeak\Structures\Handlers\Events.js:22:54)
at Client.emit (node:events:406:35)
at MessageCreateAction.handle (C:\Users\ekmrr\Desktop\squeak\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\ekmrr\Desktop\squeak\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\ekmrr\Desktop\squeak\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
at WebSocketShard.onPacket (C:\Users\ekmrr\Desktop\squeak\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\ekmrr\Desktop\squeak\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\ekmrr\Desktop\squeak\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:394:28)
Please help!
At the beginning of the construction of the database and json arose, but after writing the command, the bot passed out :(
Solution 1:[1]
To be honest I cannot read your code clearly, but I can tell you that message.guild you are sending to DB.findOne is null.
I believe to play it safe with this you can do something like
message.hasOwnProperty('guild') &&
message.guild.hasOwnProperty('id') &&
DB.findOne({ Guild: message.guild.id }, async (err, data) => {
...
or
if (message.guild !== null)
DB.findOne({ Guild: message.guild.id }, async (err, data) => {
...
hope that helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
