Advertisement
nonogamer9

NonoAI Source Code

May 30th, 2023 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | Jokes | 0 0
  1. const prefix = "!";
  2. const botname = "NonoAI playing: !help";
  3. const help = "__NonoAI Commands__\n!help, !say {TEXT}, !joke, !fact, !image {URL}, !weather {LOCATION}, !clock, !search {QUERY}, !youtube {URL}, !nicovideo {URL}";
  4.  
  5. function sendMsg(msg) {
  6. setTimeout(() => {
  7. socket.emit("talk", { text: msg });
  8. }, 1100);
  9. }
  10.  
  11. setTimeout(() => {
  12. socket.emit("command", { list: ["name", "NonoAI playing: !help"] });
  13. }, 1000);
  14.  
  15. setTimeout(() => {
  16. socket.emit("command", { list: ["name", "NonoAI playing: !help"] });
  17. }, 2100);
  18.  
  19. setTimeout(() => {
  20. sendMsg("NonoAI is online. Type " + prefix + "help to see commands. If you want to check my YouTube channel, visit https://www.youtube.com/channel/UC7a3K0C_eHvKu-pdDAqpHyg");
  21. setInterval(() => {
  22. sendMsg("NonoAI is online. Type " + prefix + "help to see commands. If you want to check my YouTube channel, visit https://www.youtube.com/channel/UC7a3K0C_eHvKu-pdDAqpHyg");
  23. }, 60000);
  24. }, 3200);
  25.  
  26. socket.on("talk", function (message) {
  27. if (message.text === prefix + "help") {
  28. sendMsg(help);
  29. } else if (message.text.startsWith(prefix + "say")) {
  30. sendMsg(message.text.substring(prefix.length + 3));
  31. } else if (message.text === prefix + "joke") {
  32. setTimeout(() => {
  33. socket.emit("command", { list: ["joke"] });
  34. }, 1100);
  35. } else if (message.text === prefix + "fact") {
  36. setTimeout(() => {
  37. socket.emit("command", { list: ["fact"] });
  38. }, 1100);
  39. } else if (message.text.startsWith(prefix + "image")) {
  40. const imageUrl = message.text.substring(prefix.length + 6).trim();
  41. if (imageUrl !== "") {
  42. sendImage(imageUrl);
  43. } else {
  44. sendMsg("Please provide a valid image URL after the command. Example: `!image https://example.com/image.jpg`");
  45. }
  46. } else if (message.text.startsWith(prefix + "weather")) {
  47. const location = message.text.substring(prefix.length + 8).trim();
  48. setTimeout(() => {
  49. getWeather(location);
  50. }, 1100);
  51. } else if (message.text === prefix + "clock") {
  52. const currentTime = new Date().toLocaleTimeString();
  53. sendMsg(`The current time is: ${currentTime}`);
  54. } else if (message.text.startsWith(prefix + "search")) {
  55. const query = message.text.substring(prefix.length + 7).trim();
  56. if (query !== "") {
  57. search(query);
  58. } else {
  59. sendMsg("Please provide a query after the command. Example: `!search cats`");
  60. }
  61. } else if (message.text.startsWith(prefix + "youtube")) {
  62. const videoUrl = message.text.substring(prefix.length + 8).trim();
  63. if (videoUrl !== "") {
  64. playYoutubeVideo(videoUrl);
  65. } else {
  66. sendMsg("Please provide a valid YouTube video URL after the command. Example: `!youtube https://www.youtube.com/watch?v=VIDEO_ID`");
  67. }
  68. } else if (message.text.startsWith(prefix + "nicovideo")) {
  69. const videoUrl = message.text.substring(prefix.length + 10).trim();
  70. if (videoUrl !== "") {
  71. playNicoVideo(videoUrl);
  72. } else {
  73. sendMsg("Please provide a valid NicoVideo URL after the command. Example: `!nicovideo https://www.nicovideo.jp/watch/sm9`");
  74. }
  75. } else {
  76. if (message.text.startsWith(prefix)) {
  77. sendMsg("Sorry, I don't understand that command. Type " + prefix + "help to see the list of available commands.");
  78. }
  79. }
  80. });
  81.  
  82. function sendImage(imageUrl) {
  83. setTimeout(() => {
  84. socket.emit("command", { list: ["image", imageUrl] });
  85. }, 1100);
  86. }
  87.  
  88. function getWeather(location) {
  89. const apiKey = "dcc6b2a3fa3d4fe58d9193316232905";
  90. const apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(location)}`;
  91.  
  92. fetch(apiUrl)
  93. .then(response => response.json())
  94. .then(data => {
  95. if (data.error) {
  96. sendMsg("Unable to retrieve weather information. Please check the location.");
  97. } else {
  98. const weather = data.current;
  99. const weatherInfo = `Weather in ${data.location.name}, ${data.location.country}:\nCondition: ${weather.condition.text}\nTemperature: ${weather.temp_c}°C\nHumidity: ${weather.humidity}%\nWind Speed: ${weather.wind_kph} km/h\n`;
  100. sendMsg(weatherInfo);
  101. }
  102. })
  103. .catch(error => {
  104. console.error("Error:", error);
  105. sendMsg("An error occurred while retrieving weather information.");
  106. });
  107. }
  108.  
  109. function search(query) {
  110. const apiKey = "4a4725a769msh10c74bc91185a85p182f25jsn9a1a93fbbb77";
  111. const apiUrl = `https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/search/NewsSearchAPI?q=${encodeURIComponent(query)}&pageSize=1`;
  112.  
  113. fetch(apiUrl, {
  114. headers: {
  115. "X-RapidAPI-Key": apiKey,
  116. "X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com"
  117. }
  118. })
  119. .then(response => response.json())
  120. .then(data => {
  121. if (data.value && data.value.length > 0) {
  122. const searchResults = data.value.map(result => ({
  123. title: result.title,
  124. description: result.description.length > 50 ? result.description.substring(0, 50) + "..." : result.description,
  125. url: result.url
  126. }));
  127.  
  128. sendMsg(formatSearchResults(searchResults));
  129. } else {
  130. sendMsg("No search results found.");
  131. }
  132. })
  133. .catch(error => {
  134. console.error("Error:", error);
  135. sendMsg("An error occurred while performing the search.");
  136. });
  137. }
  138.  
  139. function formatSearchResults(results) {
  140. let message = "Search Results:\n";
  141. results.forEach((result, index) => {
  142. message += `\n${index + 1}. ${result.title}\nDescription: ${result.description}\nURL: ${result.url}\n`;
  143. });
  144. return message;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement