Precondition error

Precondition:
const { Precondition } = require("@sapphire/framework");

class DMPrecondition extends Precondition {
constructor(context, options) {
super(context, {
...options,
position: 3,
});
}

async contextMenuRun(interaction) {
if (!interaction.guild)
return interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

if (interaction.guild) {
return this.ok();
} else {
console.log(this.error());
return this.error();
}
}
async messageRun() {
this.ok();
}
}

module.exports = {
DMPrecondition,
};
const { Precondition } = require("@sapphire/framework");

class DMPrecondition extends Precondition {
constructor(context, options) {
super(context, {
...options,
position: 3,
});
}

async contextMenuRun(interaction) {
if (!interaction.guild)
return interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

if (interaction.guild) {
return this.ok();
} else {
console.log(this.error());
return this.error();
}
}
async messageRun() {
this.ok();
}
}

module.exports = {
DMPrecondition,
};
Error: https://pastebin.com/7210hC27
Pastebin
Error - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
23 Replies
Lioness100
Lioness100•9mo ago
messageRun needs to return this.ok(), not just call it (would be caught by ts evilpatrick
-Carlos👑
-Carlos👑•9mo ago
still returns the error
Lioness100
Lioness100•9mo ago
if (!interaction.guild) return interaction.reply({ content: "You cannot use this on a DM.", ephemeral: true, });
This path also doesn't return this.ok() or this.error()
-Carlos👑
-Carlos👑•9mo ago
wdym
Lioness100
Lioness100•9mo ago
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}
All paths have to return this.ok() or this.error()
-Carlos👑
-Carlos👑•9mo ago
ah
Lioness100
Lioness100•9mo ago
I would suggest you leave the sending an error message to the *CommandDenied event though so you could just do this.error({ message: "You cannot..." }) in all your preconditions, and then the event would take the message and send the error
-Carlos👑
-Carlos👑•9mo ago
alright
now it lets me use message commands in dms

const { Precondition } = require("@sapphire/framework");

class DMPrecondition extends Precondition {
constructor(context, options) {
super(context, {
...options,
position: 3,
});
}

async contextMenuRun(interaction) {
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

if (interaction.guild) {
return this.ok();
} else {
return this.error();
}
}
async messageRun() {
return this.some();
}
}

module.exports = {
DMPrecondition,
};
now it lets me use message commands in dms

const { Precondition } = require("@sapphire/framework");

class DMPrecondition extends Precondition {
constructor(context, options) {
super(context, {
...options,
position: 3,
});
}

async contextMenuRun(interaction) {
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

if (interaction.guild) {
return this.ok();
} else {
return this.error();
}
}
async messageRun() {
return this.some();
}
}

module.exports = {
DMPrecondition,
};
Lioness100
Lioness100•9mo ago
messageRun should return ok(), not some(). Use your text editor's type hints
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

if (interaction.guild) {
return this.ok();
} else {
return this.error();
}
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

if (interaction.guild) {
return this.ok();
} else {
return this.error();
}
What is this 😭 - if no guild - return ... - else, if guild - return ... - else, if no guild - return ... That could just be
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

return this.ok();
if (!interaction.guild) {
await interaction.reply({
content: "You cannot use this on a DM.",
ephemeral: true,
});

return this.error();
}

return this.ok();
Anyways, check what's happening in contextMenuDenied, make sure it's firing, log interaction.guild to see if it's what you expect it to be... take some steps to debug on your own
-Carlos👑
-Carlos👑•9mo ago
it still lets me run message commands on dms which is weird since I have a global precondition (position 2) where it checks if the command is being ran in a guild or not
Lioness100
Lioness100•9mo ago
Yes... if your messageCommand method returns this.ok() then it will pass why do you have 2 dm preconditions
-Carlos👑
-Carlos👑•9mo ago
wdym 2 the other one I was talking about also checks for certain channels ids
Lioness100
Lioness100•9mo ago
Ok Could you send your updated code?
-Carlos👑
-Carlos👑•9mo ago
looks like i fixed it, just had to return this.error() on messageRun
Lioness100
Lioness100•9mo ago
Yes. But I assume you added in functionality to check if there was a guild and didn't have it return this.error() always?
-Carlos👑
-Carlos👑•9mo ago
wdym nvm it now doesn't let me run commands on guilds anymore
Lioness100
Lioness100•9mo ago
lol as I said here, you need to add the same functionality in messageRun that you did in contextMenuRun
-Carlos👑
-Carlos👑•9mo ago
added return !message.guild ? this.error() : this.ok(); and it works
Lioness100
Lioness100•9mo ago
Right, but note you're not sending the same error message as you are in the contextMenu function. I suggest consistency. (also nit but it's generally a good idea for readability to not make the condition a negative if there's an "else", example:
- return !message.guild ? this.error() : this.ok();
+ return message.guild ? this.ok() : this.error();
- return !message.guild ? this.error() : this.ok();
+ return message.guild ? this.ok() : this.error();
-Carlos👑
-Carlos👑•9mo ago
alright thanks idk which message to put as an answer
Lioness100
Lioness100•9mo ago
42 ^ do that one /j
-Carlos👑
-Carlos👑•9mo ago
no
-Carlos👑
-Carlos👑•9mo ago
just gonna put that