forked from RickvanLoo/discord-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (101 loc) · 2.49 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
// This file provides a basic "quick start" example of using the Discordgo
// package to connect to Discord using the New() helper function.
package main
import (
"log"
"regexp"
"github.com/Rivalo/discord-cli/DiscordState"
"github.com/chzyer/readline"
)
//Global Message Types
const (
ErrorMsg = "Error"
InfoMsg = "Info"
HeaderMsg = "Head"
TextMsg = "Text"
)
//Version is current version const
const Version = "v0.3.0-DEVELOP"
//Session is global Session
var Session *DiscordState.Session
//State is global State
var State *DiscordState.State
//MsgType is a string containing global message type
type MsgType string
func main() {
//Initialize Config
GetConfig()
CheckState()
Clear()
Msg(HeaderMsg, "discord-cli - version: %s\n\n", Version)
//NewSession
Session = DiscordState.NewSession(Config.Username, Config.Password) //Please don't abuse
err := Session.Start()
if err != nil {
log.Println("Session Failed")
log.Fatalln(err)
}
//Attach New Window
InitWindow()
//Attach Even Handlers
State.Session.DiscordGo.AddHandler(newMessage)
//Setup Readline
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
UniqueEditLine: true,
})
defer rl.Close()
log.SetOutput(rl.Stderr()) // let "log" write to l.Stderr instead of os.Stderr
//Start Listening
for {
line, _ := rl.Readline()
//QUIT
if line == ":q" {
break
}
//Parse Commands
line = ParseForCommands(line)
line = ParseForMentions(line)
if line != "" {
State.Session.DiscordGo.ChannelMessageSend(State.Channel.ID, line)
}
}
return
}
//InitWindow creates a New CLI Window
func InitWindow() {
SelectGuildMenu()
SelectChannelMenu()
State.Enabled = true
ShowContent()
}
//ShowContent shows defaulth Channel content
func ShowContent() {
Clear()
Header()
if Config.MessageDefault {
State.RetrieveMessages(Config.Messages)
PrintMessages(Config.Messages)
}
}
//ParseForMentions parses input string for mentions
func ParseForMentions(line string) string {
r, err := regexp.Compile("\\@\\w+")
if err != nil {
Msg(ErrorMsg, "Regex Error: ", err)
}
lineByte := r.ReplaceAllFunc([]byte(line), ReplaceMentions)
return string(lineByte[:])
}
//ReplaceMentions replaces mentions to ID
func ReplaceMentions(input []byte) []byte {
var OutputString string
SizeByte := len(input)
InputString := string(input[1:SizeByte])
if Member, ok := State.Members[InputString]; ok {
OutputString = "<@" + Member.User.ID + ">"
} else {
OutputString = "@" + InputString
}
return []byte(OutputString)
}