Skip to content

Commit

Permalink
Feat: Add link to open directly AmongUsCapture instead of copying and…
Browse files Browse the repository at this point in the history
… pasting in your browser
  • Loading branch information
GayKevin committed Oct 20, 2023
1 parent 881fa6a commit 2241170
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ FROM alpine:3.13.0 AS final
# * App directory to allow mounting volumes
RUN addgroup -g 1000 bot && \
adduser -HD -u 1000 -G bot bot && \
mkdir -p /app/logs /app/locales /app/storage && \
mkdir -p /app/logs /app/locales /app/storage /app/templates && \
chown -R bot:bot /app
USER bot
WORKDIR /app

# Import the compiled executable and locales.
COPY --from=builder /app /app
COPY ./locales/ /app/locales
COPY ./templates/ /app/templates
COPY ./storage/postgres.sql /app/storage/postgres.sql

# Port used for AMU API
Expand Down
27 changes: 27 additions & 0 deletions bot/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func (bot *Bot) StartAPIServer(port string) {
r := gin.Default()
r.LoadHTMLGlob("templates/*")

docs.SwaggerInfo.BasePath = "/"
docs.SwaggerInfo.Title = "AutoMuteUs"
Expand Down Expand Up @@ -61,6 +62,8 @@ func (bot *Bot) StartAPIServer(port string) {

r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

r.GET("/open/link", handleGetOpenAmongUsCapture(bot))

// TODO add endpoints for notable player information, like total games played, num wins, etc

// TODO properly configure CORS -_-
Expand Down Expand Up @@ -98,6 +101,30 @@ func handleGetCommands() func(c *gin.Context) {
}
}

// Open AmongUsCapture
// @Summary Get AmongUsCapture
// @Schemes GET
// @Description Return html that open AmongUsCapture
// @Produce {string} string "text/html"
// @Success 200 {string} string "text/html"
// @Router /open/link [get]
func handleGetOpenAmongUsCapture(bot *Bot) func(c *gin.Context) {
return func(c *gin.Context) {
connectCode := c.Query("connectCode")
if len(connectCode) != 8 {
c.JSON(http.StatusBadRequest, HttpError{
StatusCode: http.StatusBadRequest,
Error: "invalid connect code",
})
return
}
hyperlink, _, _ := formCaptureURL(bot.url, connectCode)
c.HTML(http.StatusOK, "link.tmpl", gin.H{
"URL": hyperlink,
})
}
}

// GetGameState godoc
// @Summary Get Game State
// @Schemes GET
Expand Down
17 changes: 10 additions & 7 deletions bot/command/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ const (
)

type NewInfo struct {
Hyperlink string
MinimalURL string
ConnectCode string
ActiveGames int64
Hyperlink string
MinimalURL string
ApiHyperlink string
ConnectCode string
ActiveGames int64
}

var New = discordgo.ApplicationCommand{
Expand All @@ -36,13 +37,15 @@ func NewResponse(status NewStatus, info NewInfo, sett *settings.GuildSettings) *
case NewSuccess:
content = sett.LocalizeMessage(&i18n.Message{
ID: "commands.new.success",
Other: "Click the following URL to link your capture, or paste it into your web browser: \n <{{.hyperlink}}>\n\n" +
Other: "Click the following URL to link your capture, or paste it into your web browser: \n <{{.hyperlink}}>\n" +
"or click [here]({{.apiHyperlink}})\n\n" +
"If the URL doesn't work, you may need to run the capture program first, and then try again.\n\n" +
"Don't have the capture installed? Latest version [here]({{.downloadURL}})\n\nTo link your capture manually:",
},
map[string]interface{}{
"hyperlink": info.Hyperlink,
"downloadURL": CaptureDownloadURL,
"hyperlink": info.Hyperlink,
"apiHyperlink": info.ApiHyperlink,
"downloadURL": CaptureDownloadURL,
})
embeds = []*discordgo.MessageEmbed{
{
Expand Down
10 changes: 9 additions & 1 deletion bot/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"math/rand"
"os"
"regexp"
"strings"

Expand All @@ -23,7 +24,7 @@ func generateConnectCode(guildID string) string {

var urlregex = regexp.MustCompile(`^http(?P<secure>s?)://(?P<host>[\w.-]+)(?::(?P<port>\d+))?/?$`)

func formCaptureURL(url, connectCode string) (hyperlink, minimalURL string) {
func formCaptureURL(url, connectCode string) (hyperlink, apiHyperlink, minimalURL string) {
if match := urlregex.FindStringSubmatch(url); match != nil {
secure := match[urlregex.SubexpIndex("secure")] == "s"
host := match[urlregex.SubexpIndex("host")]
Expand All @@ -44,10 +45,17 @@ func formCaptureURL(url, connectCode string) (hyperlink, minimalURL string) {
protocol = "https://"
}

hostAPI := os.Getenv("API_SERVER_URL")
if hostAPI == "" {
hostAPI = "http://localhost"
}

hyperlink = fmt.Sprintf("aucapture://%s%s/%s%s", host, port, connectCode, insecure)
apiHyperlink = fmt.Sprintf("%s/open/link?connectCode=%s", hostAPI, connectCode)
minimalURL = fmt.Sprintf("%s%s%s", protocol, host, port)
} else {
hyperlink = "Invalid HOST provided (should resemble something like `http://localhost:8123`)"
apiHyperlink = "Invalid API Link"
minimalURL = "Invalid HOST provided"
}
return
Expand Down
11 changes: 6 additions & 5 deletions bot/slash_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,16 @@ func (bot *Bot) slashCommandHandler(s *discordgo.Session, i *discordgo.Interacti
bot.EndGameChannels[dgs.ConnectCode] = killChan
bot.ChannelsMapLock.Unlock()

hyperlink, minimalURL := formCaptureURL(bot.url, dgs.ConnectCode)
hyperlink, apiHyperlink, minimalURL := formCaptureURL(bot.url, dgs.ConnectCode)

bot.handleGameStartMessage(i.GuildID, i.ChannelID, voiceChannelID, i.Member.User.ID, sett, g, dgs.ConnectCode)

return command.NewResponse(status, command.NewInfo{
Hyperlink: hyperlink,
MinimalURL: minimalURL,
ConnectCode: dgs.ConnectCode,
ActiveGames: activeGames, // not actually needed for Success messages
Hyperlink: hyperlink,
ApiHyperlink: apiHyperlink,
MinimalURL: minimalURL,
ConnectCode: dgs.ConnectCode,
ActiveGames: activeGames, // not actually needed for Success messages
}, sett)
} else {
// release the lock
Expand Down
2 changes: 1 addition & 1 deletion locales/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"commands.link.success" = "Successfully linked {{.UserMention}} to an in-game player with the color: `{{.Color}}`"
"commands.new.lockout" = "If I start any more games, Discord will lock me out, or throttle the games I'm running! 😦\\nPlease try again in a few minutes, or consider AutoMuteUs Premium (`/premium`)\\nCurrent Games: {{.Games}}"
"commands.new.nochannel" = "Please join a voice channel before starting a match!"
"commands.new.success" = "Click the following URL to link your capture, or paste it into your web browser: \\n <{{.hyperlink}}>\\n\\nIf the URL doesn't work, you may need to run the capture program first, and then try again.\\n\\nDon't have the capture installed? Latest version [here]({{.downloadURL}})\\n\\nTo link your capture manually:"
"commands.new.success" = "Click the following URL to link your capture, or paste it into your web browser: \\n <{{.hyperlink}}> or click [here]({{.apiHyperlink}})\\nIf the URL doesn't work, you may need to run the capture program first, and then try again.\\n\\nDon't have the capture installed? Latest version [here]({{.downloadURL}})\\n\\nTo link your capture manually:"
"commands.new.success.code" = "Code"
"commands.new.success.url" = "URL"
"commands.no_permissions" = "Sorry, you don't have the required permissions to issue that command."
Expand Down
2 changes: 1 addition & 1 deletion locales/active.fr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"commands.link.success" = "{{.UserMention}} a été lié avec succès avec la couleur : `{{.Color}}`"
"commands.new.lockout" = "Si je lance encore plus de parties, Discord risque de me bloquer ou de limiter les parties que je gère ! 😦\\nPMerci de réessayer dans quelques minutes, ou considérez AutoMuteUs Premium (`/premium`)\\nParties en cours : {{.Games}}"
"commands.new.nochannel" = "Veuillez rejoindre un salon vocal avant de commencer un match !"
"commands.new.success" = "Cliquez sur le lien suivant pour lier votre capture : \\n <{{.hyperlink}}>\\n\\nVous n'avez pas installé la capture ? Dernière version [ici]({{.downloadURL}})\\n\\nPour lier votre capture manuellement:"
"commands.new.success" = "Cliquez sur le lien suivant pour lier votre capture : \\n <{{.hyperlink}}> ou cliquez [ici]({{.apiHyperlink}})\\n\\nVous n'avez pas installé la capture ? Dernière version [ici]({{.downloadURL}})\\n\\nPour lier votre capture manuellement:"
"commands.new.success.code" = "Code"
"commands.new.success.url" = "URL"
"commands.no_permissions" = "Désolé, vous n'avez pas les permissions requises pour exécuter cette commande."
Expand Down
18 changes: 18 additions & 0 deletions templates/link.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Redirect</title>
<script type="text/javascript">
window.onload = function() {
window.location = "{{.URL}}";
window.onblur = function() {
setTimeout(function() {
window.close();
}, 5000); // 5s delay before closing the tab
};
};
</script>
</head>
<body>
</body>
</html>

0 comments on commit 2241170

Please sign in to comment.