Skip to content

Commit

Permalink
Update styles
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Nov 16, 2023
1 parent 085ca11 commit 324f961
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
27 changes: 25 additions & 2 deletions discordgsm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ async def select_callback(interaction: Interaction):
await interaction.response.defer(ephemeral=True)
server.style_id = select.values[0]
await database.update_server_style_id(server)
await refresh_channel_messages(interaction)
await resend_channel_messages(interaction)

select.callback = select_callback
view = View()
Expand Down Expand Up @@ -822,6 +822,29 @@ async def fetch_message(server: Server):
return None


async def embeds_chunks(servers: list[Server], n=10):
buffer = []

for server in servers:
style = Styles.get(server)

if style.standalone:
if buffer:
yield buffer
buffer = []

yield [server]
else:
buffer.append(server)

if len(buffer) == n:
yield buffer
buffer = []

if buffer:
yield buffer


async def resend_channel_messages(interaction: Optional[Interaction], channel_id: Optional[int] = None):
"""Resend channel messages"""
channel = client.get_channel(channel_id if channel_id else interaction.channel.id)
Expand All @@ -848,7 +871,7 @@ async def resend_channel_messages(interaction: Optional[Interaction], channel_id

return False

async for chunks in to_chunks(servers, 10):
async for chunks in embeds_chunks(servers):
try:
message = await channel.send(embeds=[Styles.get(server).embed() for server in chunks])
except discord.Forbidden as e:
Expand Down
4 changes: 4 additions & 0 deletions discordgsm/styles/extra_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
class ExtraLarge(Large):
"""Extra Large style"""

@property
def standalone(self) -> str:
return True

@property
def display_name(self) -> str:
return t('style.extra_large.display_name', self.locale)
Expand Down
4 changes: 4 additions & 0 deletions discordgsm/styles/large.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
class Large(Medium):
"""Large style"""

@property
def standalone(self) -> str:
return True

@property
def display_name(self) -> str:
return t('style.large.display_name', self.locale)
Expand Down
5 changes: 5 additions & 0 deletions discordgsm/styles/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def id(self) -> str:
def locale(self) -> str:
return str(self.server.style_data.get('locale', 'en-US'))

@property
def standalone(self) -> str:
"""Whether the embed should be within a single discord message"""
return False

@property
@abstractmethod
def display_name(self) -> str:
Expand Down

0 comments on commit 324f961

Please sign in to comment.