pnp-scheduler/source/Discord/DiscordClient.ts
2025-06-18 22:53:54 +02:00

77 lines
No EOL
1.9 KiB
TypeScript

import {
Client,
GatewayIntentBits,
Events,
ActivityType, REST
} from "discord.js";
import Commands from "./Commands/Commands";
import {Container} from "../Container/Container";
import {Logger} from "log4js";
import {InteractionRouter} from "./InteractionRouter";
import {CommandDeployer} from "./CommandDeployer";
export class DiscordClient {
private readonly client: Client;
public get Client(): Client {
return this.client;
}
public get Commands(): Commands {
return this.router.commands
}
public get RESTClient(): REST {
return this.restClient;
}
public get ApplicationId(): string {
return this.applicationId;
}
constructor(
private readonly applicationId: string,
private readonly router: InteractionRouter,
private readonly deployer: CommandDeployer,
private readonly logger: Logger,
private readonly restClient: REST = new REST()
) {
this.client = new Client({
intents: [GatewayIntentBits.Guilds]
})
}
applyEvents() {
this.client.once(Events.ClientReady, () => {
if (!this.client.user) {
return;
}
this.logger.info(`Ready! Logged in as ${this.client.user.tag}`);
this.client.user.setActivity('your PnP playdates', {
type: ActivityType.Watching,
});
})
this.client.on(Events.GuildCreate, (guild) => {
this.logger.info(`Joined ${guild.name}`);
this.deployer.deployServer(guild.id);
})
this.client.on(Events.GuildDelete, (guild) => {
this.logger.info(`Left ${guild.name}`);
})
this.client.on(Events.GuildAvailable, (guild) => {
this.deployer.deployServer(guild.id);
})
this.client.on(Events.InteractionCreate, this.router.route.bind(this.router));
}
connect(token: string) {
this.client.login(token);
}
connectRESTClient(token: string) {
this.restClient.setToken(token);
}
}