From 1828615e94fc0df84736cb59be9417b194490648 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Mon, 11 May 2020 14:43:55 +0800 Subject: [PATCH] humanify: Move constant string out of code --- commands/commands.go | 7 ++++--- humanify/strings.go | 35 +++++++++++++++++++++++++++++++++++ locale.toml | 7 +++++++ util/perm.go | 4 +++- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 humanify/strings.go create mode 100644 locale.toml diff --git a/commands/commands.go b/commands/commands.go index 9f1c78d..fe9e758 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -6,6 +6,7 @@ import ( "github.com/bwmarrin/discordgo" "gitea.teamortix.com/chanbakjsd/Milen/db" + "gitea.teamortix.com/chanbakjsd/Milen/humanify" "gitea.teamortix.com/chanbakjsd/Milen/util" ) @@ -18,12 +19,12 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { case "autorole": util.RequireAdmin(dg, m.Author.ID, m.ChannelID, func() { if len(split) < 6 { - util.SendCheckError(dg, m.ChannelID, "❌ Incorrect usage. Usage: `milen autorole `") + util.SendCheckError(dg, m.ChannelID, humanify.Eng.Usage.Autorole) return } role := util.ParseRole(split[2]) if role == "" { - util.SendCheckError(dg, m.ChannelID, "❌ Incorrect usage. Usage: `milen autorole `") + util.SendCheckError(dg, m.ChannelID, humanify.Eng.Usage.Autorole) return } emoji := util.ParseEmoji(split[5]) @@ -33,7 +34,7 @@ func Event(dg *discordgo.Session, m *discordgo.MessageCreate) { util.ReportError(dg, err) return } - util.SendCheckError(dg, m.ChannelID, "✅ Auto-role registered.") + util.SendCheckError(dg, m.ChannelID, humanify.Eng.Autorole.Success) }) } } diff --git a/humanify/strings.go b/humanify/strings.go new file mode 100644 index 0000000..d2c2226 --- /dev/null +++ b/humanify/strings.go @@ -0,0 +1,35 @@ +package humanify + +import ( + "fmt" + "io/ioutil" + + "github.com/BurntSushi/toml" +) + +var Eng Locale + +type Locale struct { + NoAdmin string `toml:"no_admin"` + Usage localeUsage + Autorole localeAutorole +} + +type localeUsage struct { + Autorole string `toml:"autorole"` +} + +type localeAutorole struct { + Success string `toml:"success"` +} + +func init() { + data, err := ioutil.ReadFile("locale.toml") + if err != nil { + panic(err) + } + err = toml.Unmarshal(data, &Eng) + if err != nil { + panic(err) + } +} diff --git a/locale.toml b/locale.toml new file mode 100644 index 0000000..975a0c8 --- /dev/null +++ b/locale.toml @@ -0,0 +1,7 @@ +no_admin="❌ You do not have permission to use this command." + +[usage] +autorole="❌ Incorrect usage. Usage: `milen autorole `" + +[autorole] +success="✅ Auto-role registered." diff --git a/util/perm.go b/util/perm.go index 8bd7f1c..44a2a71 100644 --- a/util/perm.go +++ b/util/perm.go @@ -2,6 +2,8 @@ package util import ( "github.com/bwmarrin/discordgo" + + "gitea.teamortix.com/chanbakjsd/Milen/humanify" ) func RequireAdmin(dg *discordgo.Session, userID string, channelID string, hasAdmin func()) { @@ -11,7 +13,7 @@ func RequireAdmin(dg *discordgo.Session, userID string, channelID string, hasAdm return } if perm&discordgo.PermissionAdministrator == 0 { - SendCheckError(dg, channelID, "❌ You do not have permission to use this command.") + SendCheckError(dg, channelID, humanify.Eng.NoAdmin) return } hasAdmin()