Skip to content

Commit

Permalink
- Improve logic for refresh time (5 second minimum, regardless of use…
Browse files Browse the repository at this point in the history
…r value)

- Account for empty messages when starting the bot
  • Loading branch information
nwithan8 committed Sep 15, 2022
1 parent 96b1fa6 commit b94757b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You will need to set the following environment variables:
| TC_TAUTULLI_URL (required) | IP of your Tautulli server | http://192.168.1.x:8181 |
| TC_TAUTULLI_KEY (required) | API key for Tautulli server | abcd1234efgh5678ijkl9012mnop3456qrst |
| TC_PLEX_PASS | Enable PlexPass Features | "False" |
| TC_REFRESH_SECONDS | Seconds between updates | 15 |
| TC_REFRESH_SECONDS | Seconds between updates (5-second minimum built-in) | 15 |
| TC_TERMINATE_MESSAGE | Message sent to users when a stream is killed | "Your stream has ended." |
| TC_USE_24_HOUR_TIME | Whether to display times in 24-hour time | "False" |
| TC_VC_STREAM_COUNT | Whether to display current stream count in a voice channel | "False" |
Expand Down
2 changes: 1 addition & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tautulli:
APIKey: ""
Customization:
TerminateMessage: "Your stream has been terminated. Please contact the admin in the Discord."
# how often (seconds) the bot pulls new data. I'd recommend not making the bot ping Tautulli more often than every 5 seconds.
# how often (seconds) the bot pulls new data. 5-second minimum built-in, it's for your own good
RefreshSeconds: 15
# can only kill streams if you have a Plex Pass, so this controls whether you're given the option
PlexPass: true
Expand Down
40 changes: 28 additions & 12 deletions modules/discord_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,28 @@ async def send_message(content: TautulliDataResponse, embed: bool = False, messa
if not channel and not message:
raise ValueError("Must specify either a channel or a message")
if message: # if message exists, use it to edit the message
if embed:
await message.edit(embed=content.embed)
else:
await message.edit(content=content.message)
if embed: # let's send an embed
if not content.embed: # oops, no embed to send
await message.edit(content="Placeholder", embed=None) # erase any existing content and embeds
else:
await message.edit(content=None, embed=content.embed) # erase any existing content and embeds
else: # let's send a normal message
if not content.message: # oops, no message to send
await message.edit(content="Placeholder", embed=None) # erase any existing content and embeds
else:
await message.edit(content=content.message, embed=None) # erase any existing content and embeds
return message
else: # otherwise, send a new message in the channel
if embed:
return await channel.send(embed=content.embed)
else:
return await channel.send(content=content.message)
if embed: # let's send an embed
if not content.embed: # oops, no embed to send
return await channel.send(content="Placeholder")
else:
return await channel.send(content=None, embed=content.embed)
else: # let's send a normal message
if not content.message: # oops, no message to send
return await channel.send(content="Placeholder")
else:
return await channel.send(content=content.message)


class DiscordConnector:
Expand All @@ -115,7 +127,7 @@ def __init__(self,
self.token = token
self.guild_id = guild_id
self.owner_id = owner_id
self.refresh_time = refresh_time
self._refresh_time = refresh_time
self.tautulli_channel_name = tautulli_channel_name
self.tautulli_channel = None
self.tautulli = tautulli_connector
Expand All @@ -124,6 +136,10 @@ def __init__(self,
self.client = discord.Client(intents=discord.Intents.default())
self.on_ready = self.client.event(self.on_ready)

@property
def refresh_time(self) -> int:
return max([5, self._refresh_time]) # minimum 5-second sleep time hard-coded, trust me, don't DDoS your server

async def on_ready(self):
info('Connected to Discord.')
self.update_libraries.start()
Expand Down Expand Up @@ -206,7 +222,7 @@ async def edit_message(self, previous_message):
stopped_message = self.tautulli.stop_stream(stream_number=loc)
info(stopped_message)
end_notification = await self.tautulli_channel.send(content=stopped_message)
await asyncio.sleep(min([5, self.refresh_time]))
await asyncio.sleep(self.refresh_time)
await end_notification.delete()
await new_message.clear_reaction(str(reaction.emoji))
return new_message
Expand All @@ -226,7 +242,7 @@ def check(reaction, user):
stopped_message = self.tautulli.stop_stream(stream_number=loc)
info(stopped_message)
end_notification = await self.tautulli_channel.send(content=stopped_message)
await asyncio.sleep(min([5, self.refresh_time]))
await asyncio.sleep(self.refresh_time)
await end_notification.delete()
await new_message.clear_reaction(str(reaction.emoji))
else:
Expand Down Expand Up @@ -319,7 +335,7 @@ async def get_old_message_in_tautulli_channel(self):
if last_bot_message_id == "":
info("Couldn't find old message, sending initial message...")
await send_starter_message(tautulli_connector=self.tautulli, discord_channel=self.tautulli_channel)
return await self.tautulli_channel.fetch_message(last_bot_message_id)
return await self.tautulli_channel.fetch_message(id=last_bot_message_id)

async def update_voice_channels(self, activity):
if activity:
Expand Down

0 comments on commit b94757b

Please sign in to comment.