trivia.py 文件源码

python
阅读 36 收藏 0 点赞 0 评论 0

项目:kitsuchan-2 作者: n303p4 项目源码 文件源码
def trivia(self, ctx):
        """Ask a random trivia question."""
        async with ctx.bot.session.get(URL_TRIVIA_API) as response:
            if response.status == 200:
                data = await response.json()

                trivia = data["results"][0]

                correct_answer = html.unescape(trivia["correct_answer"])
                incorrect_answers = []
                for answer in trivia["incorrect_answers"]:
                    incorrect_answers.append(html.unescape(answer))

                choices = [correct_answer] + incorrect_answers

                systemrandom.shuffle(choices)

                embed = discord.Embed()
                embed.title = html.unescape(trivia["category"])
                embed.description = html.unescape(trivia["question"])

                difficulty = html.unescape(trivia["difficulty"]).capitalize()
                footer_text = f"Powered by Open Trivia DB | Difficulty: {difficulty}"

                embed.set_footer(text=footer_text)

                paginator = commands.Paginator(prefix="```markdown")

                for index in range(len(choices)):
                    paginator.add_line(f"{index+1}. {choices[index]}")

                embed.add_field(name="Options", value=paginator.pages[0])

                await ctx.send(ctx.author.mention, embed=embed)
                choice = await input_number(ctx, "Answer by number in 15 seconds.",
                                            timeout=15, min_value=1,
                                            max_value=len(choices))

                if choices[choice-1] == correct_answer:
                    await ctx.send("Correct! :3")

                else:
                    await ctx.send(f"Nope, the correct answer is {correct_answer}. :<")

            else:
                await ctx.send("Could not fetch trivia. :<")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号