Twitch Pokecatch Bot

Auto catch pokemmons

  1. // ==UserScript==
  2. // @name Twitch Pokecatch Bot
  3. // @description Auto catch pokemmons
  4. // @namespace https://github.com/victornpb
  5. // @version 1.0
  6. // @match *://*.twitch.tv/*
  7. // @run-at document-idle
  8. // @author victornpb
  9. // @homepageURL https://github.com/victornpb/
  10. // @supportURL https://github.com/victornpb/
  11. // @contributionURL https://www.buymeacoffee.com/vitim
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. /* jshint esversion: 8 */
  17.  
  18. (function () {
  19.  
  20. const LOGPREFIX = '[POKECATCH]';
  21.  
  22. function createElm(html) {
  23. const div = document.createElement('div');
  24. div.innerHTML = html;
  25. return div.removeChild(div.children[0]);
  26. }
  27.  
  28. document.head.appendChild(createElm`
  29. <style>
  30. .pokeBtn {
  31. display: inline-flex;
  32. -webkit-box-align: center;
  33. align-items: center;
  34. -webkit-box-pack: center;
  35. justify-content: center;
  36. user-select: none;
  37. height: var(--button-size-default);
  38. width: var(--button-size-default);
  39. border-radius: var(--border-radius-medium);
  40. background-color: var(--color-background-button-text-default);
  41. color: var(--color-fill-button-icon);
  42. }
  43. </style>`);
  44.  
  45.  
  46. // activation button
  47. const activateBtn = createElm(`
  48. <span class="pokeBtn" title="Pokecatching...">
  49. <svg data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 980 978.9" style="fill: currentcolor;">
  50. <path d="M509 979h-38l-5-1A485 485 0 0 1 2 531c-1-7 0-14-2-21v-43a59 59 0 0 0 1-5 461 461 0 0 1 18-107Q66 194 201 95c67-50 143-80 226-91a480 480 0 0 1 103-2c38 3 75 10 111 22q208 70 299 271c22 48 34 100 38 153 1 7 0 14 2 20v42l-1 7a464 464 0 0 1-18 106q-43 150-164 248-117 94-267 106c-7 1-14 0-21 2ZM178 521H82c-8 0-8 0-7 7a456 456 0 0 0 8 49q33 148 153 242a406 406 0 0 0 365 72 413 413 0 0 0 304-364c0-4 1-6-5-6H704c-3 0-4 1-5 4l-2 14a212 212 0 0 1-416-14c-1-3-2-4-6-4h-97Zm165-32a147 147 0 0 0 294 0 147 147 0 0 0-294 0Z"/>
  51. </svg>
  52. </span>
  53. `);
  54. // activateBtn.onclick = toggle;
  55.  
  56. let enabled;
  57. let watchdogTimer;
  58.  
  59. setInterval(activatorWatchdog, 5000);
  60. function activatorWatchdog() {
  61. const modBtn = document.querySelector('[data-a-target="chat-settings"]');
  62. if (modBtn) {
  63. const twitchBar = modBtn.parentElement.parentElement.parentElement.parentElement.parentElement;
  64. if (twitchBar && !twitchBar.contains(activateBtn)) {
  65. console.log(LOGPREFIX, 'Adding button...');
  66. twitchBar.insertBefore(activateBtn, twitchBar.firstChild);
  67. if (!enabled) {
  68. console.log(LOGPREFIX, 'Started chatWatchdog...');
  69. watchdogTimer = setInterval(chatWatchdog, 500);
  70. enabled = true;
  71. }
  72. }
  73. }
  74. else {
  75. if (enabled) {
  76. console.log(LOGPREFIX, 'Stopped chatWatchdog!');
  77. clearInterval(watchdogTimer);
  78. watchdogTimer = enabled = false;
  79. }
  80. }
  81. }
  82.  
  83.  
  84. function parseChat() {
  85. return Array.from(document.querySelectorAll('[data-test-selector="chat-line-message"]')).map(chat => {
  86. return {
  87. username: chat.querySelector('[data-test-selector="message-username"]')?.innerText || '',
  88. message: chat.querySelector('[data-test-selector="chat-line-message-body"]')?.innerText || '',
  89. // timestamp: chat.querySelector('[data-test-selector="chat-timestamp"]').innerText,
  90. };
  91. });
  92. }
  93.  
  94. const delay = t => new Promise(r => setTimeout(r, t));
  95.  
  96. let ignoredMessage = new Set();
  97.  
  98. async function chatWatchdog() {
  99. const messages = parseChat().filter(m => !ignoredMessage.has(m.message));
  100. for (const m of messages) {
  101. if (m.username === 'PokemonCommunityGame' && m.message.match(/A wild (\w+) appears/)) {
  102. ignoredMessage.add(m.message);
  103.  
  104. console.log(LOGPREFIX, 'Catching pokemon...', m);
  105. sendMessage('!pokecatch greatball');
  106. }
  107. }
  108. }
  109.  
  110. function sendMessage(msg) {
  111. console.log(LOGPREFIX, 'Sending...', msg);
  112. const textarea = document.querySelector("[data-a-target='chat-input']");
  113. const nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
  114. nativeTextAreaValueSetter.call(textarea, msg);
  115. const event = new Event('input', { bubbles: true });
  116. textarea.dispatchEvent(event);
  117. document.querySelector("[data-a-target='chat-send-button']").click();
  118. }
  119.  
  120. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址