Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
feat(joke): removed adding joke from Bot level
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed May 31, 2024
1 parent a0af0c3 commit b804c10
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 70 deletions.
2 changes: 0 additions & 2 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func createCommands(
guildVoiceChats map[string]voice.ChatInfo,
) map[string]command.DiscordSlashCommandHandler {
welcome := command.WelcomeCommand{}
addJoke := command.AddJokeCommand{Service: services[databaseServiceID].(dbJoke.Database)}
getJoke := command.JokeCommand{Services: services}
spock := command.SpockCommand{
GlobalCtx: globalCtx,
Expand All @@ -151,7 +150,6 @@ func createCommands(

return map[string]command.DiscordSlashCommandHandler{
command.WelcomeCommandName: welcome,
command.AddJokeCommandName: addJoke,
command.GetJokeCommandName: getJoke,
command.SpockCommandName: spock,
command.StopCommandName: stop,
Expand Down
66 changes: 0 additions & 66 deletions bot/command/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ const (
idOptionKey = "id"
categoryOptionKey = "category"
typeOptionKey = "type"
questionOptionKey = "question"
answerOptionKey = "answer"
)

const (
AddJokeCommandName = "add-joke"
GetJokeCommandName = "joke"
)

Expand Down Expand Up @@ -295,69 +292,6 @@ func buttonReactions() []discordgo.MessageComponent {
}
}

type AddJokeCommand struct {
Service joke.AddService
}

func (a AddJokeCommand) Command() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: AddJokeCommandName,
Description: "Add new joke to server database",
GuildID: os.Getenv("SERVER_GUID"),
Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{
jokeCategoryOption(true),
jokeTypeOption(true),
{
Name: answerOptionKey,
Description: "Main part of joke",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
{
Name: questionOptionKey,
Description: "Question part in two-part joke",
Type: discordgo.ApplicationCommandOptionString,
Required: false,
},
},
}
}

func (a AddJokeCommand) Execute(ctx context.Context, _ *discordgo.Session, i *discordgo.InteractionCreate) (DiscordMessageReceiver, error) {
newJoke := jokeFromOptions(i.Data.(discordgo.ApplicationCommandInteractionData))

if newJoke.Answer == "" {
return nil, DiscordError{Err: errors.New("joke: missing answer"), Msg: "Zrujnowałeś ten żart, Panie Kapitanie"}
}

newJoke.ID = primitive.NewObjectID()

id, err := a.Service.Add(ctx, newJoke)
if err != nil {
return nil, err
}

return SimpleMessage{Msg: fmt.Sprintf("BEEP BOOP. Dodałem twój żart panie Kapitanie. Jego ID to `%s`", id), Hidden: true}, nil
}

func jokeFromOptions(data discordgo.ApplicationCommandInteractionData) (j joke.Joke) {
for _, o := range data.Options {
switch o.Name {
case categoryOptionKey:
j.Category = joke.Category(o.Value.(string))
case typeOptionKey:
j.Type = joke.Type(o.Value.(string))
case answerOptionKey:
j.Answer = o.Value.(string)
case questionOptionKey:
j.Question = o.Value.(string)
}
}

return
}

type ApologiesOption struct{}

func (a ApologiesOption) Match(customID string) bool {
Expand Down
6 changes: 4 additions & 2 deletions server/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func newJoke(j *komputer.Joke) (new joke.Joke, err error) {
new.Type, err = joke.RawType(j.Type)
new.Answer = j.Answer
new.GuildID = j.GuildId
new.ID = primitive.NewObjectID()

if j.Question != nil {
new.Question = *j.Question
Expand All @@ -105,8 +106,9 @@ func searchParams(params apiJokeParams) (p joke.SearchParams, err error) {
return
}

if params.GetId() != nil {
p.ID, err = primitive.ObjectIDFromHex(params.GetId().ObjectId)
id := params.GetId()
if id != nil {
p.ID, err = primitive.ObjectIDFromHex(id.ObjectId)
}

p.Type, err = joke.RawType(params.GetType())
Expand Down

0 comments on commit b804c10

Please sign in to comment.