Skip to content

Commit

Permalink
Created Project
Browse files Browse the repository at this point in the history
  • Loading branch information
bennystarfighter authored Feb 12, 2019
1 parent 26ece85 commit 4457878
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token: "Token here!"
62 changes: 62 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}
72 changes: 72 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -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!")
}

0 comments on commit 4457878

Please sign in to comment.