Search code examples
discorddiscord.jsdiscord.pybots

How to invoke a discord bot command via rest api?


I want to invoke the discord bot command with an HTTP API call.

Sample command: /command parameter

How can I do this?

I sent a message via discord API into chat but the bot can’t recognise it as a command. It was posted as regular text.

CURL Example (You can import into postman) change CHANNEL_ID and AUTH_TOKEN accordingly:

curl 'https://discord.com/api/v9/channels/______CHANNEL_ID_____/messages' \
  -H 'authorization: ______AUTH_TOKEN_____' \
  -H 'content-type: application/json' \
  --data-raw '{"content":"/command parameter"}' \
  --compressed

Solution

  • 1.Login to Discord and send a command to the right bot For example: /imagine prompt: SuperHero.

    2.Look at the contents of the request in Fiddler (well, or in the browser)

    3.Send the same request in the future. Profit

     static string PassPromptToSelfBot(string prompt)
     {
        WebClient client = new WebClient();
        client.Headers.Add("Authorization", "qwertyqwertyqwerty-{U SECRET KEY}");
        client.Headers.Add("Content-Type", "application/json");
    
        var payload = new
        {
            type = 2,
            application_id = "936929561302675456", // U AppId
            guild_id = "1088230970144063569", // U guild_id etc...
            channel_id = "1088230970655776809",
            session_id = "836d48312b8a69900150ab05cdb0ec78",
            data = new
            {
                version = "1077969938624553050",
                id = "938956540159881230",
                name = "imagine\r\n",
                type = 1,
                options = new []
                {
                    new{type = 3,
                    name = "promt",
                    value = $"{prompt}"}
                },
                application_command = new
                {
                    id = "938956540159881230",
                    application_id = "936929561302675456",
                    version = "1077969938624553050",
                    default_permission = true,
                    default_member_permissions = 0,
                    type = 1,
                    nsfw = false,
                    name = "imagine",
                    description = "Create images with Midjourney\r\n",
                    dm_permission = true,
    
    // ----------- Next data json here...
    //
    //And Send data:
    
     var payloadJson = JsonConvert.SerializeObject(payload);
            var response = client.UploadString("https://discord.com/api/v9/interactions", "POST", payloadJson);
            return response;
    

    request looks something like this