Cannot read properties of null when editing a automod rule

Description When attempting to update an automod rule i get an error from discord.js that it can not read properties of null when reading 'autoModerationRules'. Package [email protected] Node v20.7.0 Code
const guild = await client.guilds.fetch(guildId);
const automodRule = await guild.autoModerationRules.fetch(ruleId);
const result = await automodRule.edit(ruleConfig);
const guild = await client.guilds.fetch(guildId);
const automodRule = await guild.autoModerationRules.fetch(ruleId);
const result = await automodRule.edit(ruleConfig);
Both guild and automodRule exist and return valid objects Error
TypeError: Cannot read properties of null (reading 'autoModerationRules')
at AutoModerationRule.edit (<REDACTED>\node_modules\discord.js\src\structures\AutoModerationRule.js:146:23)
TypeError: Cannot read properties of null (reading 'autoModerationRules')
at AutoModerationRule.edit (<REDACTED>\node_modules\discord.js\src\structures\AutoModerationRule.js:146:23)
The above leads to the following method, with the cursor after the . after the guild
/**
* Edits this auto moderation rule.
* @param {AutoModerationRuleEditOptions} options Options for editing this auto moderation rule
* @returns {Promise<AutoModerationRule>}
*/
edit(options) {
return this.guild.autoModerationRules.edit(this.id, options);
^
}
/**
* Edits this auto moderation rule.
* @param {AutoModerationRuleEditOptions} options Options for editing this auto moderation rule
* @returns {Promise<AutoModerationRule>}
*/
edit(options) {
return this.guild.autoModerationRules.edit(this.id, options);
^
}
Any help would be appreciate, i am not sure what is causing this issue, thanks in advance.
12 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Woffer12
Woffer12OP11mo ago
Also, please ping me if you reply, so i can see that you have replied.
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
what intents do u have enabled in the client constructor
Woffer12
Woffer12OP11mo ago
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.MessageContent,
GatewayIntentBits.AutoModerationConfiguration,
GatewayIntentBits.AutoModerationExecution,
],
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.MessageContent,
GatewayIntentBits.AutoModerationConfiguration,
GatewayIntentBits.AutoModerationExecution,
],
These are the ones enabled, i just added the Automod ones and tried again but it appears to have made no change.
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
do u use custom cache or sweepers? in the docs it says <AutoModerationRuleManager>.guild isn't nullable so for me it looks like ur guild caches messed up or smth
Woffer12
Woffer12OP11mo ago
i don't believe so this is quite interesting because i am not sure if this is something i can fix hm
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
maybe the client isn't ready when u run this code, log client.isReady()
Woffer12
Woffer12OP11mo ago
yea it returns true if anyone else has any ideas i would like to hear just to add, using things like .setName() also do not work (they just call this.edit() so basically the same thing i am trying to do overall) does a rule.edit() work fine for you? going to go to bed now but if you know anything about this please comment wumplove
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
i don't use automod but i havent seen anyone else who ran into this issue so i dont think its a problem with djs
Sinista
Sinista11mo ago
yea it works for me
Woffer12
Woffer12OP11mo ago
logs the 2 rules present in the guild so should i be using the cache instead of directly fetching things? strange dang So i switched it to get from the cache but that does not fix the issue, both client.guilds.cache and guild.autoModerationRules.cache return what they should but i still get the null.
const guild = client.guilds.cache.get(guildId);
const automodRule = guild.autoModerationRules.cache.get(ruleId);
const result = await automodRule.edit(ruleConfig);
const guild = client.guilds.cache.get(guildId);
const automodRule = guild.autoModerationRules.cache.get(ruleId);
const result = await automodRule.edit(ruleConfig);
Collection(2) [Map] {
'1165052597539967056' => AutoModerationRule {
id: '1165052597539967056',
guild: null,
creatorId: '224188754946949130',
triggerType: 1,
name: 'Block Custom Words',
eventType: 1,
triggerMetadata: {
keywordFilter: [Array],
regexPatterns: [Array],
presets: [],
allowList: [Array],
mentionTotalLimit: null,
mentionRaidProtectionEnabled: false
},
actions: [ [Object], [Object], [Object] ],
enabled: true,
exemptRoles: Collection(0) [Map] {},
exemptChannels: Collection(0) [Map] {}
},
'1193716862526169168' => AutoModerationRule {
id: '1193716862526169168',
guild: null,
creatorId: '308272531570425856',
triggerType: 5,
name: 'Block Mention Spam',
eventType: 1,
triggerMetadata: {
keywordFilter: [],
regexPatterns: [],
presets: [],
allowList: [],
mentionTotalLimit: 20,
mentionRaidProtectionEnabled: true
},
actions: [ [Object] ],
enabled: true,
exemptRoles: Collection(0) [Map] {},
exemptChannels: Collection(0) [Map] {}
}
}
Collection(2) [Map] {
'1165052597539967056' => AutoModerationRule {
id: '1165052597539967056',
guild: null,
creatorId: '224188754946949130',
triggerType: 1,
name: 'Block Custom Words',
eventType: 1,
triggerMetadata: {
keywordFilter: [Array],
regexPatterns: [Array],
presets: [],
allowList: [Array],
mentionTotalLimit: null,
mentionRaidProtectionEnabled: false
},
actions: [ [Object], [Object], [Object] ],
enabled: true,
exemptRoles: Collection(0) [Map] {},
exemptChannels: Collection(0) [Map] {}
},
'1193716862526169168' => AutoModerationRule {
id: '1193716862526169168',
guild: null,
creatorId: '308272531570425856',
triggerType: 5,
name: 'Block Mention Spam',
eventType: 1,
triggerMetadata: {
keywordFilter: [],
regexPatterns: [],
presets: [],
allowList: [],
mentionTotalLimit: 20,
mentionRaidProtectionEnabled: true
},
actions: [ [Object] ],
enabled: true,
exemptRoles: Collection(0) [Map] {},
exemptChannels: Collection(0) [Map] {}
}
}
I get the rules originally with this code (below) do some stuff with it, then run the code above ( https://canary.discord.com/channels/222078108977594368/1198436552083247114/1198617628864938114 ) on it
const guild = await client.guilds.fetch(guildId);
const automodRule = await guild.autoModerationRules.fetch();
const guild = await client.guilds.fetch(guildId);
const automodRule = await guild.autoModerationRules.fetch();
Here is the full code of the sections
async function handleGetAllAutomodRules(client, data) {
const { guildId } = data;

try {
/** @type {Guild} */
const guild = client.guilds.cache.get(guildId);
const automodRule = guild.autoModerationRules.cache;

return { data: automodRule, ok: true };
} catch (error) {
console.error(error);
}
}

async function handleBlockMention(client, data) {
const { guildId, ruleId, ruleConfig } = data;

try {
/** @type {Guild} */
const guild = client.guilds.cache.get(guildId);
if (!guild) return { error: "No Guild", data: `No Guild was found with the ID: ${guildId}`, ok: false, status: 404 };

const automodRule = guild.autoModerationRules.cache.get(ruleId);
if (!automodRule) return { error: "No Rule", data: `No Rule was found with the ID: ${ruleId}`, ok: false, status: 404 };

const result = await automodRule.edit(ruleConfig);

return { data: result, ok: true, status: 200 };
} catch (error) {
console.error(error);
return { error: error.name, data: error.message, ok: false, status: 500 };
}
}
async function handleGetAllAutomodRules(client, data) {
const { guildId } = data;

try {
/** @type {Guild} */
const guild = client.guilds.cache.get(guildId);
const automodRule = guild.autoModerationRules.cache;

return { data: automodRule, ok: true };
} catch (error) {
console.error(error);
}
}

async function handleBlockMention(client, data) {
const { guildId, ruleId, ruleConfig } = data;

try {
/** @type {Guild} */
const guild = client.guilds.cache.get(guildId);
if (!guild) return { error: "No Guild", data: `No Guild was found with the ID: ${guildId}`, ok: false, status: 404 };

const automodRule = guild.autoModerationRules.cache.get(ruleId);
if (!automodRule) return { error: "No Rule", data: `No Rule was found with the ID: ${ruleId}`, ok: false, status: 404 };

const result = await automodRule.edit(ruleConfig);

return { data: result, ok: true, status: 200 };
} catch (error) {
console.error(error);
return { error: error.name, data: error.message, ok: false, status: 500 };
}
}
Rule config data looks like:
{
"name": "Block Custom Words",
"eventType": 1,
"triggerMetadata": {
"allowList": [],
"keywordFilter": [
"Word 1",
"Word 2",
"Word 3",
"Word 4",
"Word 5"
],
"regexPatterns": [],
"presets": [],
"mentionTotalLimit": null,
"mentionRaidProtectionEnabled": false
},
"actions": [],
"enabled": true,
"exemptRoles": [],
"exemptChannels": []
}
{
"name": "Block Custom Words",
"eventType": 1,
"triggerMetadata": {
"allowList": [],
"keywordFilter": [
"Word 1",
"Word 2",
"Word 3",
"Word 4",
"Word 5"
],
"regexPatterns": [],
"presets": [],
"mentionTotalLimit": null,
"mentionRaidProtectionEnabled": false
},
"actions": [],
"enabled": true,
"exemptRoles": [],
"exemptChannels": []
}
just return it to the api, i don't alter it. Its just sending/receiving data between the bot and api through nats, i don't know the specifics as i was not the one to set it up would there be a way for me to fix this issue without changing how that works? not particaly fond of doing a complete re-write of that system kappa is weird because other stuff seems to work fine hidethepain
Rem
Rem11mo ago
ty Hi, even this isn't working
await client.guilds.fetch();
const guild = interaction.guild;
const automodRule = await guild.autoModerationRules.fetch("any guild id");
if (automodRule) {
console.log(`Automod rule found: ${automodRule}`);
} else {
console.log("No automod rule found");
}

const editttttt = {
name: "Block Custom Words",
eventType: 1,
triggerMetadata: {
allowList: ["yes"],
keywordFilter: ["hi"],
regexPatterns: ["no"],
presets: [],
mentionTotalLimit: null,
mentionRaidProtectionEnabled: false,
},
actions: [],
enabled: true,
exemptRoles: [],
exemptChannels: [],
reason: "Automod test",
};

await automodRule.edit(editttttt);
await client.guilds.fetch();
const guild = interaction.guild;
const automodRule = await guild.autoModerationRules.fetch("any guild id");
if (automodRule) {
console.log(`Automod rule found: ${automodRule}`);
} else {
console.log("No automod rule found");
}

const editttttt = {
name: "Block Custom Words",
eventType: 1,
triggerMetadata: {
allowList: ["yes"],
keywordFilter: ["hi"],
regexPatterns: ["no"],
presets: [],
mentionTotalLimit: null,
mentionRaidProtectionEnabled: false,
},
actions: [],
enabled: true,
exemptRoles: [],
exemptChannels: [],
reason: "Automod test",
};

await automodRule.edit(editttttt);
error:
TypeError: Cannot read properties of null (reading 'autoModerationRules')
at AutoModerationRule.edit (C:\Users\me\Documents\GitHub\bot\node_modules\discord.js\src\structures\AutoModerationRule.js:146:23)
at Object.execute (C:\Users\me\Documents\GitHub\bot\plugins\help\help.js:239:44)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

(yes, i tested in a /help command)
TypeError: Cannot read properties of null (reading 'autoModerationRules')
at AutoModerationRule.edit (C:\Users\me\Documents\GitHub\bot\node_modules\discord.js\src\structures\AutoModerationRule.js:146:23)
at Object.execute (C:\Users\me\Documents\GitHub\bot\plugins\help\help.js:239:44)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

(yes, i tested in a /help command)
@Woffer12 ok, even without it it doesnt work tho oh we fetched guilds on ready but we kinda need it, is there no way around it? i dont think this was a good idea to do on ready:
const automod = await guild.autoModerationRules.fetch();

// for each automod, remove guild object
automod.forEach(async (rule) => {
rule.guild = null
})
const automod = await guild.autoModerationRules.fetch();

// for each automod, remove guild object
automod.forEach(async (rule) => {
rule.guild = null
})
facepalmz added this 3 months ago, pls no judging Pain for the help 😊
Want results from more Discord servers?
Add your server