diff --git a/.gitignore b/.gitignore index 20c72b2..4cdc449 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ go.work config.json +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..59329ea --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:1.22-alpine + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -o ./discord +CMD ["./discord"] diff --git a/bot/bot.go b/bot/bot.go index e69de29..514807a 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -0,0 +1,43 @@ +package bot + +import ( + "fmt" + + "github.com/Floriansylvain/GObot/config" + "github.com/bwmarrin/discordgo" +) + +var ( + BotId string + goBot *discordgo.Session +) + +func Start() { + goBot, err := discordgo.New("Bot " + config.Token) + if err != nil { + fmt.Println(err.Error()) + return + } + + u, err := goBot.User("@me") + if err != nil { + fmt.Println(err.Error()) + return + } + + BotId = u.ID + + goBot.AddHandler(messageCreate) + + err = goBot.Open() + if err != nil { + fmt.Println(err.Error()) + return + } +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Content == config.BotPrefix+"cc" { + s.ChannelMessageSend(m.ChannelID, "sv ?") + } +} diff --git a/config/config.go b/config/config.go index 482b401..7a1abef 100644 --- a/config/config.go +++ b/config/config.go @@ -1,17 +1,19 @@ package config -var ( - Token string - BotPrefix string - - config *Config +import ( + "fmt" + "os" ) -type Config struct { - TOKEN string `json:"TOKEN"` - BOT_PREFIX string `json:"BOT_PREFIX"` -} +var ( + Token string + BotPrefix string +) func LoadConfig() error { - fmt.printLn("Loading config file") + fmt.Println("Loading env values") + Token = os.Getenv("TOKEN") + BotPrefix = os.Getenv("BOT_PREFIX") + fmt.Println("Successfully loaded env values") + return nil } diff --git a/discord b/discord new file mode 100755 index 0000000..4d6922e Binary files /dev/null and b/discord differ diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..0a85b67 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,6 @@ +services: + gobot: + build: . + environment: + - TOKEN=${TOKEN} + - BOT_PREFIX=${BOT_PREFIX} \ No newline at end of file diff --git a/main.go b/main.go index 85f0393..1e8fdd4 100644 --- a/main.go +++ b/main.go @@ -1 +1,21 @@ -package main \ No newline at end of file +package main + +import ( + "fmt" + + "github.com/Floriansylvain/GObot/bot" + "github.com/Floriansylvain/GObot/config" +) + +func main() { + err := config.LoadConfig() + + if err != nil { + fmt.Println(err.Error()) + return + } + + bot.Start() + + select {} +}