Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main #17

Draft
wants to merge 62 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
a87a2ee
feat(dockerize): add Dockerfile + K8s conf
codeinuit Jul 18, 2020
1455616
feat(dockerization): update dockerfile
codeinuit Jul 19, 2020
053b2f8
Merge pull request #2 from P147x/feat/dockerization
codeinuit Jul 19, 2020
d24490e
fix(dev): fixed dockerfile issues + base structure
codeinuit Jul 19, 2020
4a97a0e
chore(dev): remove useless Makefile
codeinuit Jul 19, 2020
f218b3d
feat(better-logs): add logrus as logger
codeinuit Jul 19, 2020
b6bdeca
feat(better-logs): change fmt to logrus
codeinuit Jul 29, 2020
46f7404
Merge pull request #3 from P147x/feat/better-logs
codeinuit Jul 29, 2020
ffc5b22
Add timer on presence update function
codeinuit Nov 7, 2020
5527099
Merge pull request #5 from P147x/fix/daily-usage-counter
codeinuit Dec 20, 2020
6115508
Add GitHub actions
Jun 29, 2021
bb355ea
Merge pull request #6 from P147x/feat/use-github-actions
codeinuit Jun 29, 2021
338a3d6
Add support for slash commands
codeinuit Aug 18, 2021
b16ae88
Merge pull request #7 from P147x/feature/support-slash-commands
codeinuit Aug 18, 2021
5c89b44
Encapsulate logger + add Zap & Logrus support
codeinuit Jun 30, 2023
058a5b8
Use local logger implementation
codeinuit Jul 3, 2023
67e1447
Update packages
codeinuit Jul 3, 2023
1c8cf2a
Update Golang version for GitHub CI
codeinuit Jul 3, 2023
4de300d
Merge pull request #9 from codeinuit/add-logger-support
codeinuit Jul 3, 2023
815d3cf
Update Golang version in Dockerfile
codeinuit Jul 3, 2023
53ab215
Merge pull request #10 from codeinuit/update-dockerfile-version
codeinuit Jul 3, 2023
6c9c972
Remove private tokens from image providers
codeinuit Jul 3, 2023
e728867
Protected cases without data
codeinuit Jul 3, 2023
3fa2af9
Remove usage counter
codeinuit Jul 3, 2023
4e934d5
Merge pull request #11 from codeinuit/remove-private-tokens
codeinuit Jul 3, 2023
cecf9bb
Update .gitignore to remove vscode & bin files
codeinuit Jul 3, 2023
8b4fb11
Merge pull request #12 from codeinuit/update-gitignore
codeinuit Jul 3, 2023
a17eb2d
Move deployments scripts to /scripts folder
codeinuit Jul 3, 2023
1111aed
Merge pull request #13 from codeinuit/move-deployment-scripts-to-dedi…
codeinuit Jul 3, 2023
1b3cf50
Add Redis support as cache
codeinuit Jul 5, 2023
b15c235
Update go.mod
codeinuit Jul 7, 2023
ea8a170
Merge pull request #14 from codeinuit/support-caching
codeinuit Jul 7, 2023
052ac36
Count usage and display it as Discord game status
codeinuit Jul 7, 2023
0c7bc5c
Update log messages without uppercases
codeinuit Jul 10, 2023
a504831
Reset daily counter every day at midnight
codeinuit Jul 10, 2023
4669b28
Add error handling
codeinuit Jul 11, 2023
724fa27
Merge pull request #15 from codeinuit/handle-daily-counter-as-game-st…
codeinuit Jul 11, 2023
64df2c4
Add linter support on GitHub CI
codeinuit Jul 11, 2023
56f39fb
Fix lint issue
codeinuit Jul 11, 2023
e607cd3
Merge pull request #16 from codeinuit/add-lint-to-github-ci
codeinuit Jul 11, 2023
54f5f8b
Fix daily key not set
codeinuit Jul 14, 2023
2fda3ae
Merge pull request #18 from codeinuit/fix-daily-key
codeinuit Jul 14, 2023
f3a79e3
Create local storage implementation for caching
codeinuit Jul 24, 2023
a7d8ca8
Fix lint issues
codeinuit Jul 24, 2023
beba9ac
Merge pull request #19 from codeinuit/local-storage-implementaion-for…
codeinuit Jul 24, 2023
4699b20
Add main unit tests
codeinuit Jul 25, 2023
adff7af
add base monitoring with prometheus
codeinuit Feb 1, 2024
f788267
Merge pull request #21 from codeinuit/feat/instrumentation
codeinuit Feb 1, 2024
8f5a788
update go version to 1.21
codeinuit Feb 11, 2024
4e02d62
Merge pull request #22 from codeinuit/feat/update-go-version-1-21
codeinuit Feb 11, 2024
cdd6bf0
update golang from 1.19 to 1.21 + rename package
codeinuit Mar 3, 2024
b253217
Merge pull request #23 from codeinuit/refactor/rename-package-and-upd…
codeinuit Mar 3, 2024
4823244
feat(logger) support formatting
codeinuit Mar 6, 2024
a52e649
Merge pull request #24 from codeinuit/feat/update-logger-to-support-f…
codeinuit Mar 6, 2024
564801b
chore(main) remove unused calls + use local cache by default
codeinuit Mar 6, 2024
6cc974a
Merge pull request #25 from codeinuit/chore/clean-and-remove-unused-c…
codeinuit Mar 6, 2024
8386128
(clean) remove deprecated commands
codeinuit May 12, 2024
79bd069
Merge pull request #26 from codeinuit/remove-deprecated-commands
codeinuit May 12, 2024
1c9e86f
(feat) add debug wrap for logger
codeinuit May 12, 2024
83f0472
Merge pull request #27 from codeinuit/add-debug-support-on-loggers
codeinuit May 12, 2024
b53907c
add debug logging
codeinuit May 12, 2024
cd224c3
Merge pull request #28 from codeinuit/add-debug-logging-to-load-cmd
codeinuit May 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Golang CI
on: [push, pull_request]
jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Build
run: make
- name: Test
run: make test

