|
| 1 | +/* |
| 2 | + Copyright (C) 2025 Flyinghead |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +#include "discord.h" |
| 18 | +#include <curl/curl.h> |
| 19 | +#include "json.hpp" |
| 20 | + |
| 21 | +static std::string DiscordWebhook; |
| 22 | + |
| 23 | +using namespace nlohmann; |
| 24 | + |
| 25 | +struct { |
| 26 | + const char *name; |
| 27 | + const char *url; |
| 28 | +} Games[] = { |
| 29 | + { "Daytona USA", "https://dcnet.flyca.st/gamepic/daytona.jpg" }, |
| 30 | + { "Daytona USA", "https://dcnet.flyca.st/gamepic/daytona.jpg" }, |
| 31 | + { "Sega Tetris", "https://dcnet.flyca.st/gamepic/segatetris.jpg" }, |
| 32 | + { "Golf Shiyou Yo 2", "https://dcnet.flyca.st/gamepic/golfshiyou2.jpg" }, |
| 33 | +}; |
| 34 | + |
| 35 | +class Notif |
| 36 | +{ |
| 37 | +public: |
| 38 | + Notif(GameId gameId) : gameId(gameId) {} |
| 39 | + |
| 40 | + std::string to_json() const |
| 41 | + { |
| 42 | + json embeds; |
| 43 | + embeds.push_back({ |
| 44 | + { "author", |
| 45 | + { |
| 46 | + { "name", Games[(int)gameId].name }, |
| 47 | + { "icon_url", Games[(int)gameId].url } |
| 48 | + }, |
| 49 | + }, |
| 50 | + { "title", embed.title }, |
| 51 | + { "description", embed.text }, |
| 52 | + { "color", 9118205 }, |
| 53 | + }); |
| 54 | + |
| 55 | + json j = { |
| 56 | + { "content", content }, |
| 57 | + { "embeds", embeds }, |
| 58 | + }; |
| 59 | + return j.dump(4); |
| 60 | + } |
| 61 | + |
| 62 | + GameId gameId; |
| 63 | + std::string content; |
| 64 | + struct { |
| 65 | + std::string title; |
| 66 | + std::string text; |
| 67 | + } embed; |
| 68 | +}; |
| 69 | + |
| 70 | +static void postWebhook(const Notif& notif) |
| 71 | +{ |
| 72 | + if (DiscordWebhook.empty()) |
| 73 | + return; |
| 74 | + CURL *curl = curl_easy_init(); |
| 75 | + if (curl == nullptr) { |
| 76 | + fprintf(stderr, "Can't create curl handle\n"); |
| 77 | + return; |
| 78 | + } |
| 79 | + CURLcode res; |
| 80 | + curl_easy_setopt(curl, CURLOPT_URL, DiscordWebhook.c_str()); |
| 81 | + curl_easy_setopt(curl, CURLOPT_USERAGENT, "DCNet-DiscordWebhook"); |
| 82 | + curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json"); |
| 83 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 84 | + |
| 85 | + std::string json = notif.to_json(); |
| 86 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str()); |
| 87 | + |
| 88 | + res = curl_easy_perform(curl); |
| 89 | + if (res != CURLE_OK) { |
| 90 | + fprintf(stderr, "curl error: %d", res); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + long code; |
| 95 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); |
| 96 | + if (code < 200 || code >= 300) |
| 97 | + fprintf(stderr, "Discord error: %ld", code); |
| 98 | + } |
| 99 | + curl_slist_free_all(headers); |
| 100 | + curl_easy_cleanup(curl); |
| 101 | +} |
| 102 | + |
| 103 | +void setDiscordWebhook(const std::string& url) { |
| 104 | + DiscordWebhook = url; |
| 105 | +} |
| 106 | + |
| 107 | +void discordLobbyJoined(GameId gameId, const std::string& username, const std::string& lobbyName, const std::vector<std::string>& playerList) |
| 108 | +{ |
| 109 | + Notif notif(gameId); |
| 110 | + notif.content = "Player **" + username + "** joined lobby ***" + lobbyName + "***"; |
| 111 | + notif.embed.title = "Lobby Players"; |
| 112 | + for (const auto& player : playerList) |
| 113 | + notif.embed.text += player + "\n"; |
| 114 | + postWebhook(notif); |
| 115 | +} |
| 116 | + |
| 117 | +void discordGameCreated(GameId gameId, const std::string& username, const std::string& gameName, const std::vector<std::string>& playerList) |
| 118 | +{ |
| 119 | + Notif notif(gameId); |
| 120 | + notif.content = "Player **" + username + "** created team ***" + gameName + "***"; |
| 121 | + notif.embed.title = "Lobby Players"; |
| 122 | + for (const auto& player : playerList) |
| 123 | + notif.embed.text += player + "\n"; |
| 124 | + |
| 125 | + postWebhook(notif); |
| 126 | +} |
0 commit comments