commit 19541281f23c58be1c69dbf457c249a04ca8b571 Author: Julius de Jeu Date: Mon Sep 7 14:30:18 2020 +0200 yeet diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..8d84b9b --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,20 @@ +image: docker:19.03.12 + +services: + - docker:19.03.12-dind + +#variables: +# # Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled +# DOCKER_HOST: tcp://docker:2376 +# DOCKER_TLS_CERTDIR: "/certs" + +before_script: + - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + +build: + stage: build + script: + - docker pull $CI_REGISTRY_IMAGE:latest || true + - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest . + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + - docker push $CI_REGISTRY_IMAGE:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2e16f49 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:alpine as golang +WORKDIR /go/src/app +COPY . . +# Static build required so that we can safely copy the binary over. +RUN CGO_ENABLED=0 go install -ldflags '-extldflags "-static"' + +FROM alpine:latest as alpine +RUN apk --no-cache add tzdata zip ca-certificates +WORKDIR /usr/share/zoneinfo +# -0 means no compression. Needed because go's +# tz loader doesn't handle compressed data. +RUN zip -q -r -0 /zoneinfo.zip . + +FROM scratch +# the program: +COPY --from=golang /go/bin/pinbot /app +# the timezone data: +ENV ZONEINFO /zoneinfo.zip +COPY --from=alpine /zoneinfo.zip / +# the tls certificates: +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +ENTRYPOINT ["/app"] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d6b9838 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module pinbot + +go 1.15 + +require ( + github.com/bwmarrin/discordgo v0.22.0 + github.com/joho/godotenv v1.3.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bca381e --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/bwmarrin/discordgo v0.22.0 h1:uBxY1HmlVCsW1IuaPjpCGT6A2DBwRn0nvOguQIxDdFM= +github.com/bwmarrin/discordgo v0.22.0/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6a5b8ab --- /dev/null +++ b/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" + "github.com/bwmarrin/discordgo" + "os" + "os/signal" + "syscall" +) + +import _ "github.com/joho/godotenv/autoload" + +func main() { + token := os.Getenv("DISCORD_TOKEN") + if token == "" { + fmt.Println("Missing discord token (DISCORD_TOKEN), so bye bye!") + return + } + discord, err := discordgo.New("Bot " + token) + if err != nil { + fmt.Println("Error creating Discord session: ", err) + return + } + discord.AddHandler(ready) + discord.AddHandler(reactAdd) + discord.AddHandler(reactRemove) + + err = discord.Open() + if err != nil { + fmt.Println("Error opening Discord session: ", err) + } + + // Wait here until CTRL-C or other term signal is received. + fmt.Println("PinBot is running! Press CTRL-C to exit.") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // Cleanly close down the Discord session. + _ = discord.Close() + +} + +func ready(s *discordgo.Session, event *discordgo.Ready) { + _ = s.UpdateStatus(0, "5x📌 pins a message!") +} + +func reactAdd(s *discordgo.Session, event *discordgo.MessageReactionAdd) { + messageReac(s, event.MessageReaction) +} + +func reactRemove(s *discordgo.Session, event *discordgo.MessageReactionRemove) { + messageReac(s, event.MessageReaction) +} + +func messageReac(s *discordgo.Session, event *discordgo.MessageReaction) { + if event.Emoji.Name == "📌" { + + msg, err := s.ChannelMessage(event.ChannelID, event.MessageID) + if err != nil { + return + } + + for _, reaction := range msg.Reactions { + if reaction.Emoji.Name == "📌" { + var msgErr error + if reaction.Count >= 5 { + msgErr = s.ChannelMessagePin(event.ChannelID, event.MessageID) + } else if reaction.Count <= 3 { + msgErr = s.ChannelMessageUnpin(event.ChannelID, event.MessageID) + } + if msgErr != nil { + fmt.Println("Error: ", msgErr) + } + break + } + } + } +}