Skip to content

Commit

Permalink
Merge pull request #8 from Kesuaheli/feat/advent_calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli authored Nov 26, 2023
2 parents 851adf1 + 9b7e85b commit b3890f5
Show file tree
Hide file tree
Showing 48 changed files with 1,537 additions and 462 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/gotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"

- name: Set up dependencies
run: go mod download

- name: Build
run: go build -v ./...
run: |
go mod download
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/lint/golint@latest
- name: Run go vet
run: go vet -v ./...

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...

- name: Install golint
run: go install golang.org/x/lint/golint@latest

- name: Run golint
run: golint -set_exit_status ./...

- name: Build
run: go build -v ./...

- name: Run tests
run: go test -v -race -vet=off ./...
13 changes: 12 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ youtube:
- UC6sb0bkXREewXp2AkSOsOqg # Taomi

event:
birthday_hour: 8 # Time to trigger daily birthday check (24h format)
# Time (24h format) to trigger daily events like birthday check and advent calendar post
morning_hour: 8
morning_minute: 0

adventcalendar:
images: data/images/adventcalendar
# Name: The name of this emoji, e.g. '🎅', '❤️' when a default emoji
# ID: The snowflake ID if when a custom emoji
# Animated: Whether this emoji is animated. Defaults to false
emoji.name:
#emoji.id:
#emoji.animated: true

webserver:
favicon: webserver/favicon.png
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package config

import (
"log"
logger "log"

"cake4everybot/data/lang"

"github.com/spf13/viper"
)

var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix)

// Load loads the given configuration file as the global config. It
// also loads:
// - the languages from lang.Load() (see cake4everybot/data/lang)
Expand Down
16 changes: 16 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ discord.command:
latency: Latenz
version: Version

adventcalendar:
base: adventskalender
base.description: Admin Commands für das Adventskalender Giveaway

module:
adventcalendar:
post.message: Noch %d Mal schlafen bis Heilig Abend! Heute öffnet sich das **Türchen %d**.
post.message.day_23: Fast geschafft! Nur noch einmal schlafen!
post.message.day_24: Ho Ho Ho! Heute ist Heilig Abend!
post.message2: Klicke unten auf den Knopf um dich im Gewinnspiel einzutragen!
post.button: Gewinnspiel

enter.invalid: Das ist eine alte Nachricht, du kannst dich hier nicht mehr eintragen!
enter.success: Du hast dich erfolgreich eingetragen! Du hast jetzt %d Tickets.
enter.already_entered: Du hast dich heute bereits eingetragen! (Du hast momentan %d Tickets)

youtube:
embed_footer: YouTube Glocke
msg.new_vid: "%s hat ein neues Video hochgeladen"
16 changes: 16 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ discord.command:
latency: Latency
version: Version

adventcalendar:
base: adventcalendar
base.description: Admin commands for the Advent Calendar Giveaway

module:
adventcalendar:
post.message: Just sleep %d more times! Its time for **door %d**.
post.message.day_23: Almost there! Just sleep once more!
post.message.day_24: Ho Ho Ho! Its Christmas Eve!
post.message2: Click the button below to join the Giveaway!
post.button: Join

enter.invalid: This is an old message, you cannot join here anymore!
enter.success: You successfully joined! You know have %d tickets.
enter.already_entered: You already joined for today. (You have %d tickets)

youtube:
embed_footer: YouTube notification bell
msg.new_vid: "%s just uploaded a new video"
3 changes: 2 additions & 1 deletion data/lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package lang

import (
"log"
logger "log"
"strings"

"github.com/spf13/viper"
)

var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix)
var langsMap = map[string]*viper.Viper{}

// Unify takes and returns a string wich defines a language, i.e.
Expand Down
3 changes: 2 additions & 1 deletion database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ package database
import (
"database/sql"
"fmt"
"log"
logger "log"
"time"

// mysql driver used for database
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
)

var log = logger.New(logger.Writer(), "[Config] ", logger.LstdFlags|logger.Lmsgprefix)
var db *sql.DB

type connectionConfig struct {
Expand Down
112 changes: 101 additions & 11 deletions event/command/commandBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,125 @@

package command

import "github.com/bwmarrin/discordgo"
import (
"cake4everybot/modules/adventcalendar"
"cake4everybot/modules/birthday"
"cake4everybot/modules/info"
"cake4everybot/util"
"fmt"
"log"
"strings"

// Command is an interface wrapper for all commands. Including chat-
// comamnds (slash-commands), message-commands, and user-commands.
"github.com/bwmarrin/discordgo"
)

// Command is an interface wrapper for all commands. Including chat-comamnds (slash-commands),
// message-commands, and user-commands.
type Command interface {
// Definition of a command.
// E.g., name, description, options, subcommands.
AppCmd() *discordgo.ApplicationCommand

// Function of a command.
// All things that should happen at execution.
CmdHandler() func(s *discordgo.Session, i *discordgo.InteractionCreate)
Handle(s *discordgo.Session, i *discordgo.InteractionCreate)

// Sets the registered command ID for internal uses after uploading to discord
SetID(id string)
// Gets the registered command ID
GetID() string
}

// CommandMap holds all active commands. It maps them from a unique
// name identifier to the corresponding Command.
// CommandMap holds all active commands. It maps them from a unique name identifier to the
// corresponding Command.
//
// Here the name is used, because Discord uses the name too to
// identify seperate commands. When a command is beeing registered
// with a name that already is beeing registerd as a command by this
// application (bot), then the new one will simply overwrite it and
// automatically ungerister the old one.
// Here the name is used, because Discord uses the name too to identify seperate commands. When a
// command is beeing registered with a name that already is beeing registerd as a command by this
// application (bot), then the new one will simply overwrite it and automatically ungerister the old
// one.
var CommandMap map[string]Command

func init() {
CommandMap = make(map[string]Command)
}

// Register registers all application commands
func Register(s *discordgo.Session, guildID string) error {

// This is the list of commands to use. Add a command via simply appending the struct (which
// must implement the Command interface) to the list, i.e.:
//
// commandsList = append(commandsList, command.MyCommand{})
var commandsList []Command

// chat (slash) commands
commandsList = append(commandsList, &birthday.Chat{})
commandsList = append(commandsList, &info.Chat{})
commandsList = append(commandsList, &adventcalendar.Chat{})
// messsage commands
// user commands
commandsList = append(commandsList, &birthday.UserShow{})

// early return when there're no commands to add, and remove all previously registered commands
if len(commandsList) == 0 {
removeUnusedCommands(s, guildID, nil)
return nil
}

// make an array of ApplicationCommands and perform a bulk change using it
appCommandsList := make([]*discordgo.ApplicationCommand, 0, len(commandsList))
for _, cmd := range commandsList {
appCommandsList = append(appCommandsList, cmd.AppCmd())
CommandMap[cmd.AppCmd().Name] = cmd
}
commandNames := make([]string, 0, len(CommandMap))
for k := range CommandMap {
commandNames = append(commandNames, k)
}

log.Printf("Adding used commands: [%s]...\n", strings.Join(commandNames, ", "))
createdCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, guildID, appCommandsList)
if err != nil {
return fmt.Errorf("failed on bulk overwrite commands: %v", err)
}

for _, cmd := range createdCommands {
CommandMap[cmd.Name].SetID(cmd.ID)
}

removeUnusedCommands(s, guildID, createdCommands)

// set the utility map
cmdIDMap := make(map[string]string)
for k, v := range CommandMap {
cmdIDMap[k] = v.GetID()
}
util.SetCommandMap(cmdIDMap)

return err
}

func removeUnusedCommands(s *discordgo.Session, guildID string, createdCommands []*discordgo.ApplicationCommand) {
allRegisteredCommands, err := s.ApplicationCommands(s.State.User.ID, guildID)
if err != nil {
log.Printf("Error while removing unused commands: Could not get registered commands from guild '%s'. Err: %v\n", guildID, err)
return
}
newCmdIds := make(map[string]bool)
for _, cmd := range createdCommands {
newCmdIds[cmd.ID] = true
}

// Find unused commands by iterating over all commands and check if the ID is in the currently registered commands. If not, remove it.
for _, cmd := range allRegisteredCommands {
if !newCmdIds[cmd.ID] {
err = s.ApplicationCommandDelete(s.State.User.ID, guildID, cmd.ID)
if err != nil {
log.Printf("Error while removing unused commands: Could not delete comand '%s' ('/%s'). Err: %v\n", cmd.ID, cmd.Name, err)
continue
}
log.Printf("Removed unused command: '/%s'\n", cmd.Name)
}
}

}
Loading

0 comments on commit b3890f5

Please sign in to comment.