【问题标题】:HTML not updating when running my Javascript twitch bot运行我的 Javascript twitch 机器人时 HTML 未更新
【发布时间】:2020-07-04 01:58:31
【问题描述】:

const tmi = require('tmi.js');

// Define configuration options
var x = "asdfasdf"
const opts = {
  identity: {
    username: 'username',
    password: 'password'
  },
  channels: [
    "rabeya74"
  ]
};

// Create a client with our options
const client = new tmi.client(opts);

// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

// Connect to Twitch:
client.connect();

// Called every time a message comes in
function onMessageHandler(target, context, msg, self) {
  if (self) {
    return;
  } // Ignore messages from the bot

  // Remove whitespace from chat message
  const commandName = msg.trim();

  // If the command is known, let's execute it
  if (commandName === '!dice') {
    const num = rollDice();
    client.say(target, `You rolled a ${num}`);
    document.body.innerHTML = x;
    console.log(`* Executed ${commandName} command`);
    document.body.innerHTML = `You rolled a ${num}`;
    window.onload = function() {
      document.getElementById("display").innerHTML = "hello";
    }
    document.getElementById("display").innerHTML = "hello";
  } else {
    console.log(`* Unknown command ${commandName}`);
  }
}
// Function called when the "dice" command is issued
function rollDice() {
  const sides = 6;
  return Math.floor(Math.random() * sides) + 1;
}
// Called every time the bot connects to Twitch chat
function onConnectedHandler(addr, port) {
  console.log(`* Connected to ${addr}:${port}`);
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body>
  <script src="bot.js"></script>
  <div id="display"></div>
</body>

</html>

我正在尝试制作一个小型 twitch javascript 应用程序,当我掷骰子时它会更新 HTML 文件。我尝试了 document.body.innerHTML 文件的每个版本,但它根本不想更新。是因为它在函数中吗?它似乎没有显示任何内容。

【问题讨论】:

    标签: javascript html twitch


    【解决方案1】:

    您不能在浏览器中使用require()。此代码产生错误,因为 require 未定义。尝试在 &lt;head&gt; 元素中添加 &lt;script src="tmi.js"&gt;&lt;/script&gt;

    【讨论】:

    • 我刚试了,没区别?似乎什么都没有弹出
    • @ZarifRahman 确保从其他脚本中删除 require 行。
    • 我做了 const tmi = 'tmi.js' 并且还尝试将 tmi 作为一个整体删除,但这并没有真正的意义。他们都给我错误。 TMI 是一个来自 twitch 的脚本,它不在工作区中。
    猜你喜欢
    • 2021-05-03
    • 2018-12-31
    • 2021-04-19
    • 2017-12-26
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2014-08-26
    • 2015-02-21
    相关资源
    最近更新 更多