Create Channel Switch Logger

2019-09-25 09:11发布

问题:

I'm trying to create a channel switch logger which allows me to specify a channel where the messages get posted.
So, for example, I create a TextChannel called "Channel Switches". When now a user changes voice channel, it should appear a message in this Channel. (eg. <USER> left channel <CHANNEL> and joined <CHANNEL>.)

MY PROBLEM IS: I get no errors and the Bot is not responding...

Here my first try:

var Discord = require('discord.js');
var logger = require("winston");
var auth = require("./auth.json");
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
  colorize: true
});
logger.level = "debug";

// Initialize Discord Bot
var bot = new Discord.Client({
  token: auth.token,
  autorun: true
});

bot.on("ready", function(evt) {
  logger.info("Connected");
  logger.info("Logged in as: ");
  logger.info(bot.username + " – (" + bot.id + ")");
  console.log("Logged in as ${client.user.tag}!");
});

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel
  let oldUserChannel = oldMember.voiceChannel


  if (!oldUserChannel && newUserChannel) {

    bot.channels.get('475330828466126848').send("User went form Channel" + oldUserChannel.name + "to the new" +
      newUserChannel.name + "Channel");

  }
});

回答1:

Your bot does not respond because the client is not initialized correctly. You're creating the client like this:

var bot = new Discord.Client({
  token: auth.token, // <--
  autorun: true // <--
});

The problem is that these arguments do not exist in discord.js, as stated by the Client docs.
To log into your bot, please use Client.login():

var bot = new Discord.Client();
bot.login(auth.token);