Skip to content

Commit

Permalink
feat: refactor the codebase to make it easier to contribute to
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Apr 2, 2020
1 parent a8e9159 commit 0c38a04
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 105 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
*~
*#
.#*
116 changes: 116 additions & 0 deletions chaos-bot/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"sort"
"strings"

"github.com/bwmarrin/discordgo"
yaml "gopkg.in/yaml.v2"
)

type commandFunc func(s *discordgo.Session, m *discordgo.MessageCreate) error

var commands map[string]commandFunc

func init() {
//
// register custom commands
//
commands = map[string]commandFunc{
"!history": doHistory,
"!help": doHelp,
"!il-est-pas-quelle-heure": doIlEstPasQuelleHeure,
"!bite": doBite,
}
// FIXME: !pause 5min
// FIXME: !bot-stats
// FIXME: !recettator
// FIXME: !pipotron
// FIXME: !blague

//
// generate commands based on `replies.yml`
//
var repliesYaml map[string][]string
yamlFile, err := ioutil.ReadFile("replies.yml")
if err != nil {
log.Fatalf("yamlFile.Get: %v ", err)
}
err = yaml.Unmarshal(yamlFile, &repliesYaml)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
for key, msgs := range repliesYaml {
commands["!"+key] = genericRepliesYaml(msgs)
}
}

func doHistory(s *discordgo.Session, m *discordgo.MessageCreate) error {
resp, err := http.Get("http://radio-admin.casse-tete.solutions/?action=infos&format=json")
if err != nil {
return err
}
var parsed GetInfosResponse

err = json.NewDecoder(resp.Body).Decode(&parsed)
if err != nil {
return err
}

//fmt.Println(godev.PrettyJSON(parsed))
msg := fmt.Sprintf("current: %s\n", parsed.Current.Pretty())
msg += "\nhistory:\n"
for _, history := range parsed.History {
pretty := history.Pretty()
if strings.TrimSpace(pretty) == "-" {
continue
}
msg += fmt.Sprintf(" - %s\n", pretty)
}
s.ChannelMessageSend(m.ChannelID, msg)
return nil
}

func doHelp(s *discordgo.Session, m *discordgo.MessageCreate) error {
s.ChannelMessageSend(m.ChannelID, "coucou")
keys := make([]string, len(commands))
i := 0
for key := range commands {
keys[i] = key
i++
}

sort.Strings(keys)
out := strings.Join(keys, ", ")
s.ChannelMessageSend(m.ChannelID, out)
return nil
}

func doBite(s *discordgo.Session, m *discordgo.MessageCreate) error {
s.ChannelMessageSend(m.ChannelID, "B"+strings.Repeat("=", rand.Intn(42)+1)+"D")
return nil
}

func doIlEstPasQuelleHeure(s *discordgo.Session, m *discordgo.MessageCreate) error {
out := fmt.Sprintf("%02d:%02d",
rand.Intn(24),
rand.Intn(60),
)
s.ChannelMessageSend(m.ChannelID, out)
return nil
}

// see replies.yml
func genericRepliesYaml(msgs []string) commandFunc {
return func(s *discordgo.Session, m *discordgo.MessageCreate) error {
msg := msgs[rand.Intn(len(msgs))]
s.ChannelMessageSend(m.ChannelID, msg)
return nil
}
}
1 change: 1 addition & 0 deletions chaos-bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.14

require (
github.com/bwmarrin/discordgo v0.20.2
gopkg.in/yaml.v2 v2.2.8
moul.io/godev v1.6.0
)
3 changes: 3 additions & 0 deletions chaos-bot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGm
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22 h1:0efs3hwEZhFKsCoP8l6dDB1AZWMgnEl3yWXWRZTOaEA=
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
moul.io/godev v1.5.0 h1:r3z2Lf6LNd8lRtc2uVzAHThzjUuA4cPzWnUTARWHBJQ=
moul.io/godev v1.5.0/go.mod h1:5lgSpI1oH7xWpLl2Ew/Nsgk8DiNM6FzN9WV9+lgW8RQ=
moul.io/godev v1.6.0 h1:ms1aI6o9k+PhmMdTR7Aw5iDHPu56xtnmrUgdfLKPspc=
moul.io/godev v1.6.0/go.mod h1:5lgSpI1oH7xWpLl2Ew/Nsgk8DiNM6FzN9WV9+lgW8RQ=
115 changes: 10 additions & 105 deletions chaos-bot/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"sort"
"strings"
"syscall"

"github.com/bwmarrin/discordgo"
Expand All @@ -33,7 +28,7 @@ func run(token string) error {
return err
}

dg.AddHandler(messageCreate)
dg.AddHandler(onMessageCreate)

if err = dg.Open(); err != nil {
return err
Expand All @@ -48,109 +43,19 @@ func run(token string) error {
return nil
}

func sendError(s *discordgo.Session, m *discordgo.MessageCreate, err error) {
log.Printf("ERROR: %v", err)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("ERROR!"))
}

