1

我是一名小型 twitch 流媒体/内容创建者,即将推出我的 Discord,我认为如果我的 Discord 中有一个 IRC 频道在我直播时同步到我的 Twitch IRC 聊天中会很酷!我想通过创建一个机器人来实现这一点,该机器人记录两个聊天中的所有消息并将它们中继到相反的平台。我在编码方面非常业余,这是我第一次使用 JavaScript,但是通过实现 Discord 的 Eris API(此处的文档)和 Twitch 的 TMI API(此处的文档),我能够创建一个功能正常的机器人来记录来自每个平台的消息到一个可变的字符串原语中,但我被困的部分是让机器人然后中继消息。到目前为止,这是我的代码:

//implements API's to communicate with Twitch and Discord respectively.
const tmi = require("tmi.js");
const eris = require('eris');

//creates an instance of the Discord bot.
const bot = new eris.Client('DISCORD BOT TOKEN GOES HERE');

//creates an instance of the Twitch bot.
const client = new tmi.Client({
  connection: {
    secure: true,
    reconnect: true
  },
    identity: {
      username: 'botyoftheyouth ', //subtle channel plug ;)
      password: 'TWITCH IRC BOT AUTH TOKEN GOES HERE'
  },
  channels: [ 'botyoftheyouth' ]
});

//my two variables which hold the messages from each platform.
var twitchmessage ={};
var discordmessage ={};

//Discord bot logs to console on successful startup.
bot.on('ready', () => {
   console.log('Connected and ready.');
});

//both bots connect to their platforms.
client.connect();
bot.connect();

//actions for the bot to take when a user sends a message in my Twitch IRC
client.on('message', (channel, tags, message, self) => {
  
  //the bot ignores its own messages, so as to prevent feedback loop.
  if(self) return;

  //bot creates a String primitive that looks like 
  //"<user> on Twitch says: <message>"
  //and stores it in the twitchmessage variable.
  const user = channel.concat(" on Twitch says: ");
  twitchmessage = user.concat(message);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  //stored message is logged to console for testing.
  console.log(twitchmessage);
});

//actions for the bot to take when a user sends a message in my Discord IRC
bot.on('messageCreate', async (msg) => {

   //bot verifies the message is in the channel I want synced to my twitch, and that
   //the message wasn't sent by itself.
   if(msg.channel.id === 'MY CHANNEL ID' && msg.author.username !== 'botychatsync') {
     //bot creates a String primitive which looks like 
     //"<username> on Discord says: <content>"
     //and stores it in the discordmessage variable
     const auth = msg.author.username;
     const user = auth.concat(" on Discord says: ");
     discordmessage = user.concat(msg.content);

     //stored message is logged to console for testing.
     console.log(discordmessage);
     }
});

我尝试在标有 X 的行上实现下面的函数,但我不知道如何以定义函数中的对象的方式实现它,并且在 twitch 聊天中发送任何消息时都会收到错误消息。

function relaytd(message) {
  bot.msg.channel.createMessage(MY CHANNEL ID, message);
}

我想要完成的事情可能吗?我怎样才能让这个机器人在这个函数中有它需要的定义变量?

4

0 回答 0