grous
grous
SIASapphire - Imagine a framework
Created by grous on 11/29/2024 in #sapphire-support
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)
9 replies