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

feat: add command usage counter #377

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions apps/discord-bot/internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (c *commands) IsSystemCommand(commandName string) bool {
func (c *commands) Respond(ctx context.Context, m *discordgo.MessageCreate, cmdName string, messageContent string) {
c.dS.ChannelMessageSend(m.ChannelID, messageContent)
c.setCommandCooldown(m.Author.Username)
c.service.AddBotCommandStatistic(ctx, cmdName)
c.service.SaveCommandActivity(ctx, cmdName, m.GuildID, m.Author.Username, m.Author.ID)
}

Expand Down
8 changes: 8 additions & 0 deletions apps/discord-bot/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Service interface {
DeleteDiscordBotConfig(ctx context.Context, serverId, key string) (bool, error)
GetDiscordBotConfig(ctx context.Context, discordServerId string, configKey string) (*models.DiscordBotConfigs, error)
CheckDiscordBotConfig(ctx context.Context, discordServerId string, configKey string, configValue string) bool

AddBotCommandStatistic(ctx context.Context, commandName string)
}

type service struct {
Expand Down Expand Up @@ -223,3 +225,9 @@ func (s *service) CheckDiscordBotConfig(ctx context.Context, discordServerId str
}

// DISCORD BOT CONFIG

func (s *service) AddBotCommandStatistic(ctx context.Context, commandName string) {
if err := s.DB.AddBotCommandStatistic(ctx, platform.DISCORD, commandName); err != nil {
fmt.Println(err.Error())
}
}
1 change: 1 addition & 0 deletions apps/twitch-bot/internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (c *commands) IsSystemCommand(commandName string) bool {
func (c *commands) Respond(ctx context.Context, message twitch.PrivateMessage, cmdName string, messageContent string) {
c.client.Twitch.Say(message.Channel, messageContent)
c.setCommandCooldown(message.User.Name)
c.service.AddBotCommandStatistic(ctx, cmdName)
c.service.SaveCommandActivity(ctx, cmdName, message.RoomID, message.User.DisplayName, message.User.ID)
}

Expand Down
8 changes: 8 additions & 0 deletions apps/twitch-bot/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Service interface {
CreateCommandAlias(ctx context.Context, commandName string, aliases []string, twitchChannelId string, createdBy string) (*string, error)
CheckCommandAliasExist(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error)
DeleteCommandAlias(ctx context.Context, commandAlias string, twitchChannelId string) (*string, error)

AddBotCommandStatistic(ctx context.Context, commandName string)
}

type services struct {
Expand Down Expand Up @@ -210,3 +212,9 @@ func (s *services) DeleteCommandAlias(ctx context.Context, commandAlias string,

return infoText, nil
}

func (s *services) AddBotCommandStatistic(ctx context.Context, commandName string) {
if err := s.DB.AddBotCommandStatistic(ctx, platform.TWITCH, commandName); err != nil {
fmt.Println(err.Error())
}
}
2 changes: 2 additions & 0 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ type Database interface {
DeleteServerFromDB(ctx context.Context, serverId string) error

// DISCORD

AddBotCommandStatistic(ctx context.Context, botPlatform platform.Platform, commandName string) error
}
18 changes: 18 additions & 0 deletions db/mysql/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package mysql
import (
"context"
"errors"
"log"

"github.com/senchabot-opensource/monorepo/packages/gosenchabot/models"
"github.com/senchabot-opensource/monorepo/packages/gosenchabot/platform"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

func (m *MySQL) CreateBotCommand(ctx context.Context, botPlatform platform.Platform, commandName string, commandContent string, botPlatformId string, createdBy string) (*string, error) {
Expand Down Expand Up @@ -402,3 +404,19 @@ func (m *MySQL) GetCommandList(ctx context.Context, botPlatform platform.Platfor

return botCommandList, nil
}

func (m *MySQL) AddBotCommandStatistic(ctx context.Context, botPlatform platform.Platform, commandName string) error {
botCommandStatistic := models.BotCommandStatistic{CommandName: commandName, BotPlatformType: botPlatform, Count: 1}

result := m.DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.Assignments(map[string]interface{}{"count": gorm.Expr("count + 1")}),
}).Where("bot_platform_type = ?", botPlatform).Where("command_name = ?", commandName).Create(&botCommandStatistic)

if result.Error != nil {
log.Println("(AddBotcommandStatistic): db.Update Error: ", result.Error.Error())
return result.Error
}

return nil
}
6 changes: 6 additions & 0 deletions packages/gosenchabot/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ type TwitchStreamerData struct {
StreamGame string `json:"game_name"`
StartedAt string `json:"started_at"`
}

type BotCommandStatistic struct {
BotPlatformType platform.Platform `gorm:"column:bot_platform_type"`
CommandName string `gorm:"column:command_name"`
Count int `gorm:"column:count"`
}
10 changes: 10 additions & 0 deletions packages/senchabot-prisma/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ model BotCommandAliases {
@@map("bot_command_aliases")
}

model BotCommandStatistic {
id Int @id @default(autoincrement())
botPlatformType String? @map("bot_platform_type")
commandName String @map("command_name")
count Int @default(0) @map("count")

@@unique([botPlatformType, commandName])
@@map("bot_command_statistics")
}

model DiscordServer {
id Int @id @default(autoincrement())
serverId String @unique @map("server_id")
Expand Down