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



我是一个小twitch主播/内容创建者,我很快就会推出我的Discord,我想如果我在我的Discord中有一个IRC频道同步到我的twitch IRC聊天,那将是很酷的!我想通过创建一个机器人来实现这一点,它记录了聊天中的所有消息,并将它们转发到相反的平台。我在编码方面相当业余,这是我第一次使用JavaScript,但是通过实现Discord的Eris API(这里的文档)和Twitch的TMI API(这里的文档),我能够创建一个功能bot,它将来自每个平台的消息记录到一个变量String原语中,但我卡住的部分是让bot然后中继消息。下面是我到目前为止的代码:

//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);
}

我想要完成的是可能的吗?我怎么能让这个机器人有定义的变量,它需要在这个函数?

我很高兴你有兴趣创建一个机器人同步你的Twitch和Discord聊天。😊我看了你的代码,我想我找到了一个可能的解决方案。根据Eris文档,您可以使用createMessage方法使用Eris向Discord通道发送消息。语法为:

bot.createMessage(channelID, content);

,其中channelID是要将消息发送到的通道ID, content是消息内容。因此,在您的情况下,您可以尝试这样做:

//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);
//stored message is logged to console for testing.
console.log(twitchmessage);
//bot sends the twitchmessage to the Discord channel with ID 'MY CHANNEL ID'
bot.createMessage('MY CHANNEL ID', twitchmessage);
});

同样,您可以对Discord消息执行相同的操作:

//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);
//bot sends the discordmessage to the Twitch channel with name 'botyoftheyouth'
client.say('botyoftheyouth', discordmessage);
}
});

我希望这对你的项目有帮助。如果您有任何其他问题或反馈,请告诉我。👍

最新更新