From 2c08a2cbc8828359ed6e553acdc3681b0fb4ba5b Mon Sep 17 00:00:00 2001 From: LEGOlord208 Date: Wed, 26 Apr 2017 14:33:24 +0200 Subject: [PATCH] Added lang and modification of `console.` (Fixed #23 and #27) --- commands.go | 9 ++++++++- help.go | 6 ++++-- intercept.go | 6 ++++-- languages.go | 28 ++++++++++++++++++++++++++++ main.go | 21 +-------------------- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/commands.go b/commands.go index a3870bf..b8d15cd 100644 --- a/commands.go +++ b/commands.go @@ -58,7 +58,7 @@ var messages = messagesNone var intercept = true var output = false -var webhookCommands = []string{"big", "say", "sayfile", "embed", "name", "avatar", "exit", "exec", "run"} +var webhookCommands = []string{"big", "say", "sayfile", "embed", "name", "avatar", "exit", "exec", "run", "lang"} func command(session *discordgo.Session, terminal bool, cmd string, w io.Writer) (returnVal string) { cmd = strings.TrimSpace(cmd) @@ -145,6 +145,13 @@ func commandRaw(session *discordgo.Session, terminal bool, cmd string, args []st if err != nil { stdutil.PrintErr(tl("failed.lua.run"), err) } + case "lang": + if nargs < 1 { + stdutil.PrintErr("lang ", nil) + return + } + + loadLangAuto(args[0]) case "guilds": fallthrough case "guild": diff --git a/help.go b/help.go index ddee9c7..0dd6ed1 100644 --- a/help.go +++ b/help.go @@ -11,9 +11,11 @@ func printHelp(search string) { help = append(help, "help [search]\tShow help menu. Optionally search.") help = append(help, "exit\tExit DiscordConsole") + help = append(help, "exec\tExecute a shell command") + help = append(help, "run\tRun a LUA file with DiscordConsole's special functions") + help = append(help, "lang \tSame as starting with --lang") + help = append(help, "") if userType != typeWebhook { - help = append(help, "exec\tExecute a shell command") - help = append(help, "") help = append(help, "guilds\tList guilds/servers this bot is added to.") help = append(help, "guild \tSelect a guild to use for further commands.") help = append(help, "channels\tList channels in your selected guild.") diff --git a/intercept.go b/intercept.go index fba5eac..759c547 100644 --- a/intercept.go +++ b/intercept.go @@ -116,11 +116,13 @@ func messageCommand(session *discordgo.Session, e *discordgo.Message, guild *dis return } + prefix := tl("console.") + contents := strings.TrimSpace(e.Content) - if !strings.HasPrefix(contents, "console.") { + if !strings.HasPrefix(contents, prefix) { return } - cmd := contents[len("console."):] + cmd := contents[len(prefix):] isCmd = true diff --git a/languages.go b/languages.go index 1989fef..ca81b33 100644 --- a/languages.go +++ b/languages.go @@ -5,8 +5,12 @@ package main import ( "bufio" "errors" + "fmt" "io" + "os" "strings" + + "github.com/legolord208/stdutil" ) var errLangCorrupt = errors.New("corrupt language file") @@ -22,6 +26,28 @@ func tl(name string) string { return name } +func loadLangAuto(langfile string) { + fmt.Println("Loading language...") + switch langfile { + case "en": + loadLangDefault() + case "sv": + loadLangString(langSv) + default: + reader, err := os.Open(langfile) + if err != nil { + stdutil.PrintErr("Could not read language file", err) + return + } + defer reader.Close() + + err = loadLang(reader) + if err != nil { + stdutil.PrintErr("Could not load language file", err) + loadLangDefault() + } + } +} func loadLang(reader io.Reader) error { lang = make(map[string]string) scanner := bufio.NewScanner(reader) @@ -279,4 +305,6 @@ status.status=Status satt! rl.session=Startar om session... rl.cache.loc=Laddar om plats-cache... rl.cache.vars=Tar boft cache-variablar... + +console.=konsoll. ` diff --git a/main.go b/main.go index aec3854..2f34c7f 100644 --- a/main.go +++ b/main.go @@ -86,26 +86,7 @@ func main() { doErrorHook() fmt.Println("DiscordConsole " + version) - fmt.Println("Loading language...") - switch langfile { - case "en": - loadLangDefault() - case "sv": - loadLangString(langSv) - default: - reader, err := os.Open(langfile) - if err != nil { - stdutil.PrintErr("Could not read language file", err) - return - } - defer reader.Close() - - err = loadLang(reader) - if err != nil { - stdutil.PrintErr("Could not load language file", err) - loadLangDefault() - } - } + loadLangAuto(langfile) if !noupdate { fmt.Print(tl("update.checking") + " ")