Skip to content

Commit

Permalink
[Discord] Improve restart process so as to edit restart message
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Nov 23, 2020
1 parent 512416f commit c6738b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 17 additions & 10 deletions Discord/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ async def initialize_database(self):
"""
CREATE TABLE IF NOT EXISTS meta.restart_channels (
channel_id BIGINT PRIMARY KEY,
player_text_channel_id BIGINT
player_text_channel_id BIGINT,
restart_message_id BIGINT
)
"""
)
Expand Down Expand Up @@ -501,14 +502,20 @@ async def initialize_constant_objects(self):
async def startup_tasks(self):
await self.wait_until_ready()
print(f"Started up Discord {self.user} ({self.user.id})")
if restart_channel_id := await self.db.fetchval(
if (record := await self.db.fetchrow(
"""
DELETE FROM meta.restart_channels
WHERE player_text_channel_id IS NULL
RETURNING channel_id
"""
):
await self.send_embed(self.get_channel(restart_channel_id), ":thumbsup::skin-tone-2: Restarted")
RETURNING *
"""
)) and (restart_channel := self.get_channel(record["channel_id"])) and (restart_message_id := record["restart_message_id"]):
try:
restart_message = await restart_channel.fetch_message(restart_message_id)
embed = restart_message.embeds[0]
embed.description += "\n:thumbsup::skin-tone-2: Restarted"
await restart_message.edit(embed = embed)
except discord.NotFound:
await self.send_embed(restart_channel, ":thumbsup::skin-tone-2: Restarted")
if audio_cog := self.get_cog("Audio"):
for record in await self.db.fetch("DELETE FROM meta.restart_channels RETURNING *"):
if text_channel := self.get_channel(record["player_text_channel_id"]):
Expand Down Expand Up @@ -812,7 +819,7 @@ async def update_all_listing_stats(self):
for site in self.listing_sites:
await self.update_listing_stats(site)

async def restart_tasks(self, channel_id):
async def restart_tasks(self, channel_id, message_id):
# Increment restarts counter
await self.db.execute(
"""
Expand All @@ -825,11 +832,11 @@ async def restart_tasks(self, channel_id):
# Save restart text channel + voice channels
await self.db.execute(
"""
INSERT INTO meta.restart_channels (channel_id)
VALUES ($1)
INSERT INTO meta.restart_channels (channel_id, restart_message_id)
VALUES ($1, $2)
ON CONFLICT (channel_id) DO NOTHING
""",
channel_id
channel_id, message_id
)
if audio_cog := self.get_cog("Audio"):
for voice_client in self.voice_clients:
Expand Down
4 changes: 2 additions & 2 deletions Discord/cogs/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,9 @@ async def update_listing_stats(self, ctx, site = None):
@commands.is_owner()
async def restart(self, ctx):
'''Restart me'''
await ctx.embed_send(":ok_hand::skin-tone-2: Restarting...")
restart_message = await ctx.embed_send(":ok_hand::skin-tone-2: Restarting...")
print("Shutting down Discord Harmonbot...")
await ctx.bot.restart_tasks(ctx.channel.id)
await ctx.bot.restart_tasks(ctx.channel.id, restart_message.id)
await ctx.bot.logout()

@commands.command(aliases = ["crash", "panic"])
Expand Down

0 comments on commit c6738b7

Please sign in to comment.