func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// avoid loop
if m.Author.ID == s.State.User.ID {
return
}

switch {
case m.Content == "!history":
resp, err := http.Get("http://radio-admin.casse-tete.solutions/?action=infos&format=json")
if err != nil {
sendError(s, m, err)
break
}
var parsed GetInfosResponse

err = json.NewDecoder(resp.Body).Decode(&parsed)
if err != nil {
sendError(s, m, err)
break
}
command, found := commands[m.Content] // FIXME: split the content and support args
if !found {
return
}

//fmt.Println(godev.PrettyJSON(parsed))
msg := fmt.Sprintf("current: %s\n", parsed.Current.Pretty())
msg += "\nhistory:\n"
for _, history := range parsed.History {
pretty := history.Pretty()
if strings.TrimSpace(pretty) == "-" {
continue
}
msg += fmt.Sprintf(" - %s\n", pretty)
}
s.ChannelMessageSend(m.ChannelID, msg)
case m.Content == "!help":
commands := []string{
"!history",
"!manfred",
"!il-est-pas-quelle-heure",
"!discord",
"!radio",
"!zoom",
"!coucou",
"!pouet",
"!podcast",
"!calendrier",
"!bite",
"!ucpj",
"!ultreme",
"!soundcloud",
}
sort.Strings(commands)
out := strings.Join(commands, ", ")
s.ChannelMessageSend(m.ChannelID, out)
case m.Content == "!soundcloud":
s.ChannelMessageSend(m.ChannelID, "https://soundcloud.com/ultreme-reporters")
case m.Content == "!ultreme":
s.ChannelMessageSend(m.ChannelID, "https://ultre.me")
case m.Content == "!calendrier":
s.ChannelMessageSend(m.ChannelID, "https://calendrier.ultre.me")
case m.Content == "!pouet":
s.ChannelMessageSend(m.ChannelID, "https://calendrier.ultre.me/2019/pouet/")
case m.Content == "!bite":
s.ChannelMessageSend(m.ChannelID, "B"+strings.Repeat("=", rand.Intn(42)+1)+"D")
case m.Content == "!manfred":
msgs := []string{
"c'est ce qu'elles disent toutes",
"plus tu cours moins vite, moins t'avances plus vite",
}
msg := msgs[rand.Intn(len(msgs))]
s.ChannelMessageSend(m.ChannelID, msg)
case m.Content == "!il-est-pas-quelle-heure":
out := fmt.Sprintf("%d%d:%d%d",
rand.Intn(3),
rand.Intn(10),
rand.Intn(6),
rand.Intn(10),
)
s.ChannelMessageSend(m.ChannelID, out)
case m.Content == "!discord":
s.ChannelMessageSend(m.ChannelID, "https://ultre.me/discord")
case m.Content == "!radio":
s.ChannelMessageSend(m.ChannelID, "http://salutcestcool.com/radio")
case m.Content == "!ucpj":
s.ChannelMessageSend(m.ChannelID, "https://airtable.com/shrIAoOICeY54JOoY")
case m.Content == "!zoom":
s.ChannelMessageSend(m.ChannelID, `
Sur internet/via une appli: https://zoom.us/j/988093214
Depuis un téléphone: 01.70.37.22.46, puis taper 988 093 214#
`)
case m.Content == "!coucou":
msgs := []string{
"SALUT ÇA VA !?",
"bonjour à toutes et à tous",
"bonjour",
"enchanté de vous rencontrer",
"coucou",
"salut",
"yo",
}
msg := msgs[rand.Intn(len(msgs))]
s.ChannelMessageSend(m.ChannelID, msg)
err := command(s, m)
if err != nil {
sendError(s, m, err)
}
}
36 changes: 36 additions & 0 deletions chaos-bot/replies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
soundcloud:
- "https://soundcloud.com/ultreme-reporters"

ultreme:
- "https://ultre.me"

calendrier:
- "https://calendrier.ultre.me"

pouet:
- "https://calendrier.ultre.me/2019/pouet/"

manfred:
- "c'est ce qu'elles disent toutes"
- "plus tu cours moins vite, moins t'avances plus vite"

discord:
- "https://ultre.me/discord"

radio:
- "http://salutcestcool.com/radio"

ucpj:
- "https://airtable.com/shrIAoOICeY54JOoY"

zoom:
- "Sur internet/via une appli: https://zoom.us/j/988093214\nDepuis un téléphone: 01.70.37.22.46, puis taper 988 093 214#"

coucou:
- "SALUT ÇA VA !?"
- "bonjour à toutes et à tous"
- "bonjour"
- "enchanté de vous rencontrer"
- "coucou"
- "salut"
- "yo"
13 changes: 13 additions & 0 deletions chaos-bot/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"log"

"github.com/bwmarrin/discordgo"
)

func sendError(s *discordgo.Session, m *discordgo.MessageCreate, err error) {
log.Printf("ERROR: %v", err)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("ERROR!"))
}

0 comments on commit 0c38a04

Please sign in to comment.