add docker, move files into src
Marco Andronaco andronacomarco@gmail.com
Mon, 30 Sep 2024 17:04:37 +0200
11 files changed,
125 insertions(+),
61 deletions(-)
A
Dockerfile
@@ -0,0 +1,42 @@
+# syntax=docker/dockerfile:1 + +FROM golang:1.23-alpine AS builder + +# RUN apk add --no-cache git + +WORKDIR /build + +# Download Git submodules +# COPY .git ./.git +# RUN git -c submodule.ui.update=none submodule update --init --recursive + +# Download Go modules +COPY go.mod go.sum ./ +RUN go mod download +RUN go mod verify + +# Transfer source code +COPY .git/refs/heads/main ./commitID +COPY *.go ./ +COPY src ./src + +# Build +RUN commit_hash=$(cat commitID | cut -c1-7) && \ + CGO_ENABLED=0 go build -ldflags "-X github.com/BiRabittoh/disgord/src.CommitID=$commit_hash" -trimpath -o /dist/app + + +# Test +FROM builder AS run-test-stage +# COPY i18n ./i18n +RUN go test -v ./... + +FROM scratch AS build-release-stage + +WORKDIR /app + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /dist . +# COPY i18n ./i18n +# COPY publi[c] ./public + +ENTRYPOINT ["./app"]
M
commands.go
→
src/commands.go
@@ -1,6 +1,7 @@
-package main +package src import ( + "fmt" "strings" "github.com/bwmarrin/discordgo"@@ -11,6 +12,14 @@ handlersMap map[string]BotCommand
shortCommands = map[string]string{} ) +func (bc BotCommand) FormatHelp(command string) string { + var shortCodeStr string + if bc.ShortCode != "" { + shortCodeStr = fmt.Sprintf(" (%s)", formatCommand(bc.ShortCode)) + } + return fmt.Sprintf(helpFmt, formatCommand(command)+shortCodeStr, bc.Help) +} + func InitHandlers() { handlersMap = map[string]BotCommand{ "echo": {ShortCode: "e", Handler: handleEcho, Help: "echoes a message"},@@ -27,6 +36,27 @@ shortCommands[botCommand.ShortCode] = command
} } +func HandleCommand(s *discordgo.Session, m *discordgo.MessageCreate) (response string, ok bool, err error) { + command, args, ok := parseUserMessage(m.Content) + if !ok { + return + } + + longCommand, short := shortCommands[command] + if short { + command = longCommand + } + + botCommand, found := handlersMap[command] + if !found { + response = "Unknown command: " + formatCommand(command) + return + } + + response = botCommand.Handler(args, s, m) + return +} + func handleEcho(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { return strings.Join(args, " ") }@@ -41,8 +71,8 @@ if len(newPrefix) > 10 {
return "Prefix is too long." } - config.Values.Prefix = newPrefix - err := config.Save() + Config.Values.Prefix = newPrefix + err := Config.Save() if err != nil { logger.Errorf("could not save config: %s", err) }
A
docker-compose.yaml
@@ -0,0 +1,12 @@
+name: telegle + +services: + app: + build: + context: . + image: 'birabittoh/disgord' + container_name: 'disgord' + restart: unless-stopped + volumes: + - /etc/localtime:/etc/localtime:ro + - ./config.json:/app/config.json
M
globals.go
→
src/globals.go
@@ -1,10 +1,10 @@
-package main +package src import ( "os" - "github.com/BiRabittoh/disgord/myconfig" - "github.com/BiRabittoh/disgord/mylog" + "github.com/BiRabittoh/disgord/src/myconfig" + "github.com/BiRabittoh/disgord/src/mylog" "github.com/bwmarrin/discordgo" "github.com/kkdai/youtube/v2" )@@ -15,25 +15,26 @@ helpFmt = "%s - _%s_"
) var ( - config *myconfig.Config[Config] + CommitID string + Config *myconfig.Config[MyConfig] logger = mylog.NewLogger(os.Stdout, "main", mylog.DEBUG) yt = youtube.Client{} ) -type KeyValue struct { - Key string `json:"key"` +type KeyValuePair struct { + Name string `json:"name"` Value string `json:"value"` } -type Config struct { +type MyConfig struct { ApplicationID string `json:"applicationId"` Token string `json:"token"` Prefix string `json:"prefix"` - Outros []KeyValue `json:"outros"` - Radios []KeyValue `json:"radios"` + Outros []KeyValuePair `json:"outros"` + Radios []KeyValuePair `json:"radios"` MagazineSize uint `json:"magazineSize"` }
M
main.go
→
main.go
@@ -3,48 +3,19 @@
import ( "os" "os/signal" - "strings" - "github.com/BiRabittoh/disgord/myconfig" + "github.com/BiRabittoh/disgord/src" + "github.com/BiRabittoh/disgord/src/myconfig" + "github.com/BiRabittoh/disgord/src/mylog" "github.com/bwmarrin/discordgo" ) -func parseUserMessage(messageContent string) (command string, args []string, ok bool) { - after, found := strings.CutPrefix(messageContent, config.Values.Prefix) - if !found { - return - } - - userInput := strings.Split(after, " ") - command = strings.ToLower(userInput[0]) - return command, userInput[1:], len(command) > 0 -} - -func handleCommand(s *discordgo.Session, m *discordgo.MessageCreate) (response string, ok bool, err error) { - command, args, ok := parseUserMessage(m.Content) - if !ok { - return - } - - longCommand, short := shortCommands[command] - if short { - command = longCommand - } - - botCommand, found := handlersMap[command] - if !found { - response = "Unknown command: " + formatCommand(command) - return - } - - response = botCommand.Handler(args, s, m) - return -} +var logger = mylog.NewLogger(os.Stdout, "init", mylog.DEBUG) func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { logger.Debug("got a message: " + m.Content) - response, ok, err := handleCommand(s, m) + response, ok, err := src.HandleCommand(s, m) if err != nil { logger.Errorf("could not handle command: %s", err) return@@ -68,18 +39,19 @@ logger.Infof("Logged in as %s", r.User.String())
} func main() { + logger.Info("Starting bot... Commit " + src.CommitID) var err error - config, err = myconfig.New[Config]("config.json") + src.Config, err = myconfig.New[src.MyConfig]("config.json") if err != nil { logger.Errorf("could not load config: %s", err) } - session, err := discordgo.New("Bot " + config.Values.Token) + session, err := discordgo.New("Bot " + src.Config.Values.Token) if err != nil { logger.Fatalf("could not create bot session: %s", err) } - InitHandlers() + src.InitHandlers() session.AddHandler(messageHandler) session.AddHandler(readyHandler)
M
shoot.go
→
src/shoot.go
@@ -1,4 +1,4 @@
-package main +package src import ( "fmt"@@ -56,7 +56,7 @@ if ok {
return } - q = NewMagazine(config.Values.MagazineSize) + q = NewMagazine(Config.Values.MagazineSize) magazines[userID] = q return }
M
utils.go
→
src/utils.go
@@ -1,18 +1,11 @@
-package main +package src import ( "fmt" + "strings" "github.com/bwmarrin/discordgo" ) - -func (bc BotCommand) FormatHelp(command string) string { - var shortCodeStr string - if bc.ShortCode != "" { - shortCodeStr = fmt.Sprintf(" (%s)", formatCommand(bc.ShortCode)) - } - return fmt.Sprintf(helpFmt, formatCommand(command)+shortCodeStr, bc.Help) -} func getVoiceChannelID(s *discordgo.Session, m *discordgo.MessageCreate) (response string, g *discordgo.Guild, voiceChannelID string) { if m.Member == nil {@@ -41,5 +34,16 @@ return
} func formatCommand(command string) string { - return fmt.Sprintf("`%s%s`", config.Values.Prefix, command) + return fmt.Sprintf("`%s%s`", Config.Values.Prefix, command) +} + +func parseUserMessage(messageContent string) (command string, args []string, ok bool) { + after, found := strings.CutPrefix(messageContent, Config.Values.Prefix) + if !found { + return + } + + userInput := strings.Split(after, " ") + command = strings.ToLower(userInput[0]) + return command, userInput[1:], len(command) > 0 }