luccanunes / discord.cpp

A Discord API wrapper for creating BOT's
MIT License
18 stars 1 forks source link

Commands and Event handlers #2

Open HideakiAtsuyo opened 3 years ago

HideakiAtsuyo commented 3 years ago

Is there anything missing or is everything there?

luccanunes commented 3 years ago

The only thing that's missing is the .lib file, that I'll be uploading later on today along with some documention on how to link it in your IDE. However, if you wish, you could compile it yourself. In Visual Studio you have to create a solution, add these files and change the solution properties to make it a library.

HideakiAtsuyo commented 3 years ago

Thank you for this quick response! Have a nice evening !

HideakiAtsuyo commented 3 years ago

I have one last question, being a beginner in C++ I would like to make a command handler, could you give me an example?

luccanunes commented 3 years ago

What do you mean by a command handler, could you elaborate?

HideakiAtsuyo commented 3 years ago

An example:

index.js:

client.commands = new Discord.Collection();
fs.readdir('./events/', (err, files) => {
  files = files.filter(f => f.endsWith('.js'));
  files.forEach(f => {
      const event = require(`./events/${f}`);
      client.on(f.split('.')[0], event.bind(null, client));
      delete require.cache[require.resolve(`./events/${f}`)];
  });
});

Event ready.js:

fs.readdir("./commands/", async (err, files) => {
          const filez = files.length
       if (err) return console.error(err);
            console.log(`${filez} commandes chargées!`)
        })

Event message.js:

const args = message.content.slice(prefix.length).trim().split(/ +/g);
            const command = args.shift().toLowerCase();
                try {
                    let commandesfichiers = require(`../commands/${command}.js`);
                    commandesfichiers.run(client, message, args);
                } catch (err) {
                        if (err instanceof Error && err.code === "NON_TROUVE") {
                            return;
                    } else
                        console.log(err)
                    }

Entier example of a bot with command handler

luccanunes commented 3 years ago

I'm currently working on a Command class, something like discord.py's @bot.command, however, I hadn't really though about creating one for Events, guess that would be nice as well.

About your example, C++ doesn't really support importing files within a loop or anything like that, since the #include statements are not processed at run-time and expect a file name, not a variable. So you would probably have to add all those imports yourself.

Also, the Client class currently doesn't have an on method to handle events, they all have their own method, like Client::onReady for example.

I am not aware of a way to get such result as nicely as you would with something like JavaScript or Python, but I will definitely try to add some features to make that easier.

As I mentioned, I am working on a Command class, which would look something like this:

#include <dpp.h>

int main
{
  dpp::Client client;
  // ... some code
  dpp::Command ping([&](dpp::Context ctx) {
    ctx.send("pong!"); // or ctxt.message.send("pong"), still haven't made my mind up xD
  });
  client.add_command(ping);
  client.run(TOKEN)
}

With this feature it would be a lot simpler to modulate the code, since you could use something like dirent.h or boost to list the command files and add them all to the client within a loop.

Sorry about the long comment, i hope this helps you.

HideakiAtsuyo commented 3 years ago

Yeah i see thanks ! I will follow the progress of the repository good night :)

luccanunes commented 3 years ago

Update: Now discord.cpp has a Command class!! Here's some quick example:

#include <dpp.h>

int main() 
{
  dpp::Client client;

  dpp::Command help("help", [](const dpp::Message& message) {
    message.reply("here's some help!");
  });

  client.add_command(help);

  client.run("BOT_TOKEN_HERE");

  return 0;
}

As you might have noticed:

Other note: You can still use Client::onMessage, and if you do, it'll be executed before any Command