Created
February 1, 2021 03:33
-
-
Save wilik16/be314180ad38452b3544497908881e54 to your computer and use it in GitHub Desktop.
Giveaway bot sample for twitch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tmi = require('tmi.js'); | |
// Define configuration options | |
const opts = { | |
identity: { | |
username: <BOT_USERNAME>, | |
password: <OAUTH_TOKEN> | |
}, | |
channels: [ | |
<CHANNEL_NAME> | |
] | |
}; | |
// Create a client with our options | |
const client = new tmi.client(opts); | |
// Register our event handlers (defined below) | |
client.on('message', onMessageHandler); | |
client.on('connected', onConnectedHandler); | |
// Connect to Twitch: | |
client.connect(); | |
var participants = []; | |
// Called every time a message comes in | |
function onMessageHandler (target, context, msg, self) { | |
if (self) { return; } // Ignore messages from the bot | |
// Remove whitespace from chat message | |
const commandName = msg.trim(); | |
// If the command is known, let's execute it | |
if (commandName === '!giveaway') { | |
const participant = context.username; | |
if (participants.indexOf(participant) == -1) { | |
participants.push(participant); | |
client.say(target, `${participant} joined the giveaway`); | |
} else { | |
client.say(target, `${participant} have already registered before`); | |
} | |
console.log(`* Executed ${commandName} command`); | |
} else if (commandName === `!mainkan` && context.username === `wiuwwiuw`) { | |
client.say(target, `Pemenangnya adalah ${rollDice()}`); | |
} else { | |
console.log(`* Unknown command ${commandName}`); | |
} | |
} | |
// Function called when the "dice" command is issued | |
function rollDice () { | |
return participants[Math.floor(Math.random() * participants.length)]; | |
} | |
// Called every time the bot connects to Twitch chat | |
function onConnectedHandler (addr, port) { | |
console.log(`* Connected to ${addr}:${port}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment