From 95d7ea1527d92932b67552a6b3583056df85e2ae Mon Sep 17 00:00:00 2001 From: The-Foxon Date: Wed, 28 Jun 2023 04:12:46 +1200 Subject: [PATCH] add Exec command --- src/commands/Alert.ts | 3 +-- src/commands/Exec.ts | 42 ++++++++++++++++++++++++++++++++++++ src/commands/index.ts | 2 ++ src/commands/syncUsers.ts | 3 +-- src/controller/EcoManager.ts | 6 +++++- 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/commands/Exec.ts diff --git a/src/commands/Alert.ts b/src/commands/Alert.ts index 774f441..c451fe9 100644 --- a/src/commands/Alert.ts +++ b/src/commands/Alert.ts @@ -16,8 +16,7 @@ export default { const message = interaction.options.getString('message') - if (!interaction.deferred) - await interaction.deferReply({ ephemeral: true }) + await interaction.deferReply({ ephemeral: true }) try { await interaction.followUp(`Отправка уведомления`); diff --git a/src/commands/Exec.ts b/src/commands/Exec.ts new file mode 100644 index 0000000..abee3cf --- /dev/null +++ b/src/commands/Exec.ts @@ -0,0 +1,42 @@ +import { CacheType, ChatInputCommandInteraction, Message, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; +import ConfigManager from '../controller/ConfigManager'; +import EcoManager from '../controller/EcoManager'; + + +// const adminPermissions = new PermissionsBitField('Administrator'); + +export default { + data: new SlashCommandBuilder() + .setName('exec') + .setDescription('Exec command') + .addStringOption(opt => opt.setName("command").setDescription("Command").setRequired(true)) + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), + async execute(interaction: ChatInputCommandInteraction, config: ConfigManager) { + const ecoManager = new EcoManager(config) + + const command = interaction.options.getString('command') + + await interaction.deferReply({ ephemeral: true }) + + try { + if (!command) { + await interaction.followUp(`Ошибка выполнения команды.`); + return + } + + const status = await ecoManager.Exec(command) + if (!!status && !status.Errored) { + if (status.CommandMessages.filter(e => e.Type === "Notifications-Chat").length > 0) { + await interaction.followUp(status.CommandMessages.filter(e => e.Type === "Notifications-Chat")[0].Message) + } else + await interaction.followUp("Команда выполнена.\n" + JSON.stringify(status)) + } else { + await interaction.followUp("Ошибка выполнения команды.\n" + JSON.stringify(status)) + } + return + } catch (error) { + await interaction.followUp("Ошибка выполнения команды.") + console.log(error) + } + }, +}; diff --git a/src/commands/index.ts b/src/commands/index.ts index 7a743a1..92297f3 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -2,11 +2,13 @@ import { Collection } from 'discord.js' import syncUsers from './syncUsers' import Alert from './Alert' import Message from './Message' +import Exec from './Exec' const commands = new Collection commands.set('sync_users', syncUsers) commands.set('alert', Alert) commands.set('message', Message) +commands.set('exec', Exec) export default commands diff --git a/src/commands/syncUsers.ts b/src/commands/syncUsers.ts index bc6ecf0..c65f806 100644 --- a/src/commands/syncUsers.ts +++ b/src/commands/syncUsers.ts @@ -13,8 +13,7 @@ export default { async execute(interaction: ChatInputCommandInteraction, config: ConfigManager) { const userManager = new UserManager(config) - if (!interaction.deferred) - await interaction.deferReply({ ephemeral: true }) + await interaction.deferReply({ ephemeral: true }) try { await interaction.followUp(`Начало синхронизации`); diff --git a/src/controller/EcoManager.ts b/src/controller/EcoManager.ts index b942afd..ca91034 100644 --- a/src/controller/EcoManager.ts +++ b/src/controller/EcoManager.ts @@ -12,6 +12,10 @@ export default class EcoManager { } public async Alert(message: string) { if (!message) return null + return await this.Exec(`manage alert ${message}`) + } + public async Exec(command: string) { + if (!command) return null try { const { data, status } = await axios(`${process.env.ECO_API_BASE || "https://eco.kamgames.xyz"}/api/v1/command/exec`, { method: "POST", @@ -20,7 +24,7 @@ export default class EcoManager { "X-API-Key": process.env.ECO_API_KEY, }, data: { - Command: `manage alert ${message}` + Command: command }, });