add more listeners
add activity service add log channel service
This commit is contained in:
7
adapters/discord/src/actions/shutdown.ts
Normal file
7
adapters/discord/src/actions/shutdown.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { logChannelService } from "features/log-channel/log-channel.service";
|
||||
import { logger } from "lib/common-logger";
|
||||
|
||||
export const handleShutdown = async () => {
|
||||
logger.info("Bot is shutting down...");
|
||||
await logChannelService.sendLogMessage("Bot is shutting down...");
|
||||
};
|
||||
@@ -12,25 +12,6 @@ export default class DiscordController extends EventEmitter {
|
||||
super();
|
||||
let channelListeners = new Map();
|
||||
|
||||
// log when running
|
||||
client.once("ready", async () => {
|
||||
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);
|
||||
@@ -144,24 +125,6 @@ export default class DiscordController extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
async handleInteraction(interaction: Interaction<CacheType>) {
|
||||
if (interaction.isModalSubmit()) {
|
||||
await this.handleModalSubmit(interaction);
|
||||
|
||||
19
adapters/discord/src/features/activity/activity.schema.ts
Normal file
19
adapters/discord/src/features/activity/activity.schema.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import z from "zod";
|
||||
|
||||
export const Activities = z.enum([
|
||||
"playing",
|
||||
"streaming",
|
||||
"listening",
|
||||
"watching",
|
||||
"competing",
|
||||
"invisible",
|
||||
]);
|
||||
|
||||
export const ActivityLocales: Record<z.infer<typeof Activities>, string> = {
|
||||
playing: "spielt sudoku",
|
||||
streaming: "streamt sudoku",
|
||||
listening: "hört sudoku",
|
||||
watching: "schaut sudoku",
|
||||
competing: "wettstreitet sudoku",
|
||||
invisible: "versteckt sudoku",
|
||||
};
|
||||
27
adapters/discord/src/features/activity/activity.service.ts
Normal file
27
adapters/discord/src/features/activity/activity.service.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import client from "lib/client";
|
||||
import type z from "zod";
|
||||
import { type Activities, ActivityLocales } from "./activity.schema";
|
||||
|
||||
/**
|
||||
* Set the activity of the bot. This can be used to show that the bot is playing a game, listening to music, etc.
|
||||
*/
|
||||
export class ActivityService {
|
||||
async set(activity: z.output<typeof Activities>) {
|
||||
if (activity === "invisible") {
|
||||
client.user?.setPresence({
|
||||
status: "invisible",
|
||||
});
|
||||
client.user?.setActivity(" ", { type: 0 });
|
||||
return;
|
||||
}
|
||||
|
||||
client.user?.setActivity(ActivityLocales[activity], {
|
||||
type: 0,
|
||||
});
|
||||
client.user?.setPresence({
|
||||
status: "online",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const activityService = new ActivityService();
|
||||
@@ -0,0 +1,26 @@
|
||||
import { config } from "config";
|
||||
import client from "lib/client";
|
||||
import { logger } from "lib/common-logger";
|
||||
|
||||
export class LogChannelService {
|
||||
private logChannelId = config.channelMapping.text.bot;
|
||||
|
||||
async getLogChannel() {
|
||||
const logChannel = await client.channels.fetch(this.logChannelId);
|
||||
|
||||
if (logChannel?.isTextBased() && logChannel.isSendable()) {
|
||||
return logChannel;
|
||||
} else {
|
||||
logger.fatal("Log channel not found or is not text-based.");
|
||||
throw new Error("Log channel not found or is not text-based");
|
||||
}
|
||||
}
|
||||
|
||||
async sendLogMessage(message: string) {
|
||||
const logChannel = await this.getLogChannel();
|
||||
|
||||
await logChannel.send(message);
|
||||
}
|
||||
}
|
||||
|
||||
export const logChannelService = new LogChannelService();
|
||||
3
adapters/discord/src/lib/common-logger.ts
Normal file
3
adapters/discord/src/lib/common-logger.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { createLogger } from "@avocadi/bot-core/lib/logger";
|
||||
|
||||
export const logger = createLogger("DiscordAdapter");
|
||||
5
adapters/discord/src/listeners/index.ts
Normal file
5
adapters/discord/src/listeners/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import "./ready.listener";
|
||||
import "./guild-member-add.listener";
|
||||
import "./voice-state/voice-state.listener";
|
||||
import "./message-reaction-add.listener";
|
||||
import "./message-reaction-remove.listener";
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { ButtonInteraction } from "discord.js";
|
||||
|
||||
export const handleButtonInteraction = async (
|
||||
interaction: ButtonInteraction,
|
||||
) => {};
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { ChatInputCommandInteraction } from "discord.js";
|
||||
|
||||
export const handleChatInputCommandInteraction = async (
|
||||
interaction: ChatInputCommandInteraction,
|
||||
) => {};
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { ModalSubmitInteraction } from "discord.js";
|
||||
|
||||
export const handleModalSubmitInteraction = async (
|
||||
interaction: ModalSubmitInteraction,
|
||||
) => {};
|
||||
@@ -0,0 +1,20 @@
|
||||
import client from "lib/client";
|
||||
import { handleButtonInteraction } from "./handle-button";
|
||||
import { handleChatInputCommandInteraction } from "./handle-chat-input-command";
|
||||
import { handleModalSubmitInteraction } from "./handle-modal-submit";
|
||||
|
||||
client.on("interactionCreate", async (interaction) => {
|
||||
if (interaction.isModalSubmit()) {
|
||||
await handleModalSubmitInteraction(interaction);
|
||||
return;
|
||||
}
|
||||
|
||||
if (interaction.isChatInputCommand()) {
|
||||
await handleChatInputCommandInteraction(interaction);
|
||||
return;
|
||||
}
|
||||
if (interaction.isButton()) {
|
||||
await handleButtonInteraction(interaction);
|
||||
return;
|
||||
}
|
||||
});
|
||||
22
adapters/discord/src/listeners/ready.listener.ts
Normal file
22
adapters/discord/src/listeners/ready.listener.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { config } from "config";
|
||||
import { activityService } from "features/activity/activity.service";
|
||||
import client from "lib/client";
|
||||
import { logger } from "lib/common-logger";
|
||||
|
||||
client.once("ready", async () => {
|
||||
const channels = client.channels;
|
||||
const logChannel = channels.cache.get(config.channelMapping.text.bot);
|
||||
|
||||
if (logChannel?.isTextBased() && logChannel?.isSendable()) {
|
||||
try {
|
||||
logger.info("bot is online");
|
||||
await logChannel.send("wieder online!!!");
|
||||
} catch (error) {
|
||||
logger.error("failed to send online message:", error);
|
||||
}
|
||||
} else {
|
||||
logger.error("log channel is not valid or sendable.");
|
||||
}
|
||||
await activityService.set("playing");
|
||||
logger.info("ready");
|
||||
});
|
||||
15
adapters/discord/src/listeners/stop.listener.ts
Normal file
15
adapters/discord/src/listeners/stop.listener.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { handleShutdown } from "actions/shutdown";
|
||||
|
||||
process.on("exit", async () => {
|
||||
await handleShutdown();
|
||||
});
|
||||
|
||||
process.on("SIGINT", async () => {
|
||||
await handleShutdown();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on("SIGTERM", async () => {
|
||||
await handleShutdown();
|
||||
process.exit(0);
|
||||
});
|
||||
Reference in New Issue
Block a user