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

Added Trivia #1600

Merged
merged 2 commits into from
Jan 4, 2024
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: 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