This commit is contained in:
Julius 2020-09-07 14:30:18 +02:00
commit 19541281f2
6 changed files with 138 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea/

20
.gitlab-ci.yml Normal file
View file

@ -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

22
Dockerfile Normal file
View file

@ -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"]

8
go.mod Normal file
View file

@ -0,0 +1,8 @@
module pinbot
go 1.15
require (
github.com/bwmarrin/discordgo v0.22.0
github.com/joho/godotenv v1.3.0
)

8
go.sum Normal file
View file

@ -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=

79
main.go Normal file
View file

@ -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
}
}
}
}