~kris/9p

disco

ref: 37fd82dcf1b268814517f1cd97335d6cd517e30c disco/main.go -rw-r--r-- 4.1 KiB
37fd82dc — Sean Hinchee update readme with factotum format 7 years ago
                                                                                
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 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 (
	"bitbucket.org/henesy/disco/DiscordState"
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"regexp"
	"strings"
)

//Global Message Types
const (
	ErrorMsg  = "Error"
	InfoMsg   = "Info"
	HeaderMsg = "Head"
	TextMsg   = "Text"
)

//Version is current version const
const Version = "1.0"

//Session is global Session
var Session *DiscordState.Session

//State is global State
var State *DiscordState.State

//UserChannels is global User Channels

//MsgType is a string containing global message type
type MsgType string

var hideTimeStamp = flag.Bool("t", false, "Hide timestamps in channel log")
var enableNotify = flag.Bool("n", false, "Enable notifications")
var notifyFlag = flag.String("w", "10,10,260,90", "Dimensions to pass through to statusmsg")

func main() {

	flag.Parse()
	if flag.Lookup("h") != nil {
		flag.Usage()
		os.Exit(1)
	}
	if flag.Lookup("w") != nil {
		*notifyFlag = fmt.Sprintf("-w %s", *notifyFlag)
	}
	//Initialize Config
	GetConfig()
	CheckState()
	Msg(HeaderMsg, "disco 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)
	//State.Session.DiscordGo.AddHandler(newReaction)

	//defer rl.Close()
	log.SetOutput(os.Stderr) // let "log" write to l.Stderr instead of os.Stderr
	State.Session.DiscordGo.UpdateStatus(0, "Plan 9")

	//Start Listening
	reader := bufio.NewReader(os.Stdin)
	for {
		//fmt.Print("> ")
		//line, _ := rl.Readline()
		line, err := reader.ReadString('\n')
		
		if err == io.EOF {
			break
		}

		// ```
		if strings.HasPrefix(line, "```") {
			for {
				subline, _ := reader.ReadString('\n')
				line += subline
				if strings.Index(subline,  "```") != -1 {
					break
				}
			}
			_, err := State.Session.DiscordGo.ChannelMessageSend(State.Channel.ID, line)
			if err != nil {
				fmt.Printf("Error: %s\n", err)
			}
			continue
		}

		n := len(line)
		if n > 0 {
			line = line[:n-1]
		}

		//QUIT
		if line == ":q" {
			break
		}

		//Parse Commands
		line = ParseForCommands(line)

		line = ParseForMentions(line)

		if line != "" {
			_, err := State.Session.DiscordGo.ChannelMessageSend(State.Channel.ID, line)
			if err != nil {
				fmt.Print("Error: ", err, "\n")
			}
		}
	}

	return
}

//InitWindow creates a New CLI Window
func InitWindow() {
	SelectGuildMenu()
	if State.Channel == nil {
		SelectChannelMenu()
	}
	State.Enabled = true
	ShowContent()
}

//ShowContent shows default Channel content
func ShowContent() {
	Header()
	if Config.LoadBacklog {
		State.RetrieveMessages(Config.Messages)
		PrintMessages(Config.Messages)
	}
}

//ShowEmptyContent shows an empty channel
func ShowEmptyContent() {
	Header()

}

//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.ReplaceAllStringFunc(line, ReplaceMentions)

	return lineByte
}

//ReplaceMentions replaces mentions to ID 
func ReplaceMentions(input string) string {
	// Check for guild members that match
	for _, member := range State.Guild.Members {
		if strings.HasPrefix(member.Nick, input[1:]) {
			return member.User.Mention()
		}
		if strings.HasPrefix(member.User.Username, input[1:]) {
			return member.User.Mention()
		}
	}
	// Walk all PM channels
	userChannels, err := Session.DiscordGo.UserChannels()
	if err != nil {
		return input
	}
	for _, channel := range userChannels {
		for _, recipient := range channel.Recipients {
			if strings.HasPrefix(input[1:], recipient.Username) {
				return recipient.Mention()
			}
		}
	}
	return input
}

//Parse for guild-specific emoji
func ParseForEmoji(line string) string {
	r, err := regexp.Compile("<(:\\w+:)[0-9]+>")
	if err != nil {
		Msg(ErrorMsg, "Regex Error: ", err)
	}
	return r.ReplaceAllString(line, "$1")
}