-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor the codebase to make it easier to contribute to
- Loading branch information
Showing
7 changed files
with
183 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.env | ||
*~ | ||
*# | ||
.#* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!")) | ||
} |