Why my listener doesn't work?

Hello, i am trying to make a uptime checker thing. I'm currently just using a ready listener for test. main.ts
import 'dotenv/config';

import { SapphireClient } from '@sapphire/framework';
import { GatewayIntentBits } from 'discord.js';

// Logging
import { Logger, ILogObj } from "tslog";

const logger: Logger<ILogObj> = new Logger();

const client = new SapphireClient({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
],
loadMessageCommandListeners: true
});

const token: string | undefined = process.env.BOT_TOKEN;

async function run() {
if (!token) {
logger.fatal("Token is empty");
return;
}

try {
await client.login(token);
logger.info("Client logged in successfully");
} catch (error) {
logger.fatal("Failed to log in", error);
}
}

run().catch((error) => {
logger.fatal("Unexpected error occurred in run", error);
});
import 'dotenv/config';

import { SapphireClient } from '@sapphire/framework';
import { GatewayIntentBits } from 'discord.js';

// Logging
import { Logger, ILogObj } from "tslog";

const logger: Logger<ILogObj> = new Logger();

const client = new SapphireClient({
intents: [
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
],
loadMessageCommandListeners: true
});

const token: string | undefined = process.env.BOT_TOKEN;

async function run() {
if (!token) {
logger.fatal("Token is empty");
return;
}

try {
await client.login(token);
logger.info("Client logged in successfully");
} catch (error) {
logger.fatal("Failed to log in", error);
}
}

run().catch((error) => {
logger.fatal("Unexpected error occurred in run", error);
});
ready.ts
import { Listener } from '@sapphire/framework';
import type { Client, UserResolvable } from "discord.js";

export class ReadyListener extends Listener {
public constructor(context: Listener.LoaderContext, options: Listener.Options) {
super(context, {
...options,
once: true,
event: 'ready'
});
}

public async run(client: Client) {
const userID: UserResolvable = "snowflake-id";
try {
const user = await client.users.fetch(userID);
const presence = user.client.user.presence

console.log("Sapphire status: " + presence.status)
} catch (error) {
console.error("Error fetching user:", error);
}

}
}
import { Listener } from '@sapphire/framework';
import type { Client, UserResolvable } from "discord.js";

export class ReadyListener extends Listener {
public constructor(context: Listener.LoaderContext, options: Listener.Options) {
super(context, {
...options,
once: true,
event: 'ready'
});
}

public async run(client: Client) {
const userID: UserResolvable = "snowflake-id";
try {
const user = await client.users.fetch(userID);
const presence = user.client.user.presence

console.log("Sapphire status: " + presence.status)
} catch (error) {
console.error("Error fetching user:", error);
}

}
}
I'm sure that my client is logging up and ready (line 30 in main.ts gets executed)
Solution:
1. Which version of @sapphire/framework are you using? 2. What's your file/folder structure? 3. Did you use the CLI to generate your bot? 4. What's your main (CJS) or module (ESM) property in package.json 5. Are you using TypeScript? And if so, how are you compiling and running your code? That is to say, what are your build and startup scripts?...
Jump to solution
4 Replies
fenix
fenix2mo ago
I duplicated your code and it failed for me as well. I was unable to resolve it. Have you made the project with the Sapphire CLI? Or an existing project
grous
grousOP2mo ago
oh idk never seen Sapphire CLI in documentation so i just manually installed sapphire pnpm add @sapphire/framework [email protected]
Solution
Sapphire
Sapphire2mo ago
1. Which version of @sapphire/framework are you using? 2. What's your file/folder structure? 3. Did you use the CLI to generate your bot? 4. What's your main (CJS) or module (ESM) property in package.json 5. Are you using TypeScript? And if so, how are you compiling and running your code? That is to say, what are your build and startup scripts? - Did you remove your output folder and rebuild then try again? 6. Is your problem related to message commands? Did you add loadMessageCommandListeners to your SapphireClient options Remember that if you are new to @sapphire/framework it is important that you read the user guide.
Sapphire
Sapphire2mo ago
When using pnpm you have to make sure that you have the shamefully-hoist and public-hoist-pattern are set correctly in your .npmrc file. This is because pnpm will not hoist any dependencies by default and that poses a problem with how Sapphire works with module augmentation and loading files from the filesystem. Add this to your .npmrc file:
shamefully-hoist=true
public-hoist-pattern[]=@sapphire/*
shamefully-hoist=true
public-hoist-pattern[]=@sapphire/*

Did you find this page helpful?