Skip to content

Commit

Permalink
Added lang and modification of console. (Fixed #23 and #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
LEGOlord208 authored and LEGOlord208 committed Apr 26, 2017
1 parent de5ce01 commit 2c08a2c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
9 changes: 8 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <language>", nil)
return
}

loadLangAuto(args[0])
case "guilds":
fallthrough
case "guild":
Expand Down
6 changes: 4 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <language>\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 <id>\tSelect a guild to use for further commands.")
help = append(help, "channels\tList channels in your selected guild.")
Expand Down
6 changes: 4 additions & 2 deletions intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/legolord208/stdutil"
)

var errLangCorrupt = errors.New("corrupt language file")
Expand All @@ -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)
Expand Down Expand Up @@ -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.
`
21 changes: 1 addition & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") + " ")
Expand Down

0 comments on commit 2c08a2c

Please sign in to comment.