From 11fed610739db0f03fbb690b26b72bfc404e8a07 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Sat, 30 May 2020 20:51:04 +0800 Subject: [PATCH] feat: Implement macros --- commands/commands.go | 1 + commands/macro.go | 30 ++++++++++++++++++++++++++++++ config.go | 4 +++- main.go | 2 ++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 commands/macro.go diff --git a/commands/commands.go b/commands/commands.go index 618980c..b91776e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -51,4 +51,5 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { v.Processor(dg, m, split[2:]) return } + handleMacro(dg, m, split[1:]) } diff --git a/commands/macro.go b/commands/macro.go new file mode 100644 index 0000000..fce8168 --- /dev/null +++ b/commands/macro.go @@ -0,0 +1,30 @@ +package commands + +import ( + "strings" + + "github.com/bwmarrin/discordgo" + + "gitea.teamortix.com/chanbakjsd/Milen/util" +) + +var ( + CommonReplacement map[string]string + Macro map[string]string +) + +func handleMacro(dg *discordgo.Session, m *discordgo.MessageCreate, split []string) { + for k, _ := range split { + split[k] = strings.ToLower(split[k]) + if val, ok := CommonReplacement[split[k]]; ok { + split[k] = val + } + } + full := strings.Join(split, " ") + for k, v := range Macro { + if k == full { + util.SendMessage(dg, m.ChannelID, v) + return + } + } +} diff --git a/config.go b/config.go index 6f6d012..ebbfda5 100644 --- a/config.go +++ b/config.go @@ -8,7 +8,9 @@ import ( type config struct { Token string - ReportTarget string + ReportTarget string `toml:"report_target"` + MacroReplace map[string]string `toml:"macro_replace"` + Macro map[string]string } func LoadConfig() config { diff --git a/main.go b/main.go index 2b16ba9..3998daf 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,8 @@ import ( func main() { cfg := LoadConfig() + commands.CommonReplacement = cfg.MacroReplace + commands.Macro = cfg.Macro util.ReportTarget = cfg.ReportTarget dg, err := discordgo.New("Bot " + cfg.Token)