zeroclutch / gamebot

Add multiplayer games to Discord with Gamebot, and join thousands of players!
https://gamebot.gg
Other
70 stars 12 forks source link

Support for Better Command Handlers! #11

Closed ghost closed 3 years ago

ghost commented 3 years ago

Sup man! It's me Jason from SCU , COEN major '24. Glad to talk to u on discord ! On the topic of your bot, I'm beyond shocked at the amount of your hard work and dedication!!!! 15K GUILDS!!!!!!!!!!!! my god.......... i'm hardly in 30 guilds myself.

I see that this bot is using a pretty basic command handler in discord.js. Discord.js supports more multifunctional command handlers. The one this bot is using is also shown at discordjs.guide. This bot is pretty cool in terms of being a recreational fun bot but the tiny problem is when you integrate such a command handler, it's a bit clunky and a couple of commands might stop working.

This is normally discouraged as the more advanced command handler you have the easier code becomes for you. If this is actually a bot which is going to become a bot with 80-100 commands you would need a better command handler. Thus, this will eliminate the need for the fs handlers and the long code for getting commands in your bot.js file ++ your current code for prefixes/messages in the message.js event.

Currently your making an object which is getting exported. Now what if you had an object which was an instance of a class.

Here's how the bot.js file would look like for your case:

const { CommandoClient } = require("discord.js-commando"); 

 const client = new CommandoClient({
  commandPrefix: `${require("./options.json").prefix}`,
  owner: `${process.env.OWNER_ID}`,
  messageCacheLifetime: 120,
  messageSweepInterval: 10,
  messageCacheMaxSize: 50,
  disabledEvents: ['TYPING_START','MESSAGE_UPDATE', 'PRESENCE_UPDATE', 'GUILD_MEMBER_ADD', 
   'GUILD_MEMBER_REMOVE']
});

client.registry
  .registerDefaultTypes()
  .registerGroups([  
    ["dev", "Dev"],
    ["fun", "Fun"],
    ["economy", "Economy"],
    ["mod", "Mod"], 
    ["info", "Info"]
  ])
  .registerDefaultGroups()
  .registerDefaultCommands({
    unknownCommand: false,
    prefix: false, 
  })
  .registerCommandsIn(path.join(__dirname, "commands")); 

  // Add moderators
const moderators = process.env.MODERATORS
client.moderators = moderators ? moderators.split(',') : []

 client.dispatcher.addInhibitor( (client, msg) => {
  try { 
    if (msg.command.group.name === "Mod" || msg.command.group.name === "Dev") { 
        if (!msg.member.roles.cache.has(client.moderators)) || !msg.author.id === client.options.owner) {
          msg.channel.send({ embed: { description: { title: `***<@${msg.author.id}>, You don't have permission to use this command***`, description: msg}});
          msg.delete();
          return false;
        } else {
            return true;
        }
    } 
  } catch(err) {
      if (err === "TypeError: Inhibitor \"\" had an invalid result; must be a string or an Inhibition object.") {
        return;
      }
  }
}); 

Here's how your ping.js command for instance would look like , but Discord.js commando has premade ping, prefix, and help commands so it takes care of that :)

const { Command } = require("discord.js-commando"); 

module.exports = class pingCommand extends Command {
    constructor(client) {
        super(client, {
            name: "ping",
            description: "Checks ping status!",
            group: "utility",  
            memberName: "ping",
            throttling: {
                usages: 2,
                duration: 5,
            },
        });
    }

    async run(message) {
        // run code here :)
     }

Let me know your thoughts and I'll be down to make any further suggestions! Best on everything with midterms!

zeroclutch commented 3 years ago

Hey there!

An improved message handler has been somewhere on the to-do list, and adding a Command class and/or MessageHandler class is as well. The main reason that it is how it is right now is just because I was considerably worse at programming bots when I made it 2+ years ago. I ripped the code from my original bot Clutchbot (I won't link the source code because it's probably a mess, but it's on my Github somewhere). I'd rather not add the additional overhead that comes with Commando, considering the rest of the bot has already been built outside of that framework. However, adding a ratelimiter is a great idea for certain commands, and reducing the mess that is the bot.js file has been a priority. In general, improving the file structure has been a goal over time. On branch v3 there's an events folder that imports client 'message' events, and some refactoring in general. That's still WIP, though.

Current Priorities when it comes to improving commands

zeroclutch commented 3 years ago

The commit https://github.com/zeroclutch/gamebot/commit/1584710d8671793e1dddc703a392950df23c2a7f resolves this issue. Since branch v3 is still in development, I'll leave the issue open until it's moved to master.