168 lines
4.5 KiB
Go
Executable File
168 lines
4.5 KiB
Go
Executable File
package core
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
// This function will be called (due to AddHandler above) every time a new
|
|
// guild is joined.
|
|
func GuildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
|
|
|
|
if event.Guild.Unavailable {
|
|
return
|
|
}
|
|
|
|
for _, channel := range event.Guild.Channels {
|
|
if channel.ID == event.Guild.ID {
|
|
_, _ = s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func getHelpMessage() string {
|
|
// build list of possible commands
|
|
// TODO: currently, the help needs to get value initiated at startup time to be built.
|
|
// I don't like that and wish to test what is rendered without connecting to the server
|
|
var help []string
|
|
for _, command := range CommandRegister {
|
|
help = append(help,
|
|
fmt.Sprintf(
|
|
"%s : %s (Example : %s)\n",
|
|
command.name,
|
|
command.desc,
|
|
command.example))
|
|
}
|
|
sort.Strings(help)
|
|
welcome := fmt.Sprintf(
|
|
"Hello, I'm **%s**. Here are the things I can do :\n",
|
|
ConfigRegister.Name)
|
|
return welcome + strings.Join(help, "")
|
|
}
|
|
|
|
// return random hats
|
|
func getHatMessage() string {
|
|
hat := []string{"🧢", "👒", "⛑", "🎩", "🎓"}
|
|
rand.Seed(time.Now().UnixNano())
|
|
var amount int = (rand.Intn(len(hat) -1) +1)
|
|
var bag []string
|
|
for i := 0; i < amount; i++ {
|
|
rand.Seed(time.Now().UnixNano())
|
|
bag = append(bag, hat[(rand.Intn(len(hat) -1) +1)])
|
|
}
|
|
return strings.Join(bag, " ")
|
|
}
|
|
|
|
// returns a random message if the processed message matchs with a pattern.
|
|
// patterns are substrings belonging to a "domain" of lexical items
|
|
func getTalkbackMessage(message string) string {
|
|
for _, t := range ConfigRegister.Talkback {
|
|
for _, p := range t.Domain.Patterns {
|
|
rand.Seed(time.Now().UnixNano())
|
|
if strings.Contains(strings.ToLower(message), strings.ToLower(p)) {
|
|
rand.Seed(time.Now().UnixNano())
|
|
return t.Domain.Answers[rand.Intn(len(t.Domain.Answers))]
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// check if a message is a valid command. if yes, execute the command
|
|
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
for alias, _ := range CommandRegister {
|
|
if strings.HasPrefix(m.Content, alias) {
|
|
if m.Author.ID == s.State.User.ID {
|
|
return
|
|
}
|
|
var message string = strings.TrimPrefix(m.Content, alias)
|
|
CommandRegister[alias].command(s, m, message)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func MessageHat(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
|
|
s.ChannelMessageSend(m.ChannelID, getHatMessage())
|
|
}
|
|
|
|
func MessageTalkback(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
if m.Author.ID == s.State.User.ID {
|
|
return
|
|
}
|
|
var answer string = getTalkbackMessage(m.Content)
|
|
if answer != "" && rand.Intn(2) == 1 {
|
|
s.ChannelMessageSend(m.ChannelID, answer)
|
|
}
|
|
}
|
|
|
|
func MessageHelp(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
|
|
s.ChannelMessageSend(m.ChannelID, getHelpMessage())
|
|
}
|
|
|
|
func MessagePing(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
|
|
if m.Content == "ping" {
|
|
s.ChannelMessageSend(m.ChannelID, "Pong!")
|
|
}
|
|
|
|
if m.Content == "pong" {
|
|
s.ChannelMessageSend(m.ChannelID, "Ping!")
|
|
}
|
|
}
|
|
|
|
// generate TTS version of the message and upload it to the channel
|
|
func MessageAudio(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
|
|
log.Println("Messageaudio :", message)
|
|
var cleanMessage string = prepareTTSMessage(message)
|
|
filename, out := getAudioMessage(cleanMessage)
|
|
file := bytes.NewReader(out)
|
|
s.ChannelFileSend(m.ChannelID, filename, file)
|
|
// delete the receveid command after processing it. less clutter
|
|
log.Println("Messageaudio :", cleanMessage)
|
|
s.ChannelMessageDelete(m.ChannelID, m.ID)
|
|
}
|
|
|
|
func MessageVocal(s *discordgo.Session, m *discordgo.MessageCreate, message string) {
|
|
// Ignore all messages created by the bot itself
|
|
// This isn't required in this specific example but it's a good practice.
|
|
if m.Author.ID == s.State.User.ID {
|
|
return
|
|
}
|
|
|
|
// Find the channel that the message came from.
|
|
c, err := s.State.Channel(m.ChannelID)
|
|
if err != nil {
|
|
// Could not find channel.
|
|
return
|
|
}
|
|
|
|
// Find the guild for that channel.
|
|
g, err := s.State.Guild(c.GuildID)
|
|
if err != nil {
|
|
// Could not find guild.
|
|
return
|
|
}
|
|
|
|
var cleanMessage string = prepareTTSMessage(message)
|
|
_, out := getAudioMessage(cleanMessage)
|
|
|
|
// Look for the message sender in that guild's current voice states.
|
|
for _, vs := range g.VoiceStates {
|
|
if vs.UserID == m.Author.ID {
|
|
err = playSound(s, g.ID, vs.ChannelID, out)
|
|
if err != nil {
|
|
fmt.Println("Error playing sound:", err)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|