Skip to content
Merged
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
65 changes: 65 additions & 0 deletions tux/cogs/fun/fact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import random

from discord.ext import commands

from tux.utils.embeds import EmbedCreator


class Fact(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Optimize fact selection for better distribution

Consider shuffling the facts list in the __init__ method and selecting facts from the first 75% of the list in the fact method. This approach would provide a good balance between randomness and ensuring all facts are seen before repeats occur.

    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot
        self.facts = []
        self.load_and_shuffle_facts()

    def load_and_shuffle_facts(self) -> None:
        self.facts = [...]  # Load facts here
        random.shuffle(self.facts)
        self.fact_index = 0

self.bot = bot
self.facts = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Consider scalability for fact storage

If there are plans to significantly expand the fact list in the future, you might want to consider a more scalable solution for storing and retrieving facts, such as loading from a file or database.

        self.facts = self.load_facts_from_file('facts.json')

    def load_facts_from_file(self, filename):
        with open(filename, 'r') as file:
            return json.load(file)

"The Linux kernel has over 36 million lines of code in its Git repository.",
"The vast majority of the world's supercomputers run on Linux.",
"Linux 1.0 was launched on September 17, 1991 featuring 176,250 lines of code.",
"There's an easter egg in `apt` if you enter `apt moo`.",
"Linus Torvalds was around 22 years old when he started work on the Linux Kernel in 1991. In the same year, he also released prototypes of the kernel publicly.",
"Linux's 1.0 release was in March 1994.",
"Less than 1% of the latest kernel release includes code written by Linus Torvalds.",
"Linux is used by every major space programme in the world.",
"Approximately 13.3% of the latest Linux kernel is made up of blank lines.",
"Vim has various easter eggs. A notable one is found by typing :help 42 into the command bar.",
"Slackware is the oldest active linux distribution being released on the 17th July 1993.",
"Freax was the original planned name for Linux.",
"The first GUI that ran on Linux was X Window System. It ran on Linux Kernel version 0.95.",
"The Linux Kernel was the first OS kernel to support x86-64 in 2001.",
"Over 14,600 individual developers from over 1,300 different companies have contributed to the kernel.",
"95% of the Linux kernel is written in the C programming Language. Assembly language is the second most used language for Linux at 2.8%.",
"The first kernel version - Version 0.01 - contained about 10,000 lines of code.",
"96.3% of the top 1,000,000 web servers were reported to run on Linux.",
"In the early 2000s, Steve Jobs, who at the time was the CEO of Apple, offered a job to Linus Torvalds to work on OSX which Torvalds declined.",
"Linus Torvalds said that he would have never created Linux if FreeBSD had been available at the time.",
"Linux is used by every major space programme in the world including NASA, SpaceX, and the European Space Agency.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Remove redundant fact

This fact is a duplicate of the one on line 8. Consider removing one of these instances to avoid repetition.

Suggested change
"Linux is used by every major space programme in the world including NASA, SpaceX, and the European Space Agency.",
# Removed duplicate fact about space programs using Linux

]

@commands.hybrid_command(
name="fact",
aliases=["funfact"],
)
async def fact(self, ctx: commands.Context[commands.Bot]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Improve or remove method comment

The current comment doesn't provide much additional information. Consider either removing it or expanding it to include more meaningful details about the method's behavior or purpose.

Suggested change
async def fact(self, ctx: commands.Context[commands.Bot]) -> None:
async def fact(self, ctx: commands.Context[commands.Bot]) -> None:
"""
Fetch and send a random fun fact to the user.
This command retrieves a fact from an external API or database
and posts it in the channel where the command was invoked.
"""

"""
Get a random fun fact.

Parameters
----------
ctx : commands.Context[commands.Bot]
The context object for the command.
"""
embed = EmbedCreator.create_info_embed(
title="Fun Fact",
description=random.choice(self.facts),
ctx=ctx,
)

# set author
embed.set_author(
name="Submit more facts here!",
url="https://github.com/allthingslinux/tux/blob/main/tux/cogs/fun/fact.py",
icon_url="https://github.com/allthingslinux/tux/blob/main/assets/emojis/tux_info.png?raw=true",
)

await ctx.send(embed=embed)


async def setup(bot: commands.Bot) -> None:
await bot.add_cog(Fact(bot))