Skip to content

Latest commit

 

History

History
56 lines (45 loc) · 2.32 KB

NOTIFY_DISCORD.md

File metadata and controls

56 lines (45 loc) · 2.32 KB

Example code to send in-game event notifications to Discord.

Requirements for this example code:

notify_discord function:

// send JSON RPC post to Discord bot
void notify_discord(string message, string channel)
{
  extern bool DEBUG_INSTANCE;
  if (DEBUG_INSTANCE) // disable if running under debugger
    return;

  ix::HttpClient httpClient;
  std::stringstream ss;

  string url;
  if (channel == "general")
    url = "https://discord.com/api/webhooks/yourDiscordGeneralWebhook";
  else if (channel == "logins")
    url = "https://discord.com/api/webhooks/yourDiscordLoginsWebhook";
  else if (channel == "immortals") {
    url = "https://discord.com/api/webhooks/yourDiscordImmortalsWebook";
  } else {
    cerr << "ERROR: notify_discord unknown channel: " << channel << endl;
    return;
  }

  auto args = httpClient.createRequest();
  args->extraHeaders["Content-Type"] = "application/json";

  nlohmann::json body;
  body["content"] = message;


  auto response = httpClient.post(url, body.dump(), args);

  auto statusCode = response->statusCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
  auto errorCode = response->errorCode;   // 200, 404, etc...
  auto responseHeaders =
      response->headers;              // All the headers in a special case-insensitive unordered_map of (string, string)
  auto responseBody = response->body; // All the bytes from the response as an std::string
  auto errorMsg = response->errorMsg; // Descriptive error message in case of failure
  auto uploadSize = response->uploadSize;     // Byte count of uploaded data
  auto downloadSize = response->downloadSize; // Byte count of downloaded data

  // uncomment for debugging responses
  //cerr << "notify_discord status:" << statusCode << " response: " << responseBody << " error: " << errorMsg << endl;
}

Recommended: call this function via a background thread so the http request doesn't pause gameplay.