Skip to content

Commit 9dcc33b

Browse files
authored
Merge branch 'locale' into master
2 parents dc8ad3c + 8a3a68b commit 9dcc33b

18 files changed

+597
-52
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
config.txt
2525
final.txt
2626
final.env
27-
config.txt
27+
*log.txt
2828

2929
# logs file
3030
logs.txt

app.json

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
"CAPTURE_TIMEOUT": {
4242
"description": "How many seconds of no capture events received before the Bot terminates the associated game/connection. Defaults to 600 seconds.",
4343
"required": false
44+
},
45+
"BOT_LANG": {
46+
"description": "The bot localization language in Discord. By default en.",
47+
"required": false
4448
}
4549
}
4650
}

discord/bot.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
"github.com/bwmarrin/discordgo"
1717
"github.com/denverquane/amongusdiscord/game"
1818
"github.com/denverquane/amongusdiscord/storage"
19+
"github.com/denverquane/amongusdiscord/locale"
1920
socketio "github.com/googollee/go-socket.io"
2021
"github.com/gorilla/mux"
22+
"github.com/nicksnyder/go-i18n/v2/i18n"
2123
)
2224

2325
const DefaultPort = "8123"
@@ -486,7 +488,13 @@ func (bot *Bot) InactiveGameWorker(socket socketio.Conn, c <-chan string) {
486488
*v <- BroadcastMessage{
487489
Type: GRACEFUL_SHUTDOWN,
488490
Data: 1,
489-
Message: fmt.Sprintf("**I haven't received any messages from your capture in %d seconds, so I'm ending the game!**", bot.captureTimeout),
491+
Message: locale.LocalizeMessage(&i18n.Message{
492+
ID: "bot.InactiveGameWorker.Message",
493+
Other: "**I haven't received any messages from your capture in {{.captureTimeout}} seconds, so I'm ending the game!**",
494+
},
495+
map[string]interface{}{
496+
"captureTimeout": bot.captureTimeout,
497+
}),
490498
}
491499
}
492500
}
@@ -516,13 +524,20 @@ func (bot *Bot) InactiveGameWorker(socket socketio.Conn, c <-chan string) {
516524
func MessagesServer(port string, bot *Bot) {
517525
http.HandleFunc("/graceful", func(w http.ResponseWriter, r *http.Request) {
518526
if r.Method == http.MethodPost {
527+
captureTimeout := 30
519528
bot.ChannelsMapLock.RLock()
520529
for _, v := range bot.GlobalBroadcastChannels {
521530
if v != nil {
522531
*v <- BroadcastMessage{
523532
Type: GRACEFUL_SHUTDOWN,
524533
Data: 30,
525-
Message: fmt.Sprintf("I'm being shut down in %d seconds, and will be closing your active game!", 30),
534+
Message: locale.LocalizeMessage(&i18n.Message{
535+
ID: "bot.InactiveGameWorker.Message",
536+
Other: "I'm being shut down in {{.captureTimeout}} seconds, and will be closing your active game!",
537+
},
538+
map[string]interface{}{
539+
"captureTimeout": captureTimeout,
540+
}),
526541
}
527542
}
528543
}
@@ -564,7 +579,7 @@ func (bot *Bot) updatesListener() func(dg *discordgo.Session, guildID string, so
564579
break
565580
}
566581
log.Println("Detected transition to Menu")
567-
guild.AmongUsData.SetRoomRegion("Unprovided", "Unprovided")
582+
guild.AmongUsData.SetRoomRegion(getRoomAndRegionFromArgs(nil))
568583
guild.AmongUsData.SetPhase(phase)
569584
guild.GameStateMsg.Edit(dg, gameStateResponse(guild))
570585
guild.GameStateMsg.RemoveAllReactions(dg)
@@ -967,7 +982,10 @@ func (bot *Bot) handleMessageCreate(guild *GuildState, s *discordgo.Session, m *
967982
perms = guild.HasAdminPerms(m.Author) || guild.HasRolePerms(m.Member)
968983
}
969984
if !perms && g.OwnerID != m.Author.ID {
970-
s.ChannelMessageSend(m.ChannelID, "User does not have the required permissions to execute this command!")
985+
s.ChannelMessageSend(m.ChannelID, locale.LocalizeMessage(&i18n.Message{
986+
ID: "bot.handleMessageCreate.noPerms",
987+
Other: "User does not have the required permissions to execute this command!",
988+
}))
971989
} else {
972990
oldLen := len(contents)
973991
contents = strings.Replace(contents, prefix+" ", "", 1)

discord/commands.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"github.com/bwmarrin/discordgo"
66
"github.com/denverquane/amongusdiscord/game"
77
"github.com/denverquane/amongusdiscord/storage"
8+
"github.com/denverquane/amongusdiscord/locale"
89
"log"
910
"strings"
11+
"github.com/nicksnyder/go-i18n/v2/i18n"
1012
)
1113

1214
type CommandType int
@@ -211,14 +213,16 @@ func (bot *Bot) HandleCommand(guild *GuildState, s *discordgo.Session, g *discor
211213
if len(args[1:]) == 0 {
212214
embed := helpResponse(Version, prefix, AllCommands)
213215
s.ChannelMessageSendEmbed(m.ChannelID, &embed)
214-
215216
} else {
216217
cmd = GetCommand(args[1])
217218
if cmd.cmdType != Null {
218219
embed := ConstructEmbedForCommand(prefix, cmd)
219220
s.ChannelMessageSendEmbed(m.ChannelID, &embed)
220221
} else {
221-
s.ChannelMessageSend(m.ChannelID, "I didn't recognize that command! View `help` for all available commands!")
222+
s.ChannelMessageSend(m.ChannelID, locale.LocalizeMessage(&i18n.Message{
223+
ID: "commands.HandleCommand.Help.notFound",
224+
Other: "I didn't recognize that command! View `help` for all available commands!",
225+
}))
222226
}
223227
}
224228
break
@@ -305,7 +309,10 @@ func (bot *Bot) HandleCommand(guild *GuildState, s *discordgo.Session, g *discor
305309
} else {
306310
phase := getPhaseFromString(args[1])
307311
if phase == game.UNINITIALIZED {
308-
s.ChannelMessageSend(m.ChannelID, "Sorry, I didn't understand the game phase you tried to force")
312+
s.ChannelMessageSend(m.ChannelID, locale.LocalizeMessage(&i18n.Message{
313+
ID: "commands.HandleCommand.Force.UNINITIALIZED",
314+
Other: "Sorry, I didn't understand the game phase you tried to force",
315+
}))
309316
} else {
310317
//TODO this is ugly, but only for debug really
311318
bot.PushGuildPhaseUpdate(m.GuildID, phase)
@@ -336,11 +343,18 @@ func (bot *Bot) HandleCommand(guild *GuildState, s *discordgo.Session, g *discor
336343
guild.GameRunning = !guild.GameRunning
337344
guild.GameStateMsg.Edit(s, gameStateResponse(guild))
338345
break
346+
339347
case Log:
340348
guild.Logln(fmt.Sprintf("\"%s\"", strings.Join(args, " ")))
341349
break
342-
default:
343-
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Sorry, I didn't understand that command! Please see `%s help` for commands", prefix))
344350

351+
default:
352+
s.ChannelMessageSend(m.ChannelID, locale.LocalizeMessage(&i18n.Message{
353+
ID: "commands.HandleCommand.default",
354+
Other: "Sorry, I didn't understand that command! Please see `{{.CommandPrefix}} help` for commands",
355+
},
356+
map[string]interface{}{
357+
"CommandPrefix": prefix,
358+
}))
345359
}
346360
}

discord/helpers.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"log"
77
"strings"
88
"time"
9-
10-
"github.com/bwmarrin/discordgo"
9+
1110
"github.com/denverquane/amongusdiscord/game"
11+
"github.com/denverquane/amongusdiscord/locale"
12+
"github.com/bwmarrin/discordgo"
13+
"github.com/nicksnyder/go-i18n/v2/i18n"
1214
)
1315

1416
// when querying for the member list we need to specify a size
@@ -98,12 +100,21 @@ func getPhaseFromString(input string) game.Phase {
98100

99101
// GetRoomAndRegionFromArgs does what it sounds like
100102
func getRoomAndRegionFromArgs(args []string) (string, string) {
103+
roomUnprovided := locale.LocalizeMessage(&i18n.Message{
104+
ID: "helpers.getRoomAndRegionFromArgs.roomUnprovided",
105+
Other: "Unprovided",
106+
})
107+
regionUnprovided := locale.LocalizeMessage(&i18n.Message{
108+
ID: "helpers.getRoomAndRegionFromArgs.regionUnprovided",
109+
Other: "Unprovided",
110+
})
111+
101112
if len(args) == 0 {
102-
return "Unprovided", "Unprovided"
113+
return roomUnprovided, regionUnprovided
103114
}
104115
room := strings.ToUpper(args[0])
105116
if len(args) == 1 {
106-
return room, "Unprovided"
117+
return room, regionUnprovided
107118
}
108119
region := strings.ToLower(args[1])
109120
switch region {

discord/message_handlers.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"strings"
88

99
"github.com/denverquane/amongusdiscord/game"
10+
"github.com/denverquane/amongusdiscord/locale"
1011

1112
"github.com/bwmarrin/discordgo"
13+
"github.com/nicksnyder/go-i18n/v2/i18n"
1214
)
1315

1416
const downloadURL = "https://github.com/denverquane/amonguscapture/releases/latest/download/amonguscapture.exe"
@@ -85,9 +87,22 @@ func (bot *Bot) handleNewGameMessage(guild *GuildState, s *discordgo.Session, m
8587
var embed = discordgo.MessageEmbed{
8688
URL: "",
8789
Type: "",
88-
Title: "You just started a game!",
89-
Description: fmt.Sprintf("Click the following link to link your capture: \n <%s>\n\n"+
90-
"Don't have the capture installed? Latest version [here](%s)\nDon't have .NET Core installed? [32-bit here](%s), [64-bit here](%s)\n\nTo link your capture manually:", hyperlink, downloadURL, dotNet32Url, dotNet64Url),
90+
Title: locale.LocalizeMessage(&i18n.Message{
91+
ID: "message_handlers.handleNewGameMessage.embed.Title",
92+
Other: "You just started a game!",
93+
}),
94+
Description: locale.LocalizeMessage(&i18n.Message{
95+
ID: "message_handlers.handleNewGameMessage.embed.Description",
96+
Other: "Click the following link to link your capture: \n {{.hyperlink}}\n\n"+
97+
"Don't have the capture installed? Latest version [here]({{.downloadURL}})\n"+
98+
"Don't have .NET Core installed? [32-bit here]({{.dotNet32Url}}), [64-bit here]({{.dotNet64Url}})\n\nTo link your capture manually:",
99+
},
100+
map[string]interface{}{
101+
"hyperlink": hyperlink,
102+
"downloadURL": downloadURL,
103+
"dotNet32Url": dotNet32Url,
104+
"dotNet64Url": dotNet64Url,
105+
}),
91106
Timestamp: "",
92107
Color: 3066993, //GREEN
93108
Image: nil,
@@ -97,12 +112,18 @@ func (bot *Bot) handleNewGameMessage(guild *GuildState, s *discordgo.Session, m
97112
Author: nil,
98113
Fields: []*discordgo.MessageEmbedField{
99114
&discordgo.MessageEmbedField{
100-
Name: "URL",
115+
Name: locale.LocalizeMessage(&i18n.Message{
116+
ID: "message_handlers.handleNewGameMessage.embed.Fields.URL",
117+
Other: "URL",
118+
}),
101119
Value: minimalUrl,
102120
Inline: true,
103121
},
104122
&discordgo.MessageEmbedField{
105-
Name: "Code",
123+
Name: locale.LocalizeMessage(&i18n.Message{
124+
ID: "message_handlers.handleNewGameMessage.embed.Fields.Code",
125+
Other: "Code",
126+
}),
106127
Value: connectCode,
107128
Inline: true,
108129
},

0 commit comments

Comments
 (0)