Jack McCall
Jack McCall

Reputation: 143

How to get a discord bot to call its own commands

So I have a bot, and I want it to call commands that I've created from within the code. I tried e.Channel.SendMessage("!command"), where ! is the prefix and 'command' is the command

private newCommand()
{
    cmd.CreateCommand("command")
        .Do(async (e) =>
        {
        // Some command stuff
        }
}

So like, the command executes normally if I or someone else types !command, but how can I get the bot itself to call the command from within the code.

Like say for example, I have this code:

discord.MessageReceived += async (s, e) =>
        {
            if (!e.Message.IsAuthor && e.Message.User.Id == blah)
            {
                await e.Channel.SendMessage("Yo man");

                // how do I perform command??
                await e.Channel.SendMessage("!command"); // doesn't do anything
            }
        };

is there any way I can do this without having duplicate code from my command pasted into the MessageReceived section?

Upvotes: 0

Views: 3954

Answers (1)

Cory
Cory

Reputation: 1802

Expand the scope, aka, take the command code out of the anon function and define it in a higher scope so you can call it from both places.

private newCommand()
{
    cmd.CreateCommand("command")
        .Do(async (e) =>
        {
            ExecuteCommand();
        }
}
private void ExecuteCommand()
{
    // some command stuff
}

Then, call it from your other method:

discord.MessageReceived += async (s, e) =>
{
    if (!e.Message.IsAuthor && e.Message.User.Id == blah)
    {
        await e.Channel.SendMessage("Yo man");

        // how do I perform command??
        ExecuteCommand();
    }
};

Upvotes: 1

Related Questions