Merge branch 'test'

This commit is contained in:
Riley Housden 2022-08-22 16:34:22 -04:00
commit b4b09c92d3
3 changed files with 57 additions and 5 deletions

View file

@ -10,7 +10,7 @@ from typing import Optional
from lib.config import load_config from lib.config import load_config
from lib.gcal import GoogleCalendarAPI from lib.gcal import GoogleCalendarAPI
from lib.room import Room from lib.room import Room
from lib.ocr import get_room_data, NoMatchException from lib.ocr import get_room_data, NoMatchException, get_image_string
plugin = lightbulb.Plugin("LibCal") plugin = lightbulb.Plugin("LibCal")
config = load_config() config = load_config()
@ -82,8 +82,15 @@ async def book_error_handler(event: lightbulb.CommandErrorEvent):
print("ERROR TYPE:", type(event.exception)) print("ERROR TYPE:", type(event.exception))
if isinstance(event.exception.__cause__, NoMatchException): if isinstance(event.exception.__cause__, NoMatchException):
embed = hikari.Embed(title="Huh, I can't read that", embed = hikari.Embed(title="Huh, I can't read that",
description="Could you try another picture?") description="Could you try another picture?\nI'll send a notice to the bot's maintainer. :)")
await event.context.respond(embed=embed) await event.context.respond(embed=embed)
embed.title = "Image match failed!"
embed.description = f"I failed to find any bookings in the attached image."
embed.add_field("Image text", get_image_string(await event.context.options.img.read()))
embed.set_image(event.context.options.img)
owners = await event.context.bot.fetch_owner_ids()
for user_id in owners:
await (await event.context.bot.rest.fetch_user(user_id)).send(embed)
else: else:
embed = hikari.Embed( embed = hikari.Embed(
title="Booking Error", description="Whelp, better luck next time I guess... the images used are attached.") title="Booking Error", description="Whelp, better luck next time I guess... the images used are attached.")

39
ext/profile.py Normal file
View file

@ -0,0 +1,39 @@
import hikari
import lightbulb
plugin = lightbulb.Plugin("ProfilePlugin")
@plugin.command
@lightbulb.option("img", "image to set as new avatar", type=hikari.Attachment, required=True)
@lightbulb.add_checks(lightbulb.owner_only)
@lightbulb.command("avatar", "set the bot avatar.", ephemeral=True)
@lightbulb.implements(lightbulb.SlashCommand)
async def set_avatar(ctx: lightbulb.Context) -> None:
await ctx.bot.rest.edit_my_user(avatar=ctx.options.img)
embed = hikari.Embed(title="New avatar set!")
embed.set_image(ctx.options.img)
await ctx.respond(embed)
@plugin.command
@lightbulb.option("text", "text to set as custom status", type=str, required=True)
@lightbulb.option("type", "type of status, 0 is Playing, 1 is Watching, 2 is Streaming, 3 is Listening, 4 is Competing", type=int, default=0)
@lightbulb.add_checks(lightbulb.owner_only)
@lightbulb.command("status", "set the bot status.", ephemeral=True)
@lightbulb.implements(lightbulb.SlashCommand)
async def set_status(ctx: lightbulb.Context) -> None:
choices = [hikari.ActivityType.PLAYING, hikari.ActivityType.WATCHING,
hikari.ActivityType.STREAMING, hikari.ActivityType.LISTENING, hikari.ActivityType.COMPETING]
await ctx.bot.update_presence(activity=hikari.Activity(name=ctx.options.text, type=choices[ctx.options.type]))
embed = hikari.Embed(title="New status set!",
description=f"New Status: {ctx.options.text}")
await ctx.respond(embed)
def load(bot: lightbulb.BotApp):
bot.add_plugin(plugin)
def unload(bot: lightbulb.BotApp):
bot.remove_plugin(plugin)

View file

@ -14,10 +14,10 @@ __all__ = ["get_room_data"]
config = load_config() config = load_config()
RE_STRING = re.compile( # https://regex101.com/r/ELsqrO/1 RE_STRING = re.compile( # https://regex101.com/r/OkWfkC/1
r"(L-[0-9]{4}): " # room number (group 1) r"(L-[0-9]{4}): " # room number (group 1)
# time-slot (group 2,3,4 - group 5,6,7) # time-slot (group 2,3,4 - group 5,6,7)
r"([1-12]{1,2}):([0,3]{2})(am|pm) - ([1-12]{1,2}):([0,3]{2})(am|pm), " r"([1-9]{1,2}):([0,3]{2})(am|pm) - ([1-9]{1,2}):([0,3]{2})(am|pm), "
# weekday (group 8) # weekday (group 8)
r"(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), " r"(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), "
# month (group 9) # month (group 9)
@ -67,7 +67,7 @@ def get_room_data(img: bytes) -> list[Room]:
rooms: list[Room] = [] rooms: list[Room] = []
start_time: datetime | None = None start_time: datetime | None = None
end_time: datetime | None = None end_time: datetime | None = None
img_string = image_to_string(Image.open(BytesIO(img))) img_string = get_image_string(img)
img_string = correct_newlines(img_string) img_string = correct_newlines(img_string)
img_string = correct_commas(img_string) img_string = correct_commas(img_string)
matches = re.finditer(RE_STRING, img_string) matches = re.finditer(RE_STRING, img_string)
@ -85,3 +85,9 @@ def get_room_data(img: bytes) -> list[Room]:
if len(rooms) == 0: if len(rooms) == 0:
raise NoMatchException raise NoMatchException
return rooms return rooms
def get_image_string(img: bytes) -> str:
img_string = image_to_string(Image.open(BytesIO(img)))
img_string = correct_newlines(img_string)
img_string = correct_commas(img_string)
return img_string