1
+ const tmi = require ( 'tmi.js' ) ;
2
+
3
+ // Define configuration options
4
+ const opts = {
5
+ identity : {
6
+ username : "BOT_USERNAME" ,
7
+ password : "OAUTH_TOKEN"
8
+ } ,
9
+ channels : [
10
+ "CHANNEL_NAME"
11
+ ]
12
+ } ;
13
+ // Create a client with our options
14
+ const client = new tmi . client ( opts ) ;
15
+
16
+ // Register our event handlers (defined below)
17
+ client . on ( 'message' , onMessageHandler ) ;
18
+ client . on ( 'connected' , onConnectedHandler ) ;
19
+
20
+ // Connect to Twitch:
21
+ client . connect ( ) ;
22
+
23
+ // Called every time a message comes in
24
+ function onMessageHandler ( target , context , msg , self ) {
25
+ if ( self ) {
26
+ return ;
27
+ } // Ignore messages from the bot
28
+
29
+ // Remove whitespace from chat message
30
+ const commandName = msg . trim ( ) ;
31
+
32
+ // If the command is known, let's execute it
33
+ if ( commandName === '!dice' ) {
34
+ const num = rollDice ( ) ;
35
+ client . say ( target , `You rolled a ${ num } ` ) ;
36
+ console . log ( `* Executed ${ commandName } command` ) ;
37
+ } else {
38
+ console . log ( `* Unknown command ${ commandName } ` ) ;
39
+ }
40
+
41
+ // GAMEGG
42
+ if ( commandName === '!gamegg' ) {
43
+ const num = rollDice ( ) ;
44
+ client . say ( target , `https://gamegg.ir` ) ;
45
+ console . log ( `* Executed ${ commandName } command` ) ;
46
+ } else {
47
+ console . log ( `* Unknown command ${ commandName } ` ) ;
48
+ }
49
+
50
+ // Discord
51
+ if ( commandName === '!discord' ) {
52
+ const num = rollDice ( ) ;
53
+ client . say ( target , `https://discord.gg/2JjvhAk` ) ;
54
+ console . log ( `* Executed ${ commandName } command` ) ;
55
+ } else {
56
+ console . log ( `* Unknown command ${ commandName } ` ) ;
57
+ }
58
+ }
59
+ // Function called when the "dice" command is issued
60
+ function rollDice ( ) {
61
+ const sides = 6 ;
62
+ return Math . floor ( Math . random ( ) * sides ) + 1 ;
63
+ }
64
+ // Called every time the bot connects to Twitch chat
65
+ function onConnectedHandler ( addr , port ) {
66
+ console . log ( `* Connected to ${ addr } :${ port } ` ) ;
67
+ }
0 commit comments