Skip to content

Commit

Permalink
feat(mongodb): added guildID to joke database structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Oct 29, 2023
1 parent 677a724 commit 640a963
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/command/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ var (
)

func executeAddJokeCommand(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
var j mongo.JokeDB
j := mongo.JokeDB{
GuildID: i.GuildID,
}

for _, o := range i.Data.(discordgo.ApplicationCommandInteractionData).Options {
switch o.Name {
Expand Down
2 changes: 2 additions & 0 deletions internal/interaction/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
func SendJoke(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, t types.JokeType, c types.JokeCategory) {
var msg *discordgo.InteractionResponseData

ctx = context.WithValue(ctx, "GUILD_ID", i.GuildID)

switch t {
case types.Single:
j, err := getSingleTypeJokeGenerator()(ctx, c)
Expand Down
16 changes: 13 additions & 3 deletions internal/mongo/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type JokeDB struct {
ContentRes string `bson:"content"`
Type types.JokeType `bson:"type"`
Category types.JokeCategory `bson:"category"`
GuildID string `bson:"guild_id"`
ExternalID int64 `bson:"externalID"`
}

Expand Down Expand Up @@ -83,9 +84,18 @@ func findRandomJoke(ctx context.Context, j JokeSearch) (JokeDB, error) {
}},
}},
{{
"$match", bson.D{{
"type", jokeType,
}},
"$match", bson.D{
{
"type", jokeType,
}, {
"$or", []bson.D{
{{
"guild_id", "",
}},
{{
"guild_id", ctx.Value("GUILD_ID"),
}}},
}},
}},
}

Expand Down

0 comments on commit 640a963

Please sign in to comment.