Skip to content

Commit

Permalink
Added Trivia (#1600)
Browse files Browse the repository at this point in the history
* added trivia

* added trivia

---------

Co-authored-by: Ashish Jhanwar <ashishjh-bst@users.noreply.github.com>
  • Loading branch information
ashishjh-bst and ashishjh-bst authored Jan 4, 2024
1 parent 73a17ee commit 961776e
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/yagpdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/botlabs-gg/yagpdb/v2/common/prom"
"github.com/botlabs-gg/yagpdb/v2/common/run"
"github.com/botlabs-gg/yagpdb/v2/lib/confusables"
"github.com/botlabs-gg/yagpdb/v2/trivia"
"github.com/botlabs-gg/yagpdb/v2/web/discorddata"

// Core yagpdb packages
Expand Down Expand Up @@ -90,6 +91,7 @@ func main() {
internalapi.RegisterPlugin()
prom.RegisterPlugin()
featureflags.RegisterPlugin()
trivia.RegisterPlugin()

// Register confusables replacer
confusables.Init()
Expand Down
1 change: 1 addition & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
ConfLargeBotShardingEnabled = config.RegisterOption("yagpdb.large_bot_sharding", "Set to enable large bot sharding (for 200k+ guilds)", false)
ConfBucketsPerNode = config.RegisterOption("yagpdb.shard.buckets_per_node", "Number of buckets per node", 8)
ConfShardBucketSize = config.RegisterOption("yagpdb.shard.shard_bucket_size", "Shards per bucket", 2)
ConfHttpProxy = config.RegisterOption("yagpdb.http.proxy", "Proxy Url", "")

BotOwners []int64
)
Expand Down
9 changes: 9 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"database/sql"
"encoding/base64"
"fmt"
"math/rand"
"path/filepath"
Expand Down Expand Up @@ -675,3 +676,11 @@ func ParseCodeblock(input string) string {
logger.Debugf("Returning %s", parts[1])
return parts[1]
}

func Base64DecodeToString(str string) (string, error) {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return "", err
}
return string(data), nil
}
95 changes: 95 additions & 0 deletions trivia/questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package trivia

import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/url"

"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/common"
)

type TriviaQuestion struct {
Question string `json:"question"`
Answer string `json:"correct_answer"`
Category string `json:"category"`
Options []string `json:"incorrect_answers"`
}

type TriviaResponse struct {
Code int `json:"response_code"`
Questions []*TriviaQuestion `json:"results"`
}

func FetchQuestions(amount int) ([]*TriviaQuestion, error) {
client := &http.Client{}
proxy := common.ConfHttpProxy.GetString()
if len(proxy) > 0 {
proxyUrl, err := url.Parse(proxy)
if err == nil {
client.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
} else {
logger.WithError(err).Error("Invalid Proxy URL, getting questions without proxy, request maybe ratelimited")
}
}

url := fmt.Sprintf("https://opentdb.com/api.php?amount=%d&encode=base64", amount)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, commands.NewPublicError("Failed Getting Questions from opentdb")
}

var triviaResponse TriviaResponse
err = json.NewDecoder(resp.Body).Decode(&triviaResponse)
if err != nil {
return nil, err
}

if triviaResponse.Code != 0 {
return nil, commands.NewPublicError("Error from Trivia API")
}

for _, question := range triviaResponse.Questions {
question.Decode()
question.RandomizeOptionOrder()
}

return triviaResponse.Questions, nil
}

func (q *TriviaQuestion) Decode() {
q.Question, _ = common.Base64DecodeToString(q.Question)
q.Answer, _ = common.Base64DecodeToString(q.Answer)
q.Category, _ = common.Base64DecodeToString(q.Category)
for index, option := range q.Options {
q.Options[index], _ = common.Base64DecodeToString(option)
}
}

// RandomizeOptionOrder randomizes the option order and returns the result
// this also adds the answer to the list of options
func (q *TriviaQuestion) RandomizeOptionOrder() {
cop := make([]string, len(q.Options)+1)
copy(cop, q.Options)
cop[len(cop)-1] = q.Answer

rand.Shuffle(len(cop), func(i, j int) {
cop[i], cop[j] = cop[j], cop[i]
})

q.Options = cop
}
19 changes: 19 additions & 0 deletions trivia/trivia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package trivia

import "github.com/botlabs-gg/yagpdb/v2/common"

type Plugin struct{}

func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Trivia",
SysName: "trivia",
Category: common.PluginCategoryMisc,
}
}

var logger = common.GetPluginLogger(&Plugin{})

func RegisterPlugin() {
common.RegisterPlugin(&Plugin{})
}
Loading

0 comments on commit 961776e

Please sign in to comment.