I'm a small twitch streamer / content creator who's launching my Discord soon, and thought it would be cool if I had an IRC channel in my Discord synced up to my Twitch IRC chat when I livestream! I wanted to accomplish this by creating a bot which logs all messages in both chats and relays them to the opposite platform. I'm pretty amateur at coding, and this is my first time using JavaScript, but by implementing the Eris API for Discord(documentation here) and the TMI API for Twitch(documentation here), I was able to create a functioning bot which logs messages from each platform into a variable String primitive, but the part where I'm stuck is in getting the bot to then relay the message. Here's my code so far:
//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);
}
});
I tried implementing the function below on the line marked with X's, but I don't know how to implement it in a way where the objects in the function are all defined, and receive an error upon any message being sent in the twitch chat.
function relaytd(message) {
bot.msg.channel.createMessage(MY CHANNEL ID, message);
}
Is what i'm trying to accomplish possible? How can i get this bots to have the defined variables it needs in this function?