1

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
givik
  • 23
  • 6
  • Please provide enough code so others can better understand or reproduce the problem. – Blue Robin Mar 22 '23 at 17:47
  • 1
    Code is not needed here. – givik Mar 22 '23 at 18:33
  • I'm assuming you're trying to use your own bot to run a command on another bot. Most public bots **do not** read messages from other bots, to prevent abuse, so this is impossible within the discord api. You *could* technically use http requests to send commands as a normal user, but it's not well documented and against tos, so you probably won't receive help. – zmehall Mar 22 '23 at 18:53
  • So if this method is against TOS. Do you know any other solution to achieve this goal? – givik Mar 22 '23 at 19:15
  • I want to run commands through the user, not as a bot. – givik Mar 22 '23 at 19:17
  • Automating discord actions through a regular user is against their TOS so you can't get help with that here. – Dan Mar 22 '23 at 20:25

1 Answers1

3

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

  • Thanks for helping :) So in which order, I should send requests? And what data does payloadJson contain? can you share a working example? – givik Mar 23 '23 at 08:27
  • I edited my answer for you – Павел Вайт Mar 23 '23 at 09:18
  • I have followed the instructions you provide but it is not working. Does this work on your side? If I am sending data based on your example it shows 400 Bad Request. I have tried the second option to just copy the request as it was with all parameters, in that case, the response was empty but not sending prompts to the discord chat anyway. – givik Mar 24 '23 at 11:40
  • @GiviKuchukhidze, it's working for me. There may be an error in the object you're trying to reproduce. Perhaps you're ignoring the nesting? https://yapx.ru/album/VzXSI There is a simple solution to find the error, run your code and look at what you sent in Fiddler. Compare all the data strings when you manually send a request through Discord. – Павел Вайт Mar 24 '23 at 15:30
  • I was sending one extra parameter which was causing 400 bad requests. Now it is working and I am very happy! Thank you – givik Mar 24 '23 at 19:59
  • @givik Accept my answer as a solution if it helped you. A green checkmark next to my answer. Glad to help – Павел Вайт Mar 25 '23 at 09:20
  • thanks! this used to work, but not anymore request failed: 400 {"code": 50035, "errors": {"data": {"_errors": [{"code": "INTERACTION_APPLICATION_COMMAND_INVALID_VERSION", "message": "This command is outdated, please try again in a few minutes"}]}}, "message": "Invalid Form Body"} | – Aviad Rozenhek Jun 16 '23 at 11:58
  • Thank you, it works really well! – Tiago Rangel Jul 12 '23 at 11:41