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 custom command variable system #533

Merged
merged 1 commit into from
Jan 24, 2025
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
2 changes: 1 addition & 1 deletion apps/discord-bot/internal/command/addcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (c *commands) AddCommandCommand(context context.Context, m *discordgo.Messa
return nil, errors.New("dont have permission")
}

return command.AcmdCommand(context, c.service.CreateCommand, c.IsSystemCommand, *msgData, commandName, params)
return command.AcmdCommand(context, c.service.CreateCommand, c.service.GetCustomVariableContent, c.IsSystemCommand, *msgData, commandName, params)
}
28 changes: 28 additions & 0 deletions apps/discord-bot/internal/command/addcommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package command

import (
"context"
"errors"

"github.com/bwmarrin/discordgo"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) AddCommandVariableCommand(context context.Context, m *discordgo.MessageCreate, commandName string, params []string) (*model.CommandResponse, error) {
msgData := &model.MessageData{
PlatformEntityID: m.GuildID,
UserName: m.Author.Username,
}

p, err := c.dS.UserChannelPermissions(m.Author.ID, m.ChannelID)
if err != nil {
return nil, err
}

if p&discordgo.PermissionManageChannels != discordgo.PermissionManageChannels {
return nil, errors.New("dont have permission")
}

return command.AddCommandVariableCommand(context, c.service.CreateCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
9 changes: 7 additions & 2 deletions apps/discord-bot/internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (c *commands) GetSystemCommands() SysCommandMap {
"acmda": c.AddCommandAliasCommand,
"dcmda": c.DeleteCommandAliasCommand,
"sozluk": c.SozlukCommand,

"acmdvar": c.AddCommandVariableCommand,
"ucmdvar": c.UpdateCommandVariableCommand,
"dcmdvar": c.DeleteCommandVariableCommand,
"lcmdvar": c.ListCommandVariablesCommand,
}

return commands
Expand Down Expand Up @@ -113,7 +118,7 @@ func (c *commands) runCustomCommand(ctx context.Context, cmdName string, mC *dis
}
if cmdData != nil {
cmdVar := helpers.GetCommandVariables(c.dS, cmdData, mC)
formattedCommandContent := helper.FormatCommandContent(cmdVar)
formattedCommandContent := helper.FormatCommandContent(cmdVar, c.service)
c.Respond(ctx, mC, cmdName, formattedCommandContent)
return
}
Expand Down Expand Up @@ -167,7 +172,7 @@ func (c *commands) Run(ctx context.Context, cmdName string, params []string, m *
}

cmdVar := helpers.GetCommandVariables(c.dS, cmdData, m)
formattedCommandContent := helper.FormatCommandContent(cmdVar)
formattedCommandContent := helper.FormatCommandContent(cmdVar, c.service)
c.Respond(ctx, m, cmdName, formattedCommandContent)
// GLOBAL COMMANDS
}
Expand Down
28 changes: 28 additions & 0 deletions apps/discord-bot/internal/command/deletecommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package command

import (
"context"
"errors"

"github.com/bwmarrin/discordgo"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) DeleteCommandVariableCommand(context context.Context, m *discordgo.MessageCreate, commandName string, params []string) (*model.CommandResponse, error) {
msgData := &model.MessageData{
PlatformEntityID: m.GuildID,
UserName: m.Author.Username,
}

p, err := c.dS.UserChannelPermissions(m.Author.ID, m.ChannelID)
if err != nil {
return nil, err
}

if p&discordgo.PermissionManageChannels != discordgo.PermissionManageChannels {
return nil, errors.New("dont have permission")
}

return command.DeleteCommandVariableCommand(context, c.service.DeleteCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
3 changes: 3 additions & 0 deletions apps/discord-bot/internal/command/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/senchabot-opensource/monorepo/helper"
"github.com/senchabot-opensource/monorepo/model"
"github.com/senchabot-opensource/monorepo/platform"
)

func GetCommandVariables(dS *discordgo.Session, cmdData *model.BotCommand, m *discordgo.MessageCreate) *model.CommandVariable {
Expand All @@ -26,6 +27,8 @@ func GetCommandVariables(dS *discordgo.Session, cmdData *model.BotCommand, m *di
CurrentDate: &m.Timestamp,
CommandCreatedAt: cmdData.CreatedAt,
ChannelName: channelName,
BotPlatform: platform.DISCORD,
BotPlatformID: m.GuildID,
}
}

Expand Down
28 changes: 28 additions & 0 deletions apps/discord-bot/internal/command/listcommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package command

import (
"context"
"errors"

"github.com/bwmarrin/discordgo"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) ListCommandVariablesCommand(context context.Context, m *discordgo.MessageCreate, commandName string, params []string) (*model.CommandResponse, error) {
msgData := &model.MessageData{
PlatformEntityID: m.GuildID,
UserName: m.Author.Username,
}

p, err := c.dS.UserChannelPermissions(m.Author.ID, m.ChannelID)
if err != nil {
return nil, err
}

if p&discordgo.PermissionManageChannels != discordgo.PermissionManageChannels {
return nil, errors.New("dont have permission")
}

return command.ListCommandVariablesCommand(context, c.service.ListCommandVariables, *msgData)
}
2 changes: 1 addition & 1 deletion apps/discord-bot/internal/command/updatecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func (c *commands) UpdateCommandCommand(context context.Context, m *discordgo.Me
return nil, errors.New("dont have permission")
}

return command.UcmdCommand(context, c.service.UpdateCommand, c.IsSystemCommand, *msgData, commandName, params)
return command.UcmdCommand(context, c.service.UpdateCommand, c.service.GetCustomVariableContent, c.IsSystemCommand, *msgData, commandName, params)
}
28 changes: 28 additions & 0 deletions apps/discord-bot/internal/command/updatecommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package command

import (
"context"
"errors"

"github.com/bwmarrin/discordgo"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) UpdateCommandVariableCommand(context context.Context, m *discordgo.MessageCreate, commandName string, params []string) (*model.CommandResponse, error) {
msgData := &model.MessageData{
PlatformEntityID: m.GuildID,
UserName: m.Author.Username,
}

p, err := c.dS.UserChannelPermissions(m.Author.ID, m.ChannelID)
if err != nil {
return nil, err
}

if p&discordgo.PermissionManageChannels != discordgo.PermissionManageChannels {
return nil, errors.New("dont have permission")
}

return command.UpdateCommandVariableCommand(context, c.service.UpdateCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
32 changes: 32 additions & 0 deletions apps/discord-bot/internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ type Service interface {
CheckDiscordBotConfig(ctx context.Context, discordServerId string, configKey string, configValue string) bool

AddBotCommandStatistic(ctx context.Context, commandName string)

// Command Variable methods
GetCommandVariable(ctx context.Context, varName string, botPlatformId string) (*model.BotCommandVariable, error)
CreateCommandVariable(ctx context.Context, varName string, varContent string, botPlatformId string, createdBy string) error
UpdateCommandVariable(ctx context.Context, varName string, varContent string, botPlatformId string, updatedBy string) error
DeleteCommandVariable(ctx context.Context, varName string, botPlatformId string, updatedBy string) error
ListCommandVariables(ctx context.Context, botPlatformId string) ([]*model.BotCommandVariable, error)
GetCustomVariableContent(ctx context.Context, botPlatformId string, varName string) string
}

type service struct {
Expand Down Expand Up @@ -271,3 +279,27 @@ func (s *service) AddBotCommandStatistic(ctx context.Context, commandName string
log.Println("[service.AddBotCommandStatistic] AddBotCommandStatistic error:", err.Error())
}
}

func (s *service) GetCommandVariable(ctx context.Context, varName string, botPlatformId string) (*model.BotCommandVariable, error) {
return s.DB.GetCommandVariable(ctx, varName, platform.DISCORD, botPlatformId)
}

func (s *service) CreateCommandVariable(ctx context.Context, varName string, varContent string, botPlatformId string, createdBy string) error {
return s.DB.CreateCommandVariable(ctx, varName, varContent, platform.DISCORD, botPlatformId, createdBy)
}

func (s *service) UpdateCommandVariable(ctx context.Context, varName string, varContent string, botPlatformId string, updatedBy string) error {
return s.DB.UpdateCommandVariable(ctx, varName, varContent, platform.DISCORD, botPlatformId, updatedBy)
}

func (s *service) DeleteCommandVariable(ctx context.Context, varName string, botPlatformId string, updatedBy string) error {
return s.DB.DeleteCommandVariable(ctx, varName, platform.DISCORD, botPlatformId, updatedBy)
}

func (s *service) ListCommandVariables(ctx context.Context, botPlatformId string) ([]*model.BotCommandVariable, error) {
return s.DB.ListCommandVariables(ctx, platform.DISCORD, botPlatformId)
}

func (s *service) GetCustomVariableContent(ctx context.Context, botPlatformId string, varName string) string {
return s.DB.GetCustomVariableContent(ctx, platform.DISCORD, botPlatformId, varName)
}
2 changes: 1 addition & 1 deletion apps/twitch-bot/internal/command/addcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func (c *commands) AddCommandCommand(context context.Context, message twitch.Pri
UserName: message.User.DisplayName,
}

return command.AcmdCommand(context, c.service.CreateCommand, c.IsSystemCommand, *msgData, commandName, params)
return command.AcmdCommand(context, c.service.CreateCommand, c.service.GetCustomVariableContent, c.IsSystemCommand, *msgData, commandName, params)
}
25 changes: 25 additions & 0 deletions apps/twitch-bot/internal/command/addcommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"context"
"errors"

"github.com/gempir/go-twitch-irc/v3"
"github.com/senchabot-opensource/monorepo/apps/twitch-bot/internal/command/helpers"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/config"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) AddCommandVariableCommand(context context.Context, message twitch.PrivateMessage, commandName string, params []string) (*model.CommandResponse, error) {
if !helpers.CanExecuteCommand(context, c.service, message.Tags["badges"], message.RoomID) {
return nil, errors.New(message.User.DisplayName + config.CannotExecuteCommand)
}

msgData := &model.MessageData{
PlatformEntityID: message.RoomID,
UserName: message.User.DisplayName,
}

return command.AddCommandVariableCommand(context, c.service.CreateCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
9 changes: 7 additions & 2 deletions apps/twitch-bot/internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (c *commands) GetCommands() CommandMap {
"timers": c.TimersCommand,
"timer": c.TimerCommand,

"acmdvar": c.AddCommandVariableCommand,
"ucmdvar": c.UpdateCommandVariableCommand,
"dcmdvar": c.DeleteCommandVariableCommand,
"lcmdvar": c.ListCommandVariablesCommand,

"help": c.HelpCommand,
}

Expand All @@ -90,7 +95,7 @@ func (c *commands) runCustomCommand(ctx context.Context, cmdName string, privMsg
}
if cmdData != nil {
cmdVar := helpers.GetCommandVariables(cmdData, privMsg)
formattedCommandContent := helper.FormatCommandContent(cmdVar)
formattedCommandContent := helper.FormatCommandContent(cmdVar, c.service)
c.Respond(ctx, privMsg, cmdName, formattedCommandContent)
return
}
Expand Down Expand Up @@ -149,7 +154,7 @@ func (c *commands) Run(ctx context.Context, cmdName string, params []string, pri
}

cmdVar := helpers.GetCommandVariables(cmdData, privMsg)
formattedCommandContent := helper.FormatCommandContent(cmdVar)
formattedCommandContent := helper.FormatCommandContent(cmdVar, c.service)
c.Respond(ctx, privMsg, cmdName, formattedCommandContent)
// GLOBAL COMMANDS
}
Expand Down
25 changes: 25 additions & 0 deletions apps/twitch-bot/internal/command/deletecommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"context"
"errors"

"github.com/gempir/go-twitch-irc/v3"
"github.com/senchabot-opensource/monorepo/apps/twitch-bot/internal/command/helpers"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/config"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) DeleteCommandVariableCommand(context context.Context, message twitch.PrivateMessage, commandName string, params []string) (*model.CommandResponse, error) {
if !helpers.CanExecuteCommand(context, c.service, message.Tags["badges"], message.RoomID) {
return nil, errors.New(message.User.DisplayName + config.CannotExecuteCommand)
}

msgData := &model.MessageData{
PlatformEntityID: message.RoomID,
UserName: message.User.DisplayName,
}

return command.DeleteCommandVariableCommand(context, c.service.DeleteCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
3 changes: 3 additions & 0 deletions apps/twitch-bot/internal/command/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gempir/go-twitch-irc/v3"
"github.com/senchabot-opensource/monorepo/apps/twitch-bot/internal/service"
"github.com/senchabot-opensource/monorepo/model"
"github.com/senchabot-opensource/monorepo/platform"
)

func GetCommandVariables(cmdData *model.BotCommand, message twitch.PrivateMessage) *model.CommandVariable {
Expand All @@ -16,6 +17,8 @@ func GetCommandVariables(cmdData *model.BotCommand, message twitch.PrivateMessag
CurrentDate: &message.Time,
CommandCreatedAt: cmdData.CreatedAt,
ChannelName: message.Channel,
BotPlatform: platform.TWITCH,
BotPlatformID: message.RoomID,
}
}

Expand Down
25 changes: 25 additions & 0 deletions apps/twitch-bot/internal/command/listcommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"context"
"errors"

"github.com/gempir/go-twitch-irc/v3"
"github.com/senchabot-opensource/monorepo/apps/twitch-bot/internal/command/helpers"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/config"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) ListCommandVariablesCommand(context context.Context, message twitch.PrivateMessage, commandName string, params []string) (*model.CommandResponse, error) {
if !helpers.CanExecuteCommand(context, c.service, message.Tags["badges"], message.RoomID) {
return nil, errors.New(message.User.DisplayName + config.CannotExecuteCommand)
}

msgData := &model.MessageData{
PlatformEntityID: message.RoomID,
UserName: message.User.DisplayName,
}

return command.ListCommandVariablesCommand(context, c.service.ListCommandVariables, *msgData)
}
2 changes: 1 addition & 1 deletion apps/twitch-bot/internal/command/updatecommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func (c *commands) UpdateCommandCommand(context context.Context, message twitch.
UserName: message.User.DisplayName,
}

return command.UcmdCommand(context, c.service.UpdateCommand, c.IsSystemCommand, *msgData, commandName, params)
return command.UcmdCommand(context, c.service.UpdateCommand, c.service.GetCustomVariableContent, c.IsSystemCommand, *msgData, commandName, params)
}
25 changes: 25 additions & 0 deletions apps/twitch-bot/internal/command/updatecommandvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"context"
"errors"

"github.com/gempir/go-twitch-irc/v3"
"github.com/senchabot-opensource/monorepo/apps/twitch-bot/internal/command/helpers"
"github.com/senchabot-opensource/monorepo/command"
"github.com/senchabot-opensource/monorepo/config"
"github.com/senchabot-opensource/monorepo/model"
)

func (c *commands) UpdateCommandVariableCommand(context context.Context, message twitch.PrivateMessage, commandName string, params []string) (*model.CommandResponse, error) {
if !helpers.CanExecuteCommand(context, c.service, message.Tags["badges"], message.RoomID) {
return nil, errors.New(message.User.DisplayName + config.CannotExecuteCommand)
}

msgData := &model.MessageData{
PlatformEntityID: message.RoomID,
UserName: message.User.DisplayName,
}

return command.UpdateCommandVariableCommand(context, c.service.UpdateCommandVariable, c.service.GetCommandVariable, *msgData, commandName, params)
}
Loading