55 changes: 55 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: golangci-lint
on:
push:
branches:
- master
- dev
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.53

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
31 changes: 31 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Deploy
on:
push:
branches: ['dev']

env:
IMAGE_NAME: 'codeinu/shibesbot'

jobs:
build-and-push-images:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Log in to the Container registry
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ env.IMAGE_NAME }}:dev
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

.vscode
bin/
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM golang:1.21 AS build
WORKDIR $GOPATH/src/github.com/codeinuit/shibesbot
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o app ./cmd/shibesbot

FROM alpine:3.19
WORKDIR /root/
COPY --from=build /go/src/github.com/codeinuit/shibesbot/app .
ENTRYPOINT ["./app"]
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
BINARY = shibes

SRC_DIRECTORY = src/
BIN_DIRECTORY = .
SRC = $(SRC_DIRECTORY)shibes.go \
$(SRC_DIRECTORY)discord.go \
$(SRC_DIRECTORY)dogequests.go \
BIN_DIRECTORY = bin


all: $(BINARY)

$(BINARY): clean
go build -o $(BIN_DIRECTORY)/$(BINARY) $(SRC)
go build -o $(BIN_DIRECTORY)/$(BINARY) cmd/shibesbot/*.go

test:
go test ./... -v

clean:
rm -f bin/$(BINARY)
Expand Down
136 changes: 136 additions & 0 deletions cmd/shibesbot/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"context"
"fmt"
"strconv"
"time"

"github.com/bwmarrin/discordgo"
)

var (
commands = []*discordgo.ApplicationCommand{
{
Name: "shibes",
Description: "Returns an image of a Shiba",
Options: []*discordgo.ApplicationCommandOption{

{
Type: discordgo.ApplicationCommandOptionString,
Name: "count",
Description: "Ask for more pictures in one request",
Required: false,
},
},
},
{
Name: "shelp",
Description: "Returns helper",
},
}
)

func (sb *Shibesbot) initDiscord() error {
var err error

sb.session, err = discordgo.New("Bot " + sb.apiConfigurations.discordToken)
if err != nil {
return err
}

sb.session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { sb.commandPicker(s, i) })
sb.session.AddHandlerOnce(func(s *discordgo.Session, i *discordgo.Ready) {
t := time.Now()
key := fmt.Sprintf("usage:%d%d%d", t.Day(), t.Month(), t.Year())
sb.dailyKey = key

count, err := sb.cache.Get(context.Background(), sb.dailyKey)
if err != nil {
sb.log.Warn("could not get daily counter from cache : ", err.Error())
return
}

countString, ok := count.(string)
if !ok {
sb.log.Warn("could not get daily counter from cache : conversion error")
return
}
countInt, err := strconv.Atoi(countString)
if err != nil {
sb.log.Warn("could not get daily counter from cache : ", err.Error())
return
}

sb.setDailyCounter(int64(countInt))
})

if err = sb.session.Open(); err != nil {
return err
}

sb.log.Debug("updating Discord command list")
for _, cmd := range commands {
sb.log.Debugf("adding command %s", cmd.Name)
if _, err := sb.session.ApplicationCommandCreate(sb.session.State.User.ID, "", cmd); err != nil {
return err
}
}

return nil
}

func (sb *Shibesbot) commandPicker(s *discordgo.Session, i *discordgo.InteractionCreate) {
sb.log.Info("command received: " + i.ApplicationCommandData().Name)
var response string
switch i.ApplicationCommandData().Name {
case "shibes":
response = sb.getShibes()
case "shelp":
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{getHelp()},
},
})
if err != nil {
sb.log.Error("could not answer to user help command: ", err.Error())
}
}

if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
}); err != nil {
sb.log.Error("could not answer to user help command: ", err.Error())
return
}

sb.updateDailyCounter()
}

func (sb *Shibesbot) updateDailyCounter() {
sb.mtx.RLock()
defer sb.mtx.RUnlock()
count, err := sb.cache.Incr(context.Background(), sb.dailyKey)
if err != nil {
sb.log.Warn("could not get daily counter from cache : ", err.Error())
return
}
countInt, ok := count.(int64)
if !ok {
sb.log.Warn("could not get daily counter from cache")
return
}

sb.setDailyCounter(countInt)
}

func (sb *Shibesbot) setDailyCounter(count int64) {
err := sb.session.UpdateGameStatus(0, fmt.Sprintf("used %d times today", count))
if err != nil {
sb.log.Warn("could not update daily counter: ", err.Error())
}
}
65 changes: 65 additions & 0 deletions cmd/shibesbot/dogequests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"net/http"

"encoding/json"

"github.com/bwmarrin/discordgo"
)

var (
Shibes ShibesData
)

type ShibesPictures struct {
Shibes []string
Total int
Cursor int
}

type ShibesData struct {
Images ShibesPictures
}

func (sb *Shibesbot) getShibes() string {
if Shibes.Images.Cursor >= Shibes.Images.Total {
Shibes.Images.Cursor = 0
Shibes.Images.Total = 10
resp, err := http.Get("http://shibe.online/api/shibes?count=10")
if err != nil {
sb.log.Warn("could not get images from shibes.online: ", err.Error())
return ""
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&Shibes.Images.Shibes)
if err != nil {
sb.log.Warn("could not get images from shibes.online: ", err.Error())
return ""
}
sb.log.Info("Updated ", Shibes.Images.Total, " pictures from shibes.online")
}
Shibes.Images.Cursor++
return Shibes.Images.Shibes[Shibes.Images.Cursor-1]
}

func getHelp() *discordgo.MessageEmbed {
test := &discordgo.MessageEmbed{
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: "http://img.over-blog-kiwi.com/1/47/73/14/20160709/ob_bcc896_chiot-shiba-inu-a-vendre-2016.jpg",
},
Description: "Thanks for using Shibesbot on your Discord server !\n\n" +
"Our purpose is to distribute many **shibes** on your server, using http://shibe.online/ as puppy distributor.\n\n" +
"Are you enjoying this bot ? You can help us spread the doge ! https://github.com/P147x/discord-shibesbot",
Fields: []*discordgo.MessageEmbedField{
{
Name: "Available commands",
Value: "- *shibes* to get a random shibe !\n" +
"- *shelp* to get help",
Inline: false,
},
},
Title: "Hello shibes !",
}
return test
}
Loading
Loading