Skip to content

Commit 94534b2

Browse files
committed
init
0 parents  commit 94534b2

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
bin/
18+
19+
config.toml
20+
log

config.toml.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[discord]
2+
token = ""

go.mod

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module github.com/BasedDevelopment/manbot
2+
3+
go 1.20
4+
5+
require (
6+
github.com/bwmarrin/discordgo v0.27.1
7+
github.com/knadh/koanf/parsers/toml v0.1.0
8+
github.com/knadh/koanf/providers/file v0.1.0
9+
github.com/knadh/koanf/v2 v2.0.1
10+
)
11+
12+
require (
13+
github.com/fsnotify/fsnotify v1.6.0 // indirect
14+
github.com/gorilla/websocket v1.4.2 // indirect
15+
github.com/knadh/koanf/maps v0.1.1 // indirect
16+
github.com/mitchellh/copystructure v1.2.0 // indirect
17+
github.com/mitchellh/mapstructure v1.5.0 // indirect
18+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
19+
github.com/pelletier/go-toml v1.9.5 // indirect
20+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
21+
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
22+
)

go.sum

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
2+
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
5+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
6+
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
7+
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
8+
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
9+
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
10+
github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI=
11+
github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18=
12+
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
13+
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
14+
github.com/knadh/koanf/v2 v2.0.1 h1:1dYGITt1I23x8cfx8ZnldtezdyaZtfAuRtIFOiRzK7g=
15+
github.com/knadh/koanf/v2 v2.0.1/go.mod h1:ZeiIlIDXTE7w1lMT6UVcNiRAS2/rCeLn/GdLNvY1Dus=
16+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
17+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
18+
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
19+
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
20+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
21+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
22+
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
23+
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
24+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
25+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
26+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
27+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
28+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
29+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
30+
golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY=
31+
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
33+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
34+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
35+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

main.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"os"
8+
"os/signal"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/bwmarrin/discordgo"
13+
"github.com/knadh/koanf/parsers/toml"
14+
"github.com/knadh/koanf/providers/file"
15+
"github.com/knadh/koanf/v2"
16+
)
17+
18+
const (
19+
configPath = "config.toml"
20+
)
21+
22+
var (
23+
k = koanf.New(".")
24+
parser = toml.Parser()
25+
)
26+
27+
func main() {
28+
if err := k.Load(file.Provider(configPath), toml.Parser()); err != nil {
29+
fmt.Println(err)
30+
return
31+
}
32+
if k.String("discord.token") == "" {
33+
fmt.Println("No discord token found")
34+
return
35+
}
36+
dg, err := discordgo.New("Bot " + k.String("discord.token"))
37+
if err != nil {
38+
fmt.Println("error creating Discord session,", err)
39+
return
40+
}
41+
dg.Identify.Intents |= discordgo.IntentsGuildMessages
42+
dg.AddHandler(messageCreate)
43+
44+
err = dg.Open()
45+
if err != nil {
46+
fmt.Println("error opening connection,", err)
47+
return
48+
}
49+
fmt.Println("Bot is now running. Press CTRL-C to exit.")
50+
51+
sc := make(chan os.Signal, 1)
52+
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
53+
<-sc
54+
dg.Close()
55+
}
56+
57+
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
58+
if m.Author.ID == s.State.User.ID {
59+
return
60+
}
61+
// Split message by spaces
62+
words := strings.Split(m.Content, " ")
63+
// Check if first word is manpage
64+
if len(words) != 3 || words[0] != "!man" {
65+
return
66+
}
67+
manpage(s, m, words[1], words[2])
68+
69+
}
70+
71+
func manpage(s *discordgo.Session, m *discordgo.MessageCreate, section string, command string) {
72+
resp, _ := http.Get("http://localhost:3014/" + section + "/" + command)
73+
defer resp.Body.Close()
74+
body, _ := ioutil.ReadAll(resp.Body)
75+
// check err code
76+
if resp.StatusCode != 200 {
77+
s.ChannelMessageSend(m.ChannelID, "Error: "+string(body))
78+
return
79+
}
80+
//split it by new ZWSC
81+
words := strings.SplitAfter(string(body), "\n\n")
82+
83+
// Make an embed with each word as its own embed and send to discord
84+
embeds := []*discordgo.MessageEmbed{}
85+
for _, word := range words {
86+
// split on the first newline
87+
split := strings.SplitN(word, "\n", 2)
88+
title := split[0]
89+
description := split[1]
90+
embeds = append(embeds, &discordgo.MessageEmbed{
91+
Title: title,
92+
Description: description,
93+
})
94+
}
95+
// Make thread
96+
thread, err := s.MessageThreadStart(m.ChannelID, m.ID, "man "+section+" "+command, 60)
97+
if err != nil {
98+
fmt.Println(err)
99+
}
100+
101+
for _, embed := range embeds {
102+
s.ChannelMessageSendEmbed(thread.ID, embed)
103+
}
104+
105+
}

0 commit comments

Comments
 (0)