This commit is contained in:
mo
2026-03-09 07:48:08 +01:00
parent 5271665963
commit dfb8b2781d
6 changed files with 191 additions and 127 deletions

View File

@@ -1,12 +1,70 @@
import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema";
import { config } from "config"; import { config } from "config";
import type { Message } from "discord.js";
import { messagesService } from "entities/messages/messages.service"; import { messagesService } from "entities/messages/messages.service";
import client from "lib/client"; import client from "lib/client";
import { logger } from "lib/common-logger";
client.on("messageCreate", async (message) => { client.on("messageCreate", async (message) => {
if ( if (message.content?.startsWith(config.commandPrefix) && message.channel) {
message.channel.id !== config.channelMapping.text.log && if (!resolveModContext(message)) return;
message.channel.id !== config.channelMapping.text.bot
) { await messagesService.logMessage(message);
messagesService.logMessage(message); logger.info(
`Command received: ${message.content} from user ${message.author.id}`,
);
const commandWithoutPrefix = message.content
.slice(config.commandPrefix.length)
.trim();
const commandParts = commandWithoutPrefix.split(" ");
if (commandParts.length === 0) {
logger.warn("No command found after prefix.");
return;
}
const [command, ...parameters] = commandParts;
logger.info(`Parsed command: ${command}`);
const result = CommandKeys.safeParse(command);
if (result.success) {
logger.info(`Command ${command} recognized.`);
await handleCommand(result.data, parameters, message.channel.id);
}
} }
}); });
async function resolveModContext(message: Message) {
const guild = message.guild;
if (!guild) {
logger.error(`Guild not found for message ${message.id}.`);
return;
}
const roleMod = guild.roles.cache.get(config.roleMapping.mod);
// const roleMod = guild.roles.cache.find(
// (r) => r.name === config.roleMapping.mod,
// );
if (!roleMod) {
logger.error(`Role ${roleMod} not found in guild ${guild.name}.`);
return;
}
const member = await guild.members.fetch(message.author.id);
if (!member) {
logger.error(
`User with ID ${message.author.id} not found in guild ${guild.name}.`,
);
throw new Error("User not found");
}
if (!member.roles.cache.has(roleMod.id)) {
return;
}
throw new Error("Function not implemented.");
}
function handleCommand(data: string, parameters: string[], id: string) {
throw new Error("Function not implemented.");
}

View File

@@ -20,7 +20,7 @@ export const handleDynamicVoiceChannelCreation = async (
// New channel is created if user joined one of the specific channels (e.g. "Join to Create") or switched to one of them // New channel is created if user joined one of the specific channels (e.g. "Join to Create") or switched to one of them
const newChannel = const newChannel =
await dynamicVoiceChannelService.createDynamicVoiceChannel(channel.id); await dynamicVoiceChannelService.createDynamicVoiceChannel(channel.id);
logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`);
if (newChannel) { if (newChannel) {
await newState.setChannel(newChannel); await newState.setChannel(newChannel);
} }

View File

@@ -13,6 +13,7 @@ export const handleDynamicVoiceChannelDeletion = async (
if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) { if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) {
const channel = await voiceChannelsService.getVoiceChannelById(channelId); const channel = await voiceChannelsService.getVoiceChannelById(channelId);
if (!channel) return;
if (channel.members.size === 0) { if (channel.members.size === 0) {
await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId); await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId);
} }

View File

@@ -6,7 +6,12 @@ import client from "lib/client";
import { logger } from "lib/common-logger"; import { logger } from "lib/common-logger";
import { handleCommand } from "./handle-command"; import { handleCommand } from "./handle-command";
// https://fluxerjs.blstmo.com/v/latest/docs/classes/DMChannel
client.on(Events.MessageCreate, async (message: Message) => { client.on(Events.MessageCreate, async (message: Message) => {
const messageAuthor = message.author;
const messageReceiver = message.type;
logger.info("got msg");
if (message.content?.startsWith(config.commandPrefix) && message.channel) { if (message.content?.startsWith(config.commandPrefix) && message.channel) {
await messagesService.logMessage(message); await messagesService.logMessage(message);
logger.info( logger.info(

View File

@@ -1,133 +1,137 @@
import { Commands, type CommandsType } from "commands/index.ts";
import config from "config"; import config from "config";
import {
type CacheType,
ChannelType,
type ChatInputCommandInteraction,
Client,
EmbedBuilder,
type GuildMember,
type Interaction,
type Message,
type OmitPartialGroupDMChannel,
} from "discord.js";
import { time } from "drizzle-orm/mysql-core";
import client from "lib/client"; import client from "lib/client";
import { getRandomInt } from "lib/utils"; import { getRandomInt } from "lib/utils";
import { customContent, createEmbed } from "./customMessage.components.ts"; import { createEmbed, customContent } from "./customMessage.components.ts";
import {
Client,
EmbedBuilder,
type Message,
type CacheType,
type GuildMember,
type Interaction,
type OmitPartialGroupDMChannel,
type ChatInputCommandInteraction,
ChannelType,
} from "discord.js";
import { type CommandsType, Commands } from "commands/index.ts";
import { time } from "drizzle-orm/mysql-core";
export class CustomMessageService { export class CustomMessageService {
async handleInteraction(interaction: Interaction<CacheType>) { async handleInteraction(interaction: Interaction<CacheType>) {
if (interaction.isChatInputCommand()) { if (interaction.isChatInputCommand()) {
await this.handleChatInputCommand(interaction); await this.handleChatInputCommand(interaction);
return; return;
} }
} }
async handleChatInputCommand( async handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>, interaction: ChatInputCommandInteraction<CacheType>,
) { ) {
console.log("accept"); console.log("accept");
const commandName = interaction.commandName as CommandsType; const commandName = interaction.commandName as CommandsType;
switch (commandName) { switch (commandName) {
case Commands.Enum.embed: case Commands.Enum.embed:
await this.customEmbed(interaction); await this.customEmbed(interaction);
return; return;
case Commands.Enum.message: case Commands.Enum.message:
await this.customMessage(interaction); await this.customMessage(interaction);
return; return;
default: default:
break; break;
} }
} }
async checkPermission(interaction: ChatInputCommandInteraction<CacheType>) { async checkPermission(interaction: ChatInputCommandInteraction<CacheType>) {
const userIdCommand = interaction.user.id; const userIdCommand = interaction.user.id;
if (userIdCommand !== config.discord.myId) { if (userIdCommand !== config.discord.myId) {
await interaction.reply({ await interaction.reply({
content: "you have no permission for that command", content: "you have no permission for that command",
ephemeral: true, ephemeral: true,
}); });
return false; return false;
} }
return true; return true;
} }
// check if command done in server // check if command done in server
async checkIfServer(interaction: ChatInputCommandInteraction<CacheType>) { async checkIfServer(interaction: ChatInputCommandInteraction<CacheType>) {
const guild = interaction.guild; const guild = interaction.guild;
if (!guild) { if (!guild) {
await interaction.reply({ await interaction.reply({
content: "command can only be used on a server", content: "command can only be used on a server",
ephemeral: true, ephemeral: true,
}); });
return false; return false;
} }
return true; return true;
} }
async customEmbed(interaction: ChatInputCommandInteraction<CacheType>) { async customEmbed(interaction: ChatInputCommandInteraction<CacheType>) {
const title = interaction.options.getString("title") || " "; const title = interaction.options.getString("title") || " ";
const description = interaction.options.getString("description") || " "; const description = interaction.options.getString("description") || " ";
const timestamp = interaction.options.getBoolean("timestamp") || false; const timestamp = interaction.options.getBoolean("timestamp") || false;
// return the value // return the value
console.log(title, description, timestamp); console.log(title, description, timestamp);
// permission check // permission check
// permission check // permission check
if (await this.checkPermission(interaction) && await this.checkIfServer(interaction)) if (
try { (await this.checkPermission(interaction)) &&
const channels = client.channels; (await this.checkIfServer(interaction))
const channel = channels.cache.get(interaction.channelId); )
try {
const channels = client.channels;
const channel = channels.cache.get(interaction.channelId);
if (channel?.isTextBased() && channel?.isSendable()) { if (channel?.isTextBased() && channel?.isSendable()) {
await channel.send({ await channel.send({
embeds: [createEmbed(title, description, timestamp)], embeds: [createEmbed(title, description, timestamp)],
}); });
} }
await interaction.reply({ await interaction.reply({
content: "successfully created embed", content: "successfully created embed",
ephemeral: true, ephemeral: true,
}); });
} catch (error) { } catch (error) {
console.error("error while creating embed:", error); console.error("error while creating embed:", error);
await interaction.reply({ await interaction.reply({
content: content: "error while creating embed",
"error while creating embed", ephemeral: true,
ephemeral: true, });
}); }
} }
}
async customMessage(interaction: ChatInputCommandInteraction<CacheType>) { async customMessage(interaction: ChatInputCommandInteraction<CacheType>) {
const input: string = interaction.options.getString("input") || ""; const input: string = interaction.options.getString("input") || "";
const result = input.replaceAll(";", "\n"); const result = input.replaceAll(";", "\n");
// return the value // return the value
console.log(input); console.log(input);
// permission check && server check // permission check && server check
if (await this.checkPermission(interaction) && await this.checkIfServer(interaction)) if (
try { (await this.checkPermission(interaction)) &&
const channels = client.channels; (await this.checkIfServer(interaction))
const channel = channels.cache.get(interaction.channelId); )
try {
const channels = client.channels;
const channel = channels.cache.get(interaction.channelId);
if (channel?.isTextBased() && channel?.isSendable()) { if (channel?.isTextBased() && channel?.isSendable()) {
await channel.send({ await channel.send({
content: result, content: result,
}); });
} }
await interaction.reply({ await interaction.reply({
content: "successfully created message", content: "successfully created message",
ephemeral: true, ephemeral: true,
}); });
} catch (error) { } catch (error) {
console.error("error while creating message:", error); console.error("error while creating message:", error);
await interaction.reply({ await interaction.reply({
content: content: "error while creating message",
"error while creating message", ephemeral: true,
ephemeral: true, });
}); }
} }
}
} }

View File

@@ -22,10 +22,6 @@ export class AcceptUserService<U extends BaseUser = BaseUser> {
} }
async sendDmAcceptUser(user: U) { async sendDmAcceptUser(user: U) {
this.logger.info("test");
this.logger.info(`accepting ${user.id}...`);
const acceptUserMessage: string = await this.i18nService.t( const acceptUserMessage: string = await this.i18nService.t(
"dmAcceptUser", "dmAcceptUser",
this.lang, this.lang,