/
main.go
118 lines (99 loc) · 2.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
irc "github.com/fluffle/goirc/client"
"github.com/laurent22/toml-go"
"github.com/vevix/twitch-plays/win32"
"time"
)
type Twitch struct {
host string
user string
token string
channel string
}
type Input struct {
user string
message string
key uint16
}
var (
validKeys = map[string]uint16{
"a": win32.VK_X,
"b": win32.VK_Z,
"x": win32.VK_S,
"y": win32.VK_A,
"up": win32.VK_UP,
"left": win32.VK_LEFT,
"down": win32.VK_DOWN,
"right": win32.VK_RIGHT,
}
)
func inputHandler(in <-chan Input) {
for {
input := <-in
fmt.Printf("Handling: %v - %v\n", input.user, input.message)
win32.SendInput(win32.INPUT{
Type: win32.INPUT_KEYBOARD,
Ki: win32.KEYBDINPUT{
WVk: input.key,
DwFlags: win32.KEYEVENTF_KEYDOWN,
},
})
time.Sleep(200 * time.Millisecond)
win32.SendInput(win32.INPUT{
Type: win32.INPUT_KEYBOARD,
Ki: win32.KEYBDINPUT{
WVk: input.key,
DwFlags: win32.KEYEVENTF_KEYUP,
},
})
}
}
func main() {
var p toml.Parser
d := p.ParseFile("config/app.conf")
twitch := Twitch{
host: d.GetString("twitch.host"),
user: d.GetString("twitch.user"),
token: d.GetString("twitch.token"),
channel: "#" + d.GetString("twitch.channel"),
}
in := make(chan Input)
go inputHandler(in)
// make sure emulator window is open
h := win32.FindWindow("DeSmuME", "DeSmuME 0.9.10 x64")
if h == nil {
panic("Couldn't find emulator window.")
}
fmt.Printf("Emulator Window: %v\n", h)
// connect to TwitchTV chat
c := irc.SimpleClient(twitch.user)
c.AddHandler("connected", func(conn *irc.Conn, line *irc.Line) {
c.Join(twitch.channel)
})
quit := make(chan bool)
c.AddHandler("disconnected", func(conn *irc.Conn, line *irc.Line) {
quit <- true
})
c.AddHandler("privmsg", func(conn *irc.Conn, line *irc.Line) {
if line.Args[0] != twitch.channel {
return
}
user, message := line.Nick, line.Args[1]
key, ok := validKeys[message]
if !ok {
return
}
in <- Input{
user: user,
message: message,
key: key,
}
})
if err := c.Connect(twitch.host, twitch.token); err != nil {
panic("Couldn't connect to TwitchTV.")
}
// keep the connection alive
<-quit
}