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

[FEAT] Is it Read-Only Friday check #275

Closed
MrMrRubic opened this issue Aug 2, 2024 · 1 comment · Fixed by #277
Closed

[FEAT] Is it Read-Only Friday check #275

MrMrRubic opened this issue Aug 2, 2024 · 1 comment · Fixed by #277
Labels
new-cog New cog for bot.

Comments

@MrMrRubic
Copy link

Overview

A command which returns wether it's readonly friday or not using isitreadonlyfriday's API

Commands

[p]isitreadonlyfriday [UTC offset]

DoD

  • Being able to issue the command and get a response from the bot Whether it's read-only friday or not defaulting to UTC.
  • Add a UTC offset to get the right info for the user's local time.

Stretch

  • Get the user issuing the command's timezone from the Discord API to automatically get the correct time, though this might be impossible through Discord's API.
@MrMrRubic MrMrRubic added the new-cog New cog for bot. label Aug 2, 2024
@MrMrRubic
Copy link
Author

I basically copied xkcd.py and modified it to something that might work, though i don't have the setup to test it properly. That's also how i discovered issue #274.

import aiohttp
import discord
from redbot.core import commands


async def fetch_get(url_in: str) -> dict:
    """Make web requests"""
    async with aiohttp.request("GET", url_in) as response:
        if response.status != 200:
            return {}
        return await response.json()


class IsItReadOnlyFriday(commands.Cog):
    """IsItReadOnlyFriday Cog"""

    def __init__(self):
        pass

    @commands.command()
    async def isitreadonlyfriday(self, ctx: commands.Context, offset: int = 0):
        """Returns isitreadonlyfriday readonly of given offset, otherwise return UTC."""

        if not offset:
            # No offset specified, get UTC
            url = "https://isitreadonlyfriday.com/api/isitreadonlyfriday/"
        else:
            url = f"https://isitreadonlyfriday.com/api/isitreadonlyfriday/{offset}"

        # Get readonly data from isitreadonlyfriday api
        readonly_json = await fetch_get(url)

        # If the response isn't 200 throw an error
        if not readonly_json:
            embed = await self.make_error_embed(ctx, "404")
            await ctx.send(embed=embed)
            return

        embed = await self.make_readonly_embed(ctx, readonly_json)
        await ctx.send(embed=embed)

    async def make_readonly_embed(self, ctx: commands.Context, data: dict) -> discord.Embed:
        """Generate embed for isitreadonlyfriday readonly"""
        if not data["readonly"] == true:
            isitreadonlyfriday_embed = discord.Embed(
                title=f"Is It Read-Only Friday?",
                description=f"No! Change away!",
                colour=await ctx.embed_colour()
            )
        else
            isitreadonlyfriday_embed = discord.Embed(
                title=f"Is It Read-Only Friday?",
                description=f"Yes! Don't change anything!",
                colour=await ctx.embed_colour()
            )
        return isitreadonlyfriday_embed

    async def make_error_embed(self, ctx: commands.Context, error_type: str) -> discord.Embed:
        """Generate error message embeds"""
        error_msgs = {"404": "readonly not found"}
        return discord.Embed(
            title="Error",
            description=error_msgs[error_type],
            colour=await ctx.embed_colour(),
        )

I highly doubt this is actually usable though, which is why i'm not bold enough to issue a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-cog New cog for bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant