尝试将套接字数据传递给bot时未定义消息

时间:2019-01-24 13:43:14

标签: javascript node.js discord.js

我需要获取传递到bot回调的数据:

socket.on('data', function(data, message) {...});

我只是在它们之间进行了if检查,以使其仅将触发的消息发送到通道。

我确实更新了脚本,但是出现了SEND错误。

const Discord = require('discord.js')
const client = new Discord.Client()
const opn = require('opn')
const getJSON = require('get-json')
const request = require('request');
const config = require("./config.json");
//var logger = require('winston');
var net = require('net');

client.on('ready', () => {
  // List servers the bot is connected to
  console.log("Servers:");
  client.guilds.forEach((guild) => {
    console.log(`\x1b[43m%s\x1b[0m`, " - " + guild.name);
    // List all channels
    guild.channels.forEach((channel) => {
      console.log(`\x1b[45m%s\x1b[0m`, `-- ${channel.name} (${channel.type}) - ${channel.id}`);
    });
  });
});

client.on("ready", () => {
  // This event will run if the bot starts, and logs in, successfully.
  console.log(`\x1b[42m%s\x1b[0m`, `Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
  // Example of changing the bot's playing game to something useful. `client.user` is what the
  // docs refer to as the "ClientUser".
  client.user.setActivity(`Serving ${client.guilds.size} servers`);
});

client.on('ready', () => {
  // Set bot status to: "Playing with JavaScript"
  client.user.setActivity("Homeseer", {
    type: "WATCHING"
  });
  // Alternatively, you can set the activity to any of the following:
  // PLAYING, STREAMING, LISTENING, WATCHING
  // For example:
  // client.user.setActivity("TV", {type: "WATCHING"})
});

//DEBUG
const port = "11000";
const host = "192.168.1.12";
const lightss = "DC,4011";
var socket = net.createConnection(port, host);
console.log('Socket created.');
socket.on('data', function(data, message) {
  // Log the response from the HTTP server.
  console.log('' + data);
  var read = data.toString();
  const client = new Discord.Client()
  let channel = client.channels.get("**************");
  if (read.match("DC,4043,100,0")) {
    console.log("Connected to ");
    channel.send('enabled');
  }
}).on('connect', function() {
  // Manually write an HTTP request.
  socket.write("GET / HTTP/1.0\r\n\r\n");
}).on('end', function() {
  console.log('DONE');
});

bot_secret_token = "";
client.login(bot_secret_token);

2 个答案:

答案 0 :(得分:0)

尝试一下(未测试),只有一个事件处理程序“就绪”

const Discord = require('discord.js')
const client = new Discord.Client()
const opn = require('opn')
const getJSON = require('get-json')
const request = require('request');
const config = require("./config.json");
//var logger = require('winston');
var net = require('net');


client.on("ready", () => {
    // This event will run if the bot starts, and logs in, successfully.
    console.log(`\x1b[42m%s\x1b[0m`, `Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
    // Example of changing the bot's playing game to something useful. `client.user` is what the
    // docs refer to as the "ClientUser".
    client.user.setActivity(`Serving ${client.guilds.size} servers`);

    // Set bot status to: "Playing with JavaScript"
    client.user.setActivity("Homeseer", {
        type: "WATCHING"
    });
    // Alternatively, you can set the activity to any of the following:
    // PLAYING, STREAMING, LISTENING, WATCHING
    // For example:
    // client.user.setActivity("TV", {type: "WATCHING"})


    // List servers the bot is connected to
    console.log("Servers:");
    client.guilds.forEach((guild) => {
        console.log(`\x1b[43m%s\x1b[0m`, " - " + guild.name);
        // List all channels
        guild.channels.forEach((channel) => {
            console.log(`\x1b[45m%s\x1b[0m`, `-- ${channel.name} (${channel.type}) - ${channel.id}`);
        });
    });

    //DEBUG
    const port = "11000";
    const host = "192.168.1.12";
    const lightss = "DC,4011";
    var socket = net.createConnection(port, host);
    console.log('Socket created.');
    socket.on('data', function (data, message) {
        // Log the response from the HTTP server.
        console.log('' + data);
        var read = data.toString();
        //const client = new Discord.Client()
        let channel = client.channels.get("**************");
        if (read.match("DC,4043,100,0")) {
            console.log("Connected to ");
            channel.send('enabled');
        }
    }).on('connect', function () {
        // Manually write an HTTP request.
        socket.write("GET / HTTP/1.0\r\n\r\n");
    }).on('end', function () {
        console.log('DONE');
    });
});

bot_secret_token = "";
client.login(bot_secret_token);

答案 1 :(得分:-1)

您应该在此处使用clientconst client = new Discord.Client();) 因此,您只需执行以下操作: let channel = client.channels.get("channel id here");,然后channel.send('enabled'); 希望对您有帮助