-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivia.go
118 lines (104 loc) · 3.02 KB
/
trivia.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"fmt"
"github.com/Necroforger/dgrouter/exrouter"
"github.com/bwmarrin/discordgo"
"github.com/darenliang/MikuBotGo/config"
"github.com/darenliang/MikuBotGo/framework"
"html"
"math/rand"
"sync"
"time"
)
type TriviaResponse struct {
ResponseCode int `json:"response_code"`
Results []struct {
Category string `json:"category"`
Type string `json:"type"`
Difficulty string `json:"difficulty"`
Question string `json:"question"`
CorrectAnswer string `json:"correct_answer"`
IncorrectAnswers []string `json:"incorrect_answers"`
} `json:"results"`
}
func init() {
rand.Seed(time.Now().UnixNano())
}
// Trivia command
func Trivia(ctx *exrouter.Context) {
var (
lock sync.RWMutex
embedMsg *discordgo.Message
callback = make(chan struct{})
idx int
)
triviaResponse := &TriviaResponse{}
err := framework.UrlToStruct("https://opentdb.com/api.php?amount=1&category=31&type=multiple", triviaResponse)
if err != nil {
ctx.Reply(":cry: An error has occurred")
return
}
question := triviaResponse.Results[0]
answers := make([]string, 0)
answers = append(answers, question.CorrectAnswer)
answers = append(answers, question.IncorrectAnswers...)
rand.Shuffle(len(answers), func(i, j int) { answers[i], answers[j] = answers[j], answers[i] })
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: config.EmbedColor,
Description: fmt.Sprintf(
"%s\n\n"+
":one: %s\n"+
":two: %s\n"+
":three: %s\n"+
":four: %s",
html.UnescapeString(question.Question),
html.UnescapeString(answers[0]),
html.UnescapeString(answers[1]),
html.UnescapeString(answers[2]),
html.UnescapeString(answers[3]),
),
Title: fmt.Sprintf("Anime trivia"),
}
embedMsg, _ = ctx.Ses.ChannelMessageSendEmbed(ctx.Msg.ChannelID, embed)
for i := 0; i < 4; i++ {
ctx.Ses.MessageReactionAdd(ctx.Msg.ChannelID, embedMsg.ID, config.SelectionEmojis[i])
}
defer ctx.Ses.AddHandler(func(_ *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
lock.RLock()
defer lock.RUnlock()
idx = framework.Index(config.SelectionEmojis, reaction.Emoji.Name)
if reaction.MessageID == embedMsg.ID && reaction.UserID != ctx.Ses.State.User.ID && idx != -1 {
callback <- struct{}{}
}
})()
embed = &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Fields: []*discordgo.MessageEmbedField{
{
Name: "Answer",
Value: html.UnescapeString(question.CorrectAnswer),
Inline: true,
},
},
}
select {
case <-callback:
if answers[idx] == question.CorrectAnswer {
embed.Title = "Correct"
embed.Color = 0x4caf50
ctx.Ses.ChannelMessageSendEmbed(ctx.Msg.ChannelID, embed)
return
} else {
embed.Title = "Incorrect"
embed.Color = 0xf44336
ctx.Ses.ChannelMessageSendEmbed(ctx.Msg.ChannelID, embed)
return
}
case <-time.After(config.Timeout * time.Second):
embed.Title = "Timed out"
embed.Color = 0xfdd835
ctx.Ses.ChannelMessageSendEmbed(ctx.Msg.ChannelID, embed)
return
}
}