微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于同步 Discord 频道和 Twitch 聊天的 JavaScript 机器人

如何解决用于同步 Discord 频道和 Twitch 聊天的 JavaScript 机器人

我是一名小型 Twitch 主播/内容创作者,即将推出我的 discord,我认为如果我的 discord 中有一个 IRC 频道在我直播时同步到我的 Twitch IRC 聊天会很酷!我想通过创建一个机器人来实现这一点,该机器人记录两个聊天中的所有消息并将它们中继到相反的平台。我对编码非常业余,这是我第一次使用 JavaScript,但是通过为 discord(documentation here) 实现 Eris API 和为 Twitch(documentation here) 实现 TMI API,我能够创建一个功能强大的机器人,它将来自每个平台的消息记录到一个可变的 String 原语中,但我遇到的问题是让机器人转发消息。到目前为止,这是我的代码

//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 的行上实现下面的函数,但我不知道如何以函数中的对象都已定义的方式实现它,并且在发送任何消息时收到错误抽搐聊天。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。