Skip to content

Commit

Permalink
Generalize handler for non-enum readings
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenzie committed May 15, 2023
1 parent 228e55b commit a00b623
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions common/tarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def makeImgList (cards: List[Card], deck: Decks):



def cardimg(cardsO: List[Card], deck: Decks, command: ReadingType) -> Image:
def cardimg(cardsO: List[Card], deck: Decks, imgfunc) -> Image:
"""Returns an Image of the cards in cards0 in a spread specified by command.
Args:
Expand All @@ -379,5 +379,5 @@ def cardimg(cardsO: List[Card], deck: Decks, command: ReadingType) -> Image:
cards = makeImgList(cardsO, deck)
cardwidth = max(map(lambda x: x.width, cards))
cardheight = max(map(lambda x: x.height, cards))
img = command.imgfunc(cards, cardwidth, cardheight)
img = imgfunc(cards, cardwidth, cardheight)
return img
16 changes: 11 additions & 5 deletions discordbot/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,32 @@
"invert": True,
}

async def handle(interaction: discord.Interaction, reading_type: ReadingType):
async def handle_generic(interaction: discord.Interaction, numcards, imgfunc, should_inline, reading_type_name):
opts = READING_DEFAULTS
gid = str(interaction.guild_id)
uid = str(interaction.user.id)
with shelve.open(backup, 'r') as store:
if gid in store and uid in store[gid]["users"]:
opts = store[gid]["users"][uid]
cards = tarot.draw(reading_type.num, opts["invert"], opts["majorminor"])
cards = tarot.draw(numcards, opts["invert"], opts["majorminor"])
response = tarot.cardtxt(cards)
who = "<@{}>, here is your reading\n".format(interaction.user.id)
file = None
embed = None
message = who
if opts["image"]:
im = tarot.cardimg(cards, opts["deck"], reading_type)
im = tarot.cardimg(cards, opts["deck"], imgfunc)
with BytesIO() as buf:
im.save(buf, "PNG")
buf.seek(0)
file = discord.File(fp=buf,filename="image.png")

if opts["embed"]:
embed = discord.Embed(title="{} reading for {}".format(reading_type.fullname, interaction.user.display_name),
embed = discord.Embed(title="{} reading for {}".format(reading_type_name, interaction.user.display_name),
type="rich",
color=color)

if opts["text"]:
should_inline = reading_type in [ReadingType.ONE, ReadingType.THREE]
for i, (n,v) in enumerate(response):
embed.add_field(name='{}) {}'.format(i+1,n), value=v,
inline=should_inline)
Expand All @@ -56,3 +55,10 @@ async def handle(interaction: discord.Interaction, reading_type: ReadingType):
message = (message + "\n**" + str(i+1) + ") " +
n + "**\n" + v)
await interaction.response.send_message(content=message, file=file, embed=embed, ephemeral=opts["private"])

async def handle(interaction: discord.Interaction, reading_type: ReadingType):
await handle_generic(interaction,
reading_type.num,
reading_type.imgfunc,
reading_type in [ReadingType.ONE, ReadingType.THREE],
reading_type.fullname)

0 comments on commit a00b623

Please sign in to comment.