【问题标题】:How do I communicate through my discord bot and website (and get/store data in my discord bot)如何通过我的不和谐机器人和网站进行交流(并在我的不和谐机器人中获取/存储数据)
【发布时间】:2021-06-24 11:05:36
【问题描述】:

我正在尝试制作一个 Discord 机器人,每当您在网站上按下/执行某项操作时,它都会与机器人通信,并向其发送数据。有人有什么建议吗?以下是我的 GitHub 托管网站上的其中一个页面的一些代码:

<html>
<!–- Website Stuff -–>
<button onclick="share()">Share</button>
<p id="sharedtext"></p>
<script>
function share() {
var projectID = prompt('Whats the project ID of your scratch project?')

<!-- Send data to bot -->

document.getElementById("sharedtext").innerHTML = "Project Shared.";
}
</script>

我使用 discord.js 来制作我的 discord 机器人。我对 JavaScript 也有很好的理解。

【问题讨论】:

标签: discord discord.js github-pages


【解决方案1】:

您可以使用 mysql 数据库来保存来自网站的数据,并且机器人每 5 秒设置一个间隔来检查是否有新数据以及是否有新数据执行所需的操作。 HTML:

<html>
  <body>
    <button onclick="share()">Share</button>
    <p id="sharedtext"></p>
    <script>
    function share() {
        var projectID = prompt('Whats the project ID of your scratch project?');
        var path = 'Action page path here'; //Change this to the action page path
        if(projectID !== null){
           var Http = new XMLHttpRequest();
           Http.responseType = 'text';
           Http.onreadystatechange = function(){
             if(Http.readyState == 4 && Http.status == 200){
                 document.getElementById("sharedtext").innerHTML = Http.response;
             }
           }
           Http.open("POST", path, true);
           Http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
           Http.send(`projectID=${projectID}`);
        }
    }
    </script>
  </body>
</html>

动作页面:

<?php
   $hostname = 'localhost';
   $username = 'root';
   $password = '';
   $dbname = 'db';
   $conn = new mysqli($hostname, $username, $password, $dbname);
   if(!$conn){
       echo 'There was an error while connecting to the database!';
       exit();
   }

   if(isset($_POST['projectID'])){
      $stmt = $conn->prepare("SELECT * FROM yourTable WHERE projectid = ?");
      $stmt->bind_param("s", $_POST['projectID']);
      $stmt->execute();
      $result = $stmt->get_result();
      if($result->num_rows < 1){
          $stmt2 = $conn->prepare("INSERT INTO yourTable (projectid) VALUES (?)");
          $stmt2->bind_param("s", $_POST['projectID']);
          $stmt2->execute();
          echo 'Project Shared.';
          exit();
      } else {
         echo 'The project ID has already been shared!';
         exit();
      }
   } else {
       echo 'Invalid method';
       exit();
   }
?>

机器人:

const Discord = require('discord.js');
const mysql = require('mysql');
const client = new Discord.Client();

var conn = = mysql.createPool({
     connectionLimit : 50,
     host            : 'localhost',
     user            : 'root',
     password        : '',
     database        : 'db'
});

var diff, length, numb;
function checkNew(){
    conn.query('SELECT * FROM yourTable ORDER BY id ASC', function(err, results){
        if(err) return console.log(err);
        if(length === results.length) return;
        diff = results.length - length;
        numb = results.length;
        length = results.length;
        while(diff > 0){
            results[numb - 1].projectid //Do something with the project id
            --numb;
            --diff;
        }
    });
}
client.once("ready", () => {
    console.log(`Started`);
    setInterval(checkNew, 5000);
});

MySQL:

CREATE TABLE yourTable(
    id int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
    projectid TINYTEXT NOT NULL
);

【讨论】:

  • 嗨!对不起,我是个 nub,但我想知道我应该如何处理 MySQL 文件?我想把它和其他人放在同一个文件夹中。如果我错了,请告诉我。
  • 另外,我使用this 制作我的机器人,它说:SyntaxError: Unexpected token '='
  • 点击链接时看不到你的repl文件,所以看不出问题出在哪里。对于 SQL,您需要一个 repl.it 不提供的 MySQL 数据库。您可以寻找另一个提供 MySQL 数据库的虚拟主机。在一些不和谐的机器人托管中,他们还提供 MySQL 数据库,您也可以将其用于您的网站。
  • 这里,去This
猜你喜欢
  • 2018-03-20
  • 2019-09-04
  • 2021-11-18
  • 2020-06-22
  • 1970-01-01
  • 2022-11-25
  • 2020-07-03
  • 2020-09-23
  • 2021-10-30
相关资源
最近更新 更多