use numbered tabs for low page count paged messages

pull/1/head
qwewqa 4 years ago
parent 9fb92c449c
commit 03596b635e
  1. 21
      miyu_bot/commands/common/reaction_message.py

@ -1,14 +1,17 @@
import asyncio import asyncio
from typing import List, Callable, Awaitable, Union, Optional from typing import List, Callable, Awaitable, Union
import discord import discord
from discord import Message, Embed, Emoji, PartialEmoji from discord import Message, Embed, Emoji
from discord.ext.commands import Context from discord.ext.commands import Context
AnyEmoji = Union[str, Emoji] AnyEmoji = Union[str, Emoji]
async def run_tabbed_message(ctx: Context, message: Message, emojis: List[Emoji], embeds: List[Embed], timeout=300): async def run_tabbed_message(ctx: Context, message: Message, emojis: List[AnyEmoji], embeds: List[Embed], timeout=300):
if len(emojis) != len(embeds):
raise ValueError('Emojis and embeds must have the same number of elements.')
async def callback(emoji, _ctx, _message): async def callback(emoji, _ctx, _message):
await message.edit(embed=embeds[emojis.index(emoji)]) await message.edit(embed=embeds[emojis.index(emoji)])
@ -16,7 +19,10 @@ async def run_tabbed_message(ctx: Context, message: Message, emojis: List[Emoji]
async def run_paged_message(ctx: Context, title: str, content: List[str], page_size: int = 15, numbered: bool = True, async def run_paged_message(ctx: Context, title: str, content: List[str], page_size: int = 15, numbered: bool = True,
timeout=300, double_arrow_threshold=4): timeout=300, max_tabbed_pages=4):
if max_tabbed_pages > 9:
raise ValueError('max_tabbed_pages must be 9 or less.')
if not content: if not content:
embed = discord.Embed(title=title).set_footer(text='Page 0/0') embed = discord.Embed(title=title).set_footer(text='Page 0/0')
await ctx.send(embed=embed) await ctx.send(embed=embed)
@ -45,15 +51,16 @@ async def run_paged_message(ctx: Context, title: str, content: List[str], page_s
if len(embeds) == 1: if len(embeds) == 1:
return return
if len(embeds) <= max_tabbed_pages:
reaction_emoji = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
await run_tabbed_message(ctx, message, reaction_emoji[:len(embeds)], embeds, timeout=timeout)
else:
double_left_arrow = '' double_left_arrow = ''
double_right_arrow = '' double_right_arrow = ''
left_arrow = '' left_arrow = ''
right_arrow = '' right_arrow = ''
if 0 < double_arrow_threshold <= len(embeds):
arrows = [double_left_arrow, left_arrow, right_arrow, double_right_arrow] arrows = [double_left_arrow, left_arrow, right_arrow, double_right_arrow]
else:
arrows = [left_arrow, right_arrow]
index = 0 index = 0

Loading…
Cancel
Save