From 16b56302ba7b05d2315442b71ff065ac15dfd962 Mon Sep 17 00:00:00 2001 From: entar Date: Sun, 18 May 2025 12:33:20 +0700 Subject: [PATCH] Better command parsing --- commands/base.js | 29 +++++++++++++++++++++++++++-- commands/extra.js | 17 +++++++++++++++++ index.js | 31 +++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 commands/extra.js diff --git a/commands/base.js b/commands/base.js index c83e5ff..2c2474e 100644 --- a/commands/base.js +++ b/commands/base.js @@ -1,4 +1,5 @@ -const client = require("../index") +const {client, commands} = require("../index") +const { EmbedBuilder, Message, Colors } = require("discord.js") module.exports = [ { @@ -6,6 +7,30 @@ module.exports = [ run: (ctx, args) => { console.log(ctx, args) ctx.reply(`Pong! ${args}`) - } + }, + hide: false, + arguments: ["any"], + description: "Check how fast bot will reply!" + }, + { + name: "help", + run: (ctx, args) => { + const embed = new EmbedBuilder() + .setAuthor({name: ctx.author.username, iconURL: ctx.author.avatarURL(), url: "https://git.squog.ru/entar/SquogAdmin"}) + .setColor(Colors.White) + + commands.forEach(commandList => { + commandList.forEach(command => { + if (command.hide) { + return // Go to next command if this one should show up + } + const field = {name: command.name, value: `Description: ${command.description || "None"}\nArguments: ${command.arguments || "None"}`} + console.log(field) + embed.addFields(field) + }) + }) + ctx.reply({embeds: [embed]}) + }, + hide: true, } ] \ No newline at end of file diff --git a/commands/extra.js b/commands/extra.js new file mode 100644 index 0000000..e9aa2bc --- /dev/null +++ b/commands/extra.js @@ -0,0 +1,17 @@ +const {client} = require("../index") +const { GuildMember, GuildChannel } = require("discord.js") + +module.exports = [ + { + name: "helptest", + description: "Kill every1", + arguments: [GuildMember.name, GuildChannel.name], + run: (ctx, args) => { + if (!args[0].user) { + ctx.reply("Member invalid.") + } + + ctx.reply(`Member: ${args[0]}\nChannel: ${args[1]}`) + } + } +] \ No newline at end of file diff --git a/index.js b/index.js index 25d3ff8..6a42041 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,8 @@ const client = new Client({ ) // Exporting the client for event and command files to use it -module.exports = {client: client, dir: __dirname} +module.exports.client = client +module.exports.dir = __dirname // Requiring all the event files. fs.readdir(`${__dirname}/events/`, (err, files) => { @@ -61,7 +62,7 @@ function getCommand(commandName) { } // Parsing commands on message create -client.on("messageCreate", async (message) => { +client.on("messageCreate", (message) => { var content = message.content // Continue only if starts with the prefix @@ -81,6 +82,30 @@ client.on("messageCreate", async (message) => { // Removing command from the args const args = parsed.filter(value => {return value != command}) + // Parsing + args.forEach((arg, index) => { + console.log(index) + if (arg.startsWith("<@")) { + // User, lets get the id + const id = arg.slice(2, arg.length - 1) + + const member = message.guild.members.cache.find(member => member.user.id == id) + + if (member) { + args[index] = member + } + } else if (arg.startsWith("<#")) { + // Channel, lets get the id + const id = arg.slice(2, arg.length - 1) + + const channel = message.guild.channels.cache.find(channel => channel.id == id) + + if (channel) { + args[index] = channel + } + } + }) + // Actual command const realCommand = getCommand(command) @@ -93,5 +118,7 @@ client.on("messageCreate", async (message) => { realCommand.run(message, args) }) +module.exports.commands = commands + // Time to login client.login(token) \ No newline at end of file