Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const tmi = require('tmi.js')
- const haikudos = require('haikudos')
- var net = require('net');
- // Valid commands start with:
- let commandPrefix = '!'
- // Define configuration options:
- let opts = {
- identity: {
- username: 'BOT_NAME',
- password: 'oauth:' + 'YOUR_OAUTH_GOES_HERE'
- },
- channels: [
- 'deadmau5'
- ]
- }
- // These are the commands the bot knows (defined below):
- let knownCommands = { echo, haiku }
- // Function called when the "echo" command is issued:
- function echo (target, context, params) {
- // If there's something to echo:
- if (params.length) {
- // Join the params into a string:
- var msg = params.join(' ')
- // Interrupt attempted slash and dot commands:
- if (msg.charAt(0) == '/' || msg.charAt(0) == '.') {
- //msg = 'Nice try...'
- }
- // Send it back to the correct place:
- //sendMessage(target, context, msg)
- } else { // Nothing to echo
- console.log(`* Nothing to echo`)
- }
- }
- // Function called when the "haiku" command is issued:
- function haiku (target, context) {
- // Generate a new haiku:
- haikudos((newHaiku) => {
- // Split it line-by-line:
- newHaiku.split('\n').forEach((h) => {
- // Send each line separately:
- //sendMessage(target, context, h)
- })
- })
- }
- // Helper function to send the correct type of message:
- function sendMessage (target, context, message) {
- if (context['message-type'] === 'whisper') {
- client.whisper(target, message)
- } else {
- client.say(target, message)
- }
- }
- // Create a client with our options:
- let client = new tmi.client(opts)
- // Register our event handlers (defined below):
- client.on('message', onMessageHandler)
- client.on('connected', onConnectedHandler)
- client.on('disconnected', onDisconnectedHandler)
- // Connect to Twitch:
- client.connect()
- // Called every time a message comes in:
- function onMessageHandler (target, context, msg, self) {
- if (self) { return } // Ignore messages from the bot
- // This isn't a command since it has no prefix:
- if (msg.substr(0, 1) !== commandPrefix) {
- //var twmsg = `[${target} (${context['message-type']})] ${context.username}: ${msg}`
- var twmsg = `${context.username}: ${msg}`
- var client = new net.Socket();
- client.setNoDelay();
- client.connect(1337, 'localhost', function(){
- //console.log('Connected to TD Server')
- client.write(twmsg + "\r\n")
- });
- client.on('error',()=>{
- console.log("An error has occured");
- console.log("Open TouchDesigner Server before starting.");
- });
- client.on('data', (data) => {
- console.log(data.toString());
- client.end();
- });
- client.on('end', () => {
- console.log('disconnected from server');
- });
- console.log(twmsg)
- return
- }
- // Split the message into individual words:
- const parse = msg.slice(1).split(' ')
- // The command name is the first (0th) one:
- const commandName = parse[0]
- // The rest (if any) are the parameters:
- const params = parse.splice(1)
- // If the command is known, let's execute it:
- if (commandName in knownCommands) {
- // Retrieve the function by its name:
- const command = knownCommands[commandName]
- // Then call the command with parameters:
- command(target, context, params)
- console.log(`* Executed ${commandName} command for ${context.username}`)
- } else {
- console.log(`* Unknown command ${commandName} from ${context.username}`)
- }
- }
- // Called every time the bot connects to Twitch chat:
- function onConnectedHandler (addr, port) {
- console.log(`* Connected to ${addr}:${port}`)
- }
- // Called every time the bot disconnects from Twitch:
- function onDisconnectedHandler (reason) {
- console.log(`Disconnected: ${reason}`)
- process.exit(1)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement