134 lines
4.6 KiB
TypeScript
134 lines
4.6 KiB
TypeScript
import config from "config";
|
|
import client from "lib/client";
|
|
import { getRandomInt } from "lib/utils";
|
|
import { customContent, createEmbed } 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 {
|
|
async handleInteraction(interaction: Interaction<CacheType>) {
|
|
if (interaction.isChatInputCommand()) {
|
|
await this.handleChatInputCommand(interaction);
|
|
return;
|
|
}
|
|
}
|
|
|
|
async handleChatInputCommand(
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
) {
|
|
console.log("accept");
|
|
const commandName = interaction.commandName as CommandsType;
|
|
switch (commandName) {
|
|
case Commands.Enum.embed:
|
|
await this.customEmbed(interaction);
|
|
return;
|
|
case Commands.Enum.message:
|
|
await this.customMessage(interaction);
|
|
return;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
async checkPermission(interaction: ChatInputCommandInteraction<CacheType>) {
|
|
const userIdCommand = interaction.user.id;
|
|
if (userIdCommand !== config.discord.myId) {
|
|
await interaction.reply({
|
|
content: "you have no permission for that command",
|
|
ephemeral: true,
|
|
});
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// check if command done in server
|
|
async checkIfServer(interaction: ChatInputCommandInteraction<CacheType>) {
|
|
const guild = interaction.guild;
|
|
if (!guild) {
|
|
await interaction.reply({
|
|
content: "command can only be used on a server",
|
|
ephemeral: true,
|
|
});
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
async customEmbed(interaction: ChatInputCommandInteraction<CacheType>) {
|
|
const title = interaction.options.getString("title") || " ";
|
|
const description = interaction.options.getString("description") || " ";
|
|
const timestamp = interaction.options.getBoolean("timestamp") || false;
|
|
// return the value
|
|
console.log(title, description, timestamp);
|
|
|
|
// permission check
|
|
// permission check
|
|
if (await this.checkPermission(interaction) && await this.checkIfServer(interaction))
|
|
try {
|
|
const channels = client.channels;
|
|
const channel = channels.cache.get(interaction.channelId);
|
|
|
|
if (channel?.isTextBased() && channel?.isSendable()) {
|
|
await channel.send({
|
|
embeds: [createEmbed(title, description, timestamp)],
|
|
});
|
|
}
|
|
await interaction.reply({
|
|
content: "successfully created embed",
|
|
ephemeral: true,
|
|
});
|
|
} catch (error) {
|
|
console.error("error while creating embed:", error);
|
|
await interaction.reply({
|
|
content:
|
|
"error while creating embed",
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
async customMessage(interaction: ChatInputCommandInteraction<CacheType>) {
|
|
const input: string = interaction.options.getString("input") || "";
|
|
const result = input.replaceAll(";", "\n");
|
|
|
|
// return the value
|
|
console.log(input);
|
|
|
|
// permission check && server check
|
|
if (await this.checkPermission(interaction) && await this.checkIfServer(interaction))
|
|
try {
|
|
const channels = client.channels;
|
|
const channel = channels.cache.get(interaction.channelId);
|
|
|
|
if (channel?.isTextBased() && channel?.isSendable()) {
|
|
await channel.send({
|
|
content: result,
|
|
});
|
|
}
|
|
await interaction.reply({
|
|
content: "successfully created message",
|
|
ephemeral: true,
|
|
});
|
|
} catch (error) {
|
|
console.error("error while creating message:", error);
|
|
await interaction.reply({
|
|
content:
|
|
"error while creating message",
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
}
|
|
}
|