Missing access error

I'm trying to make a warn command, I don't know why it's saying missing access. The only thing the bot fetches is guild ID.
const { Events, Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder, EmbedBuilder, SelectMenuDefaultValueType, Embed } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const Warning = require('../../Schemas/warning');

module.exports = {
data: new SlashCommandBuilder()
.setName('warn')
.setDescription('Warn a user')
.addUserOption(option => option.setName('user').setDescription('The user to warn').setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('The reason for the warning').setRequired(true)),
async execute(interaction) {
const userId = interaction.options.getUser('user').id;
const reason = interaction.options.getString('reason');
const moderatorId = interaction.user.id;
const guildId = interaction.guildId;

let warning = await Warning.findOne({ userId, guildId });

if (!warning) {
warning = new Warning({
userId,
guildId,
warnings: []
});
}

warning.warnings.push({ moderatorId, reason });
await warning.save();

await interaction.reply(`User has been warned for: ${reason}`);
}
};
const { Events, Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, SlashCommandBuilder, EmbedBuilder, SelectMenuDefaultValueType, Embed } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const Warning = require('../../Schemas/warning');

module.exports = {
data: new SlashCommandBuilder()
.setName('warn')
.setDescription('Warn a user')
.addUserOption(option => option.setName('user').setDescription('The user to warn').setRequired(true))
.addStringOption(option => option.setName('reason').setDescription('The reason for the warning').setRequired(true)),
async execute(interaction) {
const userId = interaction.options.getUser('user').id;
const reason = interaction.options.getString('reason');
const moderatorId = interaction.user.id;
const guildId = interaction.guildId;

let warning = await Warning.findOne({ userId, guildId });

if (!warning) {
warning = new Warning({
userId,
guildId,
warnings: []
});
}

warning.warnings.push({ moderatorId, reason });
await warning.save();

await interaction.reply(`User has been warned for: ${reason}`);
}
};
12 Replies
d.js toolkit
d.js toolkit8mo 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!
Inky
Inky8mo ago
Can you show the full error?
$ JJ
$ JJOP8mo ago
Inky
Inky8mo ago
That’s a long stack trace
$ JJ
$ JJOP8mo ago
How can I make it shorter?
Inky
Inky8mo ago
I’ve never seen it in this format before Thought you were using a custom error handler or smth Do you have a try catch block in the interactionCreate event?
$ JJ
$ JJOP8mo ago
Nope. I can send you the InteractionCreate if you want.
Inky
Inky8mo ago
Put one around the .execute(…) And make sure to await the execute call And log the error like console.error(error) in the catch block
$ JJ
$ JJOP8mo ago
okay so i think i did what you said and now i got this
MongooseError: Operation `warnings.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\imjjb\Downloads\LSRP Management\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:23)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {stack: 'MongooseError: Operation `warnings.findOne()`…ss.processTimers (node:internal/timers:514:7)', message: 'Operation `warnings.findOne()` buffering timed out after 10000ms'}
MongooseError: Operation `warnings.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\imjjb\Downloads\LSRP Management\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:23)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {stack: 'MongooseError: Operation `warnings.findOne()`…ss.processTimers (node:internal/timers:514:7)', message: 'Operation `warnings.findOne()` buffering timed out after 10000ms'}
and i also got this one
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:730:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:835:23)
at async _REST.request (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:1278:22)
at async ChatInputCommandInteraction.reply (C:\Users\imjjb\Downloads\LSRP Management\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:114:5)
at async Object.execute (C:\Users\imjjb\Downloads\LSRP Management\src\events\interactionCreate.js:16:13) {requestBody: {…}, rawError: {…}, code: 10062, status: 404, method: 'POST', …}
DiscordAPIError[10062]: Unknown interaction
at handleErrors (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:730:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async BurstHandler.runRequest (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:835:23)
at async _REST.request (C:\Users\imjjb\Downloads\LSRP Management\node_modules\@discordjs\rest\dist\index.js:1278:22)
at async ChatInputCommandInteraction.reply (C:\Users\imjjb\Downloads\LSRP Management\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:114:5)
at async Object.execute (C:\Users\imjjb\Downloads\LSRP Management\src\events\interactionCreate.js:16:13) {requestBody: {…}, rawError: {…}, code: 10062, status: 404, method: 'POST', …}
this is my interaction create
$ JJ
$ JJOP8mo ago
Inky
Inky8mo ago
This one is either not calling connect beforehand or slow connection to db The former is more common You can’t reply to the interaction more than once Also can’t reply the interaction has been deferred
$ JJ
$ JJOP8mo ago
im not? i only have one interaction.reply

Did you find this page helpful?