Type '"Rules"' is not assignable to type 'PreconditionEntryResolvable'.

Type '"Rules"' is not assignable to type 'PreconditionEntryResolvable'. src/preconditions/Rules.ts:
export class RulesPrecondition extends Precondition {
public constructor(context: PieceContext, options: PieceOptions) {
super(context, {
...options,
name: "Rules",
});
}
public override async messageRun(message: Message) {
const res = await this.checkUser(message.author.id);
if (res) return this.ok();
return this.error({
message: "You must accept the rules to use this command.",
});
}

private async checkUser(userId: string) {
const exists = await this.container.client.db.has(`user_${userId}`);
return exists;
}
}
export class RulesPrecondition extends Precondition {
public constructor(context: PieceContext, options: PieceOptions) {
super(context, {
...options,
name: "Rules",
});
}
public override async messageRun(message: Message) {
const res = await this.checkUser(message.author.id);
if (res) return this.ok();
return this.error({
message: "You must accept the rules to use this command.",
});
}

private async checkUser(userId: string) {
const exists = await this.container.client.db.has(`user_${userId}`);
return exists;
}
}
types/index.d.ts:
declare module "@sapphire/framework" {
interface SapphireClient {
db: PetDatabase;
}
interface Preconditions {
Rules: never;
}
}
declare module "@sapphire/framework" {
interface SapphireClient {
db: PetDatabase;
}
interface Preconditions {
Rules: never;
}
}
src/commands/ping.ts:
export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: "ping",
description: "ping pong",
preconditions: ["Rules"],
});
}
public async messageRun(message: Message) {
return message.channel.send(
`:ping_pong: | Pong! ${this.container.client.ws.ping}ms`
);
}
}
export class PingCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: "ping",
description: "ping pong",
preconditions: ["Rules"],
});
}
public async messageRun(message: Message) {
return message.channel.send(
`:ping_pong: | Pong! ${this.container.client.ws.ping}ms`
);
}
}
discord.js v14.11.0 @sapphire/framework v4.4.4
8 Replies
Favna
Favna2y ago
add export default undefined to types/index.d.ts. then move the entire file to src/types/index.d.ts The former because otherwise it gets seen as a module declaration instead of a module augmentation by TypeScript The latter because it has to be within the scope of your includes path as defined by your tsconfig.json
h1dr4x
h1dr4xOP2y ago
i have edited my tsconfig.json to read types/index.d.ts too by adding it to
includes
includes
do i still have to move it? if so why? i might be asking a lot sorry, just want to learn im still an intermediate and also moving and adding export default undefined didn't solve the problem the other typing is working well
interface SapphireClient {
db: PetDatabase;
}
interface SapphireClient {
db: PetDatabase;
}
Favna
Favna2y ago
it does if you set rootDir to src as well because you cannot include files that are not in the rootDir. And if you dont set rootDir you'll get dist/src/<file>.ts structure
h1dr4x
h1dr4xOP2y ago
ah alright, thanks for the information, i did all of the steps you gave but it didnt work again
Favna
Favna2y ago
shrug. No idea then. Look at some other bots or send the full code:
Spinel
Spinel2y ago
Discord bots that use @sapphire/framework v4 - Official Bot Examples ᴱ ᴰ ᴶˢ - Gemboard ᴱ ᴰ - Dragonite ᴱ ᴰ - Radon ᴱ ᴬ - Sapphire Application Commands Examples ᴱ - Archangel ᴱ ᴰ - Zeyr ᴰ ᴬ - Birthdayy ᴰ Discord bots that use @sapphire/framework v3 - Arima ᴱ - Nino ᴱ ᴰ - Operator ᴱ ᴬ ᴰ - Spectera ᴬ Discord bots that use @sapphire/framework v2 - Materia ᴱ - RTByte ᴱ ᴬ - Skyra ᴬ ᴰ - YliasDiscordBot ᴬ : Uses ESM (if not specified then uses CJS) : Advanced bot (if not specified it is a simple bot, or not graded) : Uses Docker in production ᴶˢ: Written in JavaScript. If not specified then the bot is written in TypeScript.
Favna
Favna2y ago
also regarding the db, you dont have to but you can also just add it to the container so you save yourself 1 property level
this.container.db
this.container.client.db
this.container.db
this.container.client.db
setup.ts (or similar, called at init for example)
import { container } from '@sapphire/framework';

container.db = new DbConnection();

declare module '@sapphire/pieces' {
interface Container {
db: PetDatabase;
}
}
import { container } from '@sapphire/framework';

container.db = new DbConnection();

declare module '@sapphire/pieces' {
interface Container {
db: PetDatabase;
}
}
h1dr4x
h1dr4xOP2y ago
thanks!
Want results from more Discord servers?
Add your server