From 6608bd6ee715163f55621f4c9c27d8a09ac3e222 Mon Sep 17 00:00:00 2001 From: mo Date: Thu, 18 Dec 2025 21:39:13 +0100 Subject: [PATCH] small update with changes i did in the last 6 months --- src/actions/dm/dm.service.ts | 10 +-- src/actions/medication/medication.task.ts | 4 +- src/config.ts | 8 ++- src/discord.controller.ts | 76 ++++++++++++++++++++--- 4 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/actions/dm/dm.service.ts b/src/actions/dm/dm.service.ts index 6cfba0e..1cc7c96 100644 --- a/src/actions/dm/dm.service.ts +++ b/src/actions/dm/dm.service.ts @@ -25,7 +25,7 @@ export class DmService { await client.users.send(member, content); } catch (error) { const channels = client.channels; - const channel = channels.cache.get(config.discord.channelIdNotification); + const channel = channels.cache.get(config.discord.channelIdLog); if (channel?.isTextBased() && channel?.isSendable()) { await channel.send(`konnte keine private nachricht an <@${member.user.id}> senden`); } @@ -39,7 +39,7 @@ export class DmService { await client.users.send(member, dmWelcomeContent); } catch (error) { const channels = client.channels; - const channel = channels.cache.get(config.discord.channelIdNotification); + const channel = channels.cache.get(config.discord.channelIdLog); if (channel?.isTextBased() && channel?.isSendable()) { await channel.send(`konnte keine private nachricht an <@${member.user.id}> senden`); } @@ -63,7 +63,7 @@ export class DmService { try { const channels = client.channels; - const channel = channels.cache.get(config.discord.channelIdNotification); + const channel = channels.cache.get(config.discord.channelIdLog); if (channel?.isTextBased() && channel?.isSendable()) { await channel.send(context); } @@ -79,7 +79,7 @@ export class DmService { await client.users.send(member, dmAcceptedContent); } catch (error) { const channels = client.channels; - const channel = channels.cache.get(config.discord.channelIdNotification); + const channel = channels.cache.get(config.discord.channelIdLog); if (channel?.isTextBased() && channel?.isSendable()) { await channel.send(`konnte keine private nachricht an <@${member.user.id}> senden`); } @@ -94,7 +94,7 @@ export class DmService { client.users.send(member, contentRoleMentionDm); } catch (error) { const channels = client.channels; - const channel = channels.cache.get(config.discord.channelIdNotification); + const channel = channels.cache.get(config.discord.channelIdLog); if (channel?.isTextBased() && channel?.isSendable()) { await channel.send(`konnte keine private nachricht an <@${member.user.id}> senden`); } diff --git a/src/actions/medication/medication.task.ts b/src/actions/medication/medication.task.ts index cff5e6f..51844df 100644 --- a/src/actions/medication/medication.task.ts +++ b/src/actions/medication/medication.task.ts @@ -3,7 +3,7 @@ import { MedicationService } from "actions/medication/medication.service"; const medicationService = new MedicationService(); -new CronJob( +/*new CronJob( "0 0 19 * * *", // cronTime async () => { console.log("askMedication()"); @@ -12,4 +12,4 @@ new CronJob( null, // onComplete true, // start "Europe/Berlin", // timeZone -); \ No newline at end of file +);*/ \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 5a1195b..14a7736 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,12 +1,13 @@ export default { discord: { - version: 250602.10, + version: 251218, // avocadi serverID: process.env.DISCORD_SERVER_ID || "", - // texxt channel + // text channel + channelIdBump: process.env.DISCORD_CHANNEL_ID_BUMP || "", channelIdBot: process.env.DISCORD_CHANNEL_ID_BOT || "", channelIdFeedback: process.env.DISCORD_CHANNEL_ID_FEEDBACK || "", - channelIdNotification: process.env.DISCORD_CHANNEL_ID_NOTIFICATION || "", + channelIdLog: process.env.DISCORD_CHANNEL_ID_NOTIFICATION || "", channelIdWelcome: process.env.DISCORD_CHANNEL_ID_WELCOME || "", channelIdRules: process.env.DISCORD_CHANNEL_ID_RULE || "", channelIdNews: process.env.DISCORD_CHANNEL_ID_NEWS || "", @@ -22,6 +23,7 @@ export default { vchannelIdPomodoro25: process.env.DISCORD_VCHANNEL_ID_POMODORO_25_5 || "", vchannelIdPomodoro50: process.env.DISCORD_VCHANNEL_ID_POMODORO_50_10 || "", // roles + roleBot: process.env.BOT || "", roleStudy: process.env.PEOPLE || "", roleMod: process.env.MOD || "", roleAdmin: process.env.ADMIN || "", diff --git a/src/discord.controller.ts b/src/discord.controller.ts index c37cb3a..f5af883 100644 --- a/src/discord.controller.ts +++ b/src/discord.controller.ts @@ -10,6 +10,8 @@ import { type ChatInputCommandInteraction, type Interaction, type ModalSubmitInteraction, + ActivityType, + type Channel, } from "discord.js"; import client from "lib/client"; import EventEmitter from "node:events"; @@ -70,10 +72,44 @@ export default class DiscordController extends EventEmitter { // log when running client.once("ready", async () => { - await this.setActivity(); + const channels = client.channels; + const logChannel = channels.cache.get(config.discord.channelIdLog); + + if (logChannel?.isTextBased() && logChannel?.isSendable()) { + try { + console.log("bot is online"); + await logChannel.send("wieder online!!!"); + } catch (error) { + console.error("failed to send online message:", error); + } + } else { + console.error("log channel is not valid or sendable."); + } + await this.setActivity(100); console.log("ready"); }); + process.on("exit", async () => { + const channels = client.channels; + const logChannel = channels.cache.get(config.discord.channelIdLog); + await this.handleShutdown(logChannel); + process.exit(0); + }); + + process.on("SIGINT", async () => { + const channels = client.channels; + const logChannel = channels.cache.get(config.discord.channelIdLog); + await this.handleShutdown(logChannel); + process.exit(0); + }); + + process.on("SIGTERM", async () => { + const channels = client.channels; + const logChannel = channels.cache.get(config.discord.channelIdLog); + await this.handleShutdown(logChannel); + process.exit(0); + }); + // listen for interactions client.on("interactionCreate", this.handleInteraction.bind(this)); @@ -144,14 +180,40 @@ export default class DiscordController extends EventEmitter { console.log(`----------------\nversion ${config.discord.version}\n----------------`); } - async setActivity() { - client.user?.setActivity(":3", { type: 4 }); - console.log("set activity"); - client.user?.setPresence({ - status: "online", - }); + async setActivity(state: number) { + switch (state) { + case 0: + client.user?.setActivity(" ", { type: 0 }); + console.log("set activity"); + client.user?.setPresence({ + status: "invisible", + }); + break; + default: + client.user?.setActivity("spielt sudoku", { type: 0 }); + console.log("set activity"); + client.user?.setPresence({ + status: "online", + }); + break; + } } + async handleShutdown(logChannel: Channel | undefined) { + if (logChannel?.isTextBased() && logChannel?.isSendable()) { + try { + await logChannel.send("bot is going offline..."); + } catch (error) { + console.error("failed to send offline message:", error); + } + } else { + console.error("log channel is not valid or sendable."); + } + await this.setActivity(0); + console.log("bot is offline."); + } + + async init() { await this.discordService.init(); }