Bonk.io ChatGPT Integration

Type /cmds in the bonk chat to get started

  1. // ==UserScript==
  2. // @name Bonk.io ChatGPT Integration
  3. // @namespace https://greatest.deepsurf.us/en/users/1122101
  4. // @version 1.1
  5. // @description Type /cmds in the bonk chat to get started
  6. // @author rats410
  7. // @match https://bonk.io/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var lobbyChatInput = document.getElementById("newbonklobby_chat_input");
  16. var lobbyChatContent = document.getElementById("newbonklobby_chat_content");
  17. var gameChatInput = document.getElementById("ingamechatinputtext");
  18. var gameChatContent = document.getElementById("ingamechatcontent");
  19. var apiKey = null;
  20. var isChatGPTGeneratingResponse = false;
  21.  
  22. apiKey = localStorage.getItem("chatGPTApiKey");
  23.  
  24. function displayCommands() {
  25. var commandsMessage = "All Commands: /key, /setup, /credits, /cmds";
  26.  
  27. var commandsDiv = document.createElement("div");
  28. commandsDiv.className = "chat-content-message ng-binding";
  29. commandsDiv.textContent = commandsMessage;
  30. commandsDiv.style.color = "#008000"; // Green
  31.  
  32. if (lobbyChatContent) {
  33. lobbyChatContent.appendChild(commandsDiv);
  34. }
  35.  
  36. if (gameChatContent) {
  37. gameChatContent.appendChild(commandsDiv.cloneNode(true));
  38. }
  39. }
  40.  
  41. function listenForCommands() {
  42. if (lobbyChatInput && lobbyChatContent) {
  43. lobbyChatInput.addEventListener("keydown", function(e) {
  44. if (e.keyCode === 13) {
  45. var chatValue = lobbyChatInput.value.trim();
  46. if (chatValue.startsWith("/cmds")) {
  47. displayCommands();
  48. lobbyChatInput.value = "";
  49. e.preventDefault();
  50. } else if (chatValue.startsWith("/key")) {
  51. handleKeyCommand(chatValue);
  52. lobbyChatInput.value = "";
  53. e.preventDefault();
  54. } else if (isChatGPTCommand(chatValue)) {
  55. communicateWithChatGPT(chatValue);
  56. e.preventDefault();
  57. } else if (chatValue.startsWith("/setup")) {
  58. showSetupInstructions();
  59. lobbyChatInput.value = "";
  60. e.preventDefault();
  61. } else if (chatValue.startsWith("/credits")) {
  62. displayCredits();
  63. lobbyChatInput.value = "";
  64. e.preventDefault();
  65. }
  66. }
  67. });
  68. }
  69.  
  70. if (gameChatInput && gameChatContent) {
  71. gameChatInput.addEventListener("keydown", function(e) {
  72. if (e.keyCode === 13) {
  73. var chatValue = gameChatInput.value.trim();
  74. if (chatValue.startsWith("/cmds")) {
  75. displayCommands();
  76. gameChatInput.value = "";
  77. e.preventDefault();
  78. } else if (chatValue.startsWith("/key")) {
  79. handleKeyCommand(chatValue);
  80. gameChatInput.value = "";
  81. e.preventDefault();
  82. } else if (isChatGPTCommand(chatValue)) {
  83. communicateWithChatGPT(chatValue);
  84. e.preventDefault();
  85. } else if (chatValue.startsWith("/setup")) {
  86. showSetupInstructions();
  87. gameChatInput.value = "";
  88. e.preventDefault();
  89. } else if (chatValue.startsWith("/credits")) {
  90. displayCredits();
  91. gameChatInput.value = "";
  92. e.preventDefault();
  93. }
  94. }
  95. });
  96. }
  97. }
  98.  
  99. listenForCommands();
  100.  
  101. function handleKeyCommand(command) {
  102. var args = command.substr(1).trim().split(" ");
  103. var commandName = args[0].toLowerCase();
  104.  
  105. if (commandName === "key") {
  106. if (args.length >= 2) {
  107. apiKey = args.slice(1).join(" ").trim();
  108. localStorage.setItem("chatGPTApiKey", apiKey);
  109. displayInChat("API key set successfully. ChatGPT is ready to use.", "system");
  110. } else {
  111. displayInChat("No API key provided. Set API key with /key [API key here]", "system");
  112. }
  113. } else {
  114. // Ignore
  115. }
  116. }
  117.  
  118. function isChatGPTCommand(message) {
  119. return (
  120. message.toLowerCase().includes("gpt") ||
  121. message.toLowerCase().includes("chatgpt")
  122. );
  123. }
  124.  
  125. const gptData = {
  126. model: "gpt-3.5-turbo",
  127. max_tokens: 2048,
  128. user: "1",
  129. temperature: 1,
  130. frequency_penalty: 0.2,
  131. presence_penalty: -1,
  132. stop: ["#", ";"]
  133. };
  134.  
  135. function communicateWithChatGPT(message) {
  136. if (isChatGPTGeneratingResponse) {
  137. return;
  138. }
  139. isChatGPTGeneratingResponse = true;
  140.  
  141. if (apiKey) {
  142. var requestPayload = {
  143. messages: [{ role: 'user', content: message }],
  144. ...gptData
  145. };
  146.  
  147. var oHttp = new XMLHttpRequest();
  148. oHttp.open("POST", "https://api.openai.com/v1/chat/completions");
  149. oHttp.setRequestHeader("Accept", "application/json");
  150. oHttp.setRequestHeader("Content-Type", "application/json");
  151. oHttp.setRequestHeader("Authorization", "Bearer " + apiKey);
  152.  
  153. oHttp.onreadystatechange = function() {
  154. if (oHttp.readyState === 4) {
  155. if (oHttp.status === 200) {
  156. var response = JSON.parse(oHttp.responseText);
  157. if (response.choices && response.choices.length > 0) {
  158. var reply = response.choices[0].message.content;
  159. newbonklobby_chat_input.value = "ChatGPT: " + reply;
  160. simulateEnterKeyPress(newbonklobby_chat_input);
  161. } else {
  162. displayInChat("Failed to generate a response from ChatGPT", "system");
  163. }
  164. } else {
  165. displayInChat("An error occurred while communicating with ChatGPT", "system");
  166. }
  167. isChatGPTGeneratingResponse = false;
  168. }
  169. };
  170.  
  171. oHttp.send(JSON.stringify(requestPayload));
  172. } else {
  173. displayInChat("No API key provided. Set API key with /key [API key here] first", "system");
  174. isChatGPTGeneratingResponse = false;
  175. }
  176. }
  177.  
  178. function simulateEnterKeyPress(element) {
  179. var event = new KeyboardEvent("keydown", {
  180. keyCode: 13,
  181. bubbles: true,
  182. cancelable: true,
  183. });
  184. element.dispatchEvent(event);
  185. }
  186.  
  187. function simulatePublicChatMessage(message) {
  188. var chatMessageEvent = new Event('message', { bubbles: true });
  189. var messageData = { type: "public", content: "ChatGPT: " + message };
  190. Object.defineProperty(chatMessageEvent, 'data', { value: JSON.stringify(messageData) });
  191. document.dispatchEvent(chatMessageEvent);
  192. }
  193.  
  194. function displayInChat(message, messageType) {
  195. var chatMessage = document.createElement("div");
  196. chatMessage.className = "chat-content-message";
  197. chatMessage.textContent = message;
  198.  
  199. if (messageType === "system") {
  200. chatMessage.style.color = "#808080"; // Gray
  201. }
  202.  
  203. if (lobbyChatContent) {
  204. lobbyChatContent.appendChild(chatMessage);
  205. }
  206.  
  207. if (gameChatContent) {
  208. gameChatContent.appendChild(chatMessage.cloneNode(true));
  209. }
  210. }
  211.  
  212. function showSetupInstructions() {
  213. var setupInstructions =
  214. "Use /key [API key here] to link your GPT (stored securely)." +
  215. "Use phrases like 'hey gpt' or 'gpt' to talk to ChatGPT.";
  216.  
  217. var setupMessage = document.createElement("div");
  218. setupMessage.className = "chat-content-message";
  219. setupMessage.textContent = setupInstructions;
  220. setupMessage.style.color = "#C0C0C0"; // Silver
  221.  
  222. if (lobbyChatContent) {
  223. lobbyChatContent.appendChild(setupMessage);
  224. }
  225.  
  226. if (gameChatContent) {
  227. gameChatContent.appendChild(setupMessage.cloneNode(true));
  228. }
  229. }
  230.  
  231. function displayCredits() {
  232. var creditsMessage =
  233. "Thanks to LEGENDBOSS123 and iNeonz for their helpful Userscripts!";
  234.  
  235. var creditsDiv = document.createElement("div");
  236. creditsDiv.className = "chat-content-message";
  237. creditsDiv.textContent = creditsMessage;
  238. creditsDiv.style.color = "#FFD700"; // Gold
  239.  
  240. if (lobbyChatContent) {
  241. lobbyChatContent.appendChild(creditsDiv);
  242. }
  243.  
  244. if (gameChatContent) {
  245. gameChatContent.appendChild(creditsDiv.cloneNode(true));
  246. }
  247. }
  248. })();