diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..926fbe6 --- /dev/null +++ b/config.yaml @@ -0,0 +1 @@ +token: "Token here!" diff --git a/main.go b/main.go new file mode 100644 index 0000000..422c9b8 --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "runtime" + "syscall" + + "github.com/bwmarrin/discordgo" + "github.com/fatih/color" + "github.com/spf13/viper" +) + +var er = color.New(color.BgRed).Add(color.FgBlack) +var suc = color.New(color.BgGreen).Add(color.FgBlack) + +func main() { + fmt.Println("Starting program!") + sc := make(chan os.Signal, 1) + go CLIhandler(&sc) + viper.SetConfigName("config") + viper.AddConfigPath(".") + if runtime.GOOS == "linux" { + viper.AddConfigPath("$HOME/.config/durband/") + viper.AddConfigPath(".") + } + + err := viper.ReadInConfig() + if err != nil { + er.Println("Error reading config file!", err.Error()) + return + } + + token := viper.GetString("token") + + discord, err := discordgo.New("Bot " + token) + if err != nil { + er.Println("Error starting bot!") + } + + err = discord.Open() + if err != nil { + er.Println(("error opening connection to discord,"), err) + return + } + go discord.AddHandler(messageCreate) + suc.Println("Bot is now running. Press CTRL-C to exit or write exit and press enter!") + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + discord.Close() +} + +func CLIhandler(sc *chan os.Signal) { + for { + var input string + fmt.Scanf("%s", &input) + if input == "stop" { + *sc <- os.Kill + } + } +} diff --git a/message.go b/message.go new file mode 100644 index 0000000..e15fbec --- /dev/null +++ b/message.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "strings" + + "github.com/bwmarrin/discordgo" +) + +type UrbanDictionary struct { + List []SomeThingy +} + +type SomeThingy struct { + Definition string + Permalink string + Thumbs_up int + Sound_urls []string + Author string + Word string + Defid int + Current_vote string + Written_on string + Example string + Thumbs_down int +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == s.State.User.ID { + return + } + words := strings.Split(strings.ToLower(m.Content), " ") + if words[0] != "!def" { + return + } + if len(words) == 1 { + s.ChannelMessageSend(m.ChannelID, "<@"+m.Author.ID+"> Could not process the request!") + er.Println("Something wrong with message sent by: " + m.Author.Username) + return + } + resp, err := http.Get("http://api.urbandictionary.com/v0/define?term=" + string(words[1])) + if err != nil { + er.Println("Could not make the request to the api!", err.Error()) + return + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + er.Println("The request to urban dictionary failed! The GET did not respond with statuscode 200.") + return + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + er.Println("Failed to read response body! ", err.Error()) + return + } + var result UrbanDictionary + err = json.Unmarshal(body, &result) + if err != nil { + er.Println("Error decoding json: ", err.Error()) + return + } + if len(result.List) < 1 { + s.ChannelMessageSend(m.ChannelID, "Thats not a word <@"+m.Author.ID+">!") + er.Println("The requested word is not defined in urban dictionary!") + return + } + urban := result.List[0] + s.ChannelMessageSend(m.ChannelID, "Word: "+urban.Word+"\n"+"Definition: "+urban.Definition+"\n"+"Link: "+urban.Permalink) + suc.Println("Definition processed and sent!") +}