-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
177 lines (160 loc) · 3.95 KB
/
main.go
File metadata and controls
177 lines (160 loc) · 3.95 KB
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
)
const (
configPath = "config.toml"
)
var (
k = koanf.New(".")
parser = toml.Parser()
)
func main() {
if err := k.Load(file.Provider(configPath), toml.Parser()); err != nil {
fmt.Println(err)
return
}
if k.String("discord.token") == "" {
fmt.Println("No discord token found")
return
}
if k.String("man.server") == "" {
fmt.Println("No manpage server url found")
return
}
dg, err := discordgo.New("Bot " + k.String("discord.token"))
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
dg.Identify.Intents |= discordgo.IntentsGuildMessages
dg.AddHandler(messageCreate)
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
// Split message by spaces
words := strings.Split(m.Content, " ")
// Check if first word is man!
if words[0] != "man!" {
return
}
fmt.Println(m.Author.Username + "#" + m.Author.Discriminator + ": " + m.Content)
if len(words) == 2 {
manpage(s, m, "0", words[1])
}
if len(words) == 3 {
manpage(s, m, words[1], words[2])
}
}
func getManPage(section string, command string) (status int, body string, err error) {
resp, err := http.Get(k.String("man.server") + section + "/" + command)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return 0, "", err
}
status = resp.StatusCode
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return status, string(bodyBytes), nil
}
func manpage(s *discordgo.Session, m *discordgo.MessageCreate, section string, command string) {
body := ""
err := error(nil)
status := 0
if section == "0" {
for i := 1; i < 10; i++ {
status, body, _ = getManPage(strconv.Itoa(i), command)
if status == 200 {
section = strconv.Itoa(i)
break
}
}
if status != 200 {
s.ChannelMessageSend(m.ChannelID, "No manpage found for "+command)
return
}
} else {
status, body, err = getManPage(section, command)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Internal server error")
return
}
if status != 200 {
switch status {
case 404:
s.ChannelMessageSend(m.ChannelID, "No manpage found for "+section+" "+command)
return
case 500:
fmt.Println("Internal server error" + string(body))
s.ChannelMessageSend(m.ChannelID, "Internal server error")
return
default:
fmt.Println("Unknown error code: " + string(status) + " " + string(body))
s.ChannelMessageSend(m.ChannelID, "Unknown error")
return
}
}
}
//split it by zero width space
sections := strings.SplitAfter(string(body), "\u200b")
// Make an embed with each word as its own embed and send to discord
embeds := []*discordgo.MessageEmbed{}
for _, section := range sections {
// split on the first newline
split := strings.SplitN(section, "\n", 2)
title := split[0]
if len(split) == 1 {
continue
}
description := split[1]
if len(description) > 4096 {
for len(description) > 4096 {
embeds = append(embeds, &discordgo.MessageEmbed{
Title: title,
Description: description[:4096],
})
description = description[4096:]
}
}
embeds = append(embeds, &discordgo.MessageEmbed{
Title: title,
Description: description,
})
}
// Make thread
thread, err := s.MessageThreadStart(m.ChannelID, m.ID, "man "+section+" "+command, 60)
if err != nil {
fmt.Println(err)
return
}
for _, embed := range embeds {
_, err := s.ChannelMessageSendEmbed(thread.ID, embed)
if err != nil {
fmt.Println(err)
}
}
}