Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bugfixes #22

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ async def run(self):
for t in channels:
channels_list.append(await self._bot.fetch_channel(t[0]))
tasks = self._startup_tasks(channels_list)
utility.log_event(f'Found {len(tasks)} coroutines to start')
await asyncio.gather(*tasks)
utility.log_event('Coroutines ended')

@staticmethod
def _startup_tasks(channels):
Expand All @@ -37,5 +39,4 @@ def _startup_tasks(channels):
}
main_loop = MainLoop(params)
tasks.append(main_loop.run())
utility.log_event(f'Found {len(tasks)} coroutines to start')
return tasks
12 changes: 9 additions & 3 deletions app/bot/main_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ async def run(self):
if not self._restart:
sent = await self._send_images(database.get_posting_amount(self._channel_id))
if not sent:
utility.log_event(f'Out of images for {self._guild_id}')
await self._ctx.send(text.NO_MORE_TO_SEE)
database.delete_channel(self._channel_id)
break
else:
database.set_last_post_date(self._channel_id, time.time())
self._restart = False
await self._wait()
utility.log_event(f'Stopped posting for server {self._guild_id} channel {self._channel_id}')
utility.log_event(f'Stopped posting for guild {self._guild_id} channel {self._channel_id}')

async def _send_images(self, post_amount):
sender = image_sender.ImageSender(self._ctx, self._guild_id)
Expand All @@ -39,6 +38,13 @@ async def _send_images(self, post_amount):
async def _wait(self):
last_post_date = datetime.fromtimestamp(float(database.get_last_post_date(self._channel_id)))
start_waiting = (datetime.now() - last_post_date).total_seconds()
while start_waiting < (constants.TRIGGER_DURATION / int(database.get_posting_frequency(self._channel_id))):
while start_waiting < (self._trigger_time()):
await asyncio.sleep(constants.POLL_INTERVAL)
start_waiting += constants.POLL_INTERVAL

def _trigger_time(self):
try:
posting_freq = int(database.get_posting_frequency(self._channel_id))
except TypeError:
return 1
return constants.TRIGGER_DURATION / posting_freq
2 changes: 1 addition & 1 deletion app/cogs/get_one_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def get_one_image(self, ctx):
await ctx.respond(text.START_POSTING)
sent = await image_sender.send_image()
if not sent:
utility.log_event(f'Out of images for {guild_id}')
utility.log_event(f'Unable to send images for {guild_id}')
await ctx.send(text.NO_MORE_TO_SEE)


Expand Down
4 changes: 2 additions & 2 deletions app/commands/start_posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ async def run(self):
await asyncio.create_task(loop.run())

def _stop_posting(self):
utility.log_event(f'Stop posting called for server {self._guild_id} channel {self._channel_id}')
utility.log_event(f'Stop posting called for guild {self._guild_id} channel {self._channel_id}')
database.delete_channel(self._channel_id)

def _start_posting(self):
database.start_posting_entry(self._channel_id, self._guild_id)
utility.log_event(f'Started posting for server {self._guild_id} channel {self._channel_id}')
utility.log_event(f'Started posting for guild {self._guild_id} channel {self._channel_id}')
return MainLoop({
'ctx': self._ctx.channel,
'guild_id': self._guild_id,
Expand Down
2 changes: 1 addition & 1 deletion app/utilities/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_file(file_list):
try:
filename = file_list.pop(random.randrange(len(file_list)))
except ValueError:
log_event('All files are too big to send')
log_event('All files remaining are too big to send')
filename = None
break
return filename
Expand Down
5 changes: 4 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# DIP-bot
# Author: Liam Goring

import asyncio
import os
from pathlib import Path

Expand Down Expand Up @@ -31,9 +32,11 @@ async def on_ready():

@tasks.loop(count=1)
async def primary_application_loop():
await bot.wait_until_ready()
while not bot.is_ready():
await asyncio.sleep(0.1)
app = App(bot)
await app.run()
log_event('----Primary Application Loop Stopped----')


primary_application_loop.start()
Expand Down