-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (136 loc) · 3.72 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
"log"
"flag"
"github.com/bwmarrin/discordgo"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
var verbosePtr bool
type Conf struct {
DiscordToken string `json:"discordToken"`
TelegramToken string `json:"telegramToken"`
TelegramChatID int64 `json:"telegramChatID"`
}
func createLogin() {
var email, pass, token string
// TODO: Check if works with passwords with spaces.
// https://stackoverflow.com/q/20895552
fmt.Println("Enter email:")
fmt.Scanln(&email)
// TODO: Hide password input.
// https://stackoverflow.com/q/2137357
fmt.Println("Enter password:")
fmt.Scanln(&pass)
dg, err := discordgo.New(email, pass)
if err != nil {
fmt.Println("Error creating Discord session using email/pass,", err)
os.Exit(1)
}
// Obtaining authentication token for Discord account.
token = dg.Token
input := Conf{DiscordToken: token}
data, err := json.MarshalIndent(input, "", "\t")
if err != nil {
fmt.Println("Error encoding JSON,", err)
os.Exit(1)
}
err = ioutil.WriteFile("conf.json", data, 0600)
if err != nil {
fmt.Println("Error writing JSON file,", err)
os.Exit(1)
}
}
func retrieveFromJSON() (string, string, int64) {
var conf Conf
raw, err := ioutil.ReadFile("/etc/discord-notify.json")
if err != nil {
fmt.Println("Error reading JSON file,", err)
os.Exit(1)
}
err = json.Unmarshal(raw, &conf)
if err != nil {
fmt.Println("Error decoding JSON,", err)
os.Exit(1)
}
log.Println(conf.DiscordToken, conf.TelegramToken, conf.TelegramChatID)
return conf.DiscordToken, conf.TelegramToken, conf.TelegramChatID
}
func processMessages(discordToken string) {
dg, err := discordgo.New(discordToken)
if err != nil {
fmt.Println("Error creating Discord session using token,", err)
os.Exit(1)
}
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection,", err)
os.Exit(1)
}
// Register the directRecieve func as a callback for MessageCreate events.
dg.AddHandler(directRecieve)
fmt.Println("Bot is now running. Press CTRL-C to exit.")
// Channel to recieve signal notifications.
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sigChan
dg.Close()
}
func directRecieve(s *discordgo.Session, m *discordgo.MessageCreate) {
var telegramToken string
var telegramChatID int64
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
fmt.Println("Error", err)
os.Exit(1)
}
if m.Author.ID == s.State.User.ID {
return
}
if channel.Type == discordgo.ChannelTypeDM {
log.Println(m.Author.Username)
log.Println(m.Content)
log.Println(time.Now().Format(time.RFC850))
_, telegramToken, telegramChatID = retrieveFromJSON()
telegramNotify(m.Author.Username, telegramToken, telegramChatID)
} else {
return
}
}
func telegramNotify(message, telegramToken string, telegramChatID int64) {
bot, err := tgbotapi.NewBotAPI(telegramToken)
if err != nil {
fmt.Println("Error authenticating bot,", err)
os.Exit(1)
}
request := tgbotapi.NewMessage(telegramChatID, message)
bot.Send(request)
}
func main() {
var discordToken string
verbosePtr := flag.Bool("verbose", false, "Print verbose log output")
flag.Parse()
log.SetOutput(ioutil.Discard)
if *verbosePtr {
log.SetOutput(os.Stderr)
}
_, err := os.Stat("conf.json")
if os.IsNotExist(err) {
fmt.Println("File doesn't exist, creating file...")
createLogin()
discordToken, _, _ = retrieveFromJSON()
} else if !os.IsNotExist(err) {
fmt.Println("File exists, accessing...")
discordToken, _, _ = retrieveFromJSON()
} else {
fmt.Println("File stat error,", err)
}
processMessages(discordToken)
}