|
| 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