Skip to content
Open
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
82 changes: 76 additions & 6 deletions src/statusbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import discord
import discord.client
import mcstatus
import outcome
import trio

# from discord.ext import tasks, commands
from aiohttp.client_exceptions import ClientConnectorError
Expand Down Expand Up @@ -978,11 +980,16 @@
self,
prefix: str,
loop: asyncio.AbstractEventLoop,
main_nursery: trio.Nursery,
trio_finish: trio.Event,
*args: Any,
intents: discord.Intents,
**kwargs: Any,
) -> None:
"""Initialize StatusBot."""
self.nursery = main_nursery
self.trio_finish_event = trio_finish

discord.client._loop = loop
discord.Client.__init__(
self,
Expand Down Expand Up @@ -2411,6 +2418,7 @@
# Default, not affected by intents
async def close(self) -> None:
"""Tell guilds bot shutting down."""
self.trio_finish_event.set()
self.stopped.set()
print("\nShutting down gears.")
await gears.BaseBot.close(self)
Expand Down Expand Up @@ -2438,14 +2446,21 @@
await discord.Client.close(self)


def setup_bot(loop: asyncio.AbstractEventLoop) -> tuple[

Check failure on line 2449 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2449:24 - 2449:24): Missing type parameters for generic type "Outcome" [type-arg]

Check failure on line 2449 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2449:24 - 2449:24): Missing type parameters for generic type "Outcome" [type-arg]
StatusBot,
asyncio.Task[None],
tuple[
StatusBot,
asyncio.Task[None],
],
tuple[
trio.Event,
asyncio.Future[outcome.Outcome],
asyncio.Future[trio.Nursery],
],
]:
"""Return StatusBot run parts."""
if TOKEN is None:
raise RuntimeError(
"""No token set!
"""\nNo token set!
Either add ".env" file in bots folder with DISCORD_TOKEN=<token here> line,
or set DISCORD_TOKEN environment variable.""",
)
Expand All @@ -2461,18 +2476,64 @@
)
# 4867

trio_finish = trio.Event()

bot_run_task: asyncio.Task[None] | None = None

trio_done_future = loop.create_future()

def trio_done_callback(trio_outcome: outcome.Outcome) -> None:

Check failure on line 2485 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2485:42 - 2485:42): Missing type parameters for generic type "Outcome" [type-arg]

Check failure on line 2485 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2485:42 - 2485:42): Missing type parameters for generic type "Outcome" [type-arg]
trio_done_future.set_result(trio_outcome)
print("Trio complete")
if (
isinstance(trio_outcome, outcome.Error)
and bot_run_task is not None
):
print("[Trio] Canceling Bot Run")
# bot_run_task.set_exception(trio_outcome.error)
bot_run_task.cancel(
msg="".join(traceback.format_exception(trio_outcome.error)),
)
else:
trio_outcome.unwrap()

main_nursery: trio.Nursery | None = None

trio_nursery_future = loop.create_future()

@trio.lowlevel.disable_ki_protection
async def trio_async_root() -> None:
print("Trio start ticking.")
nonlocal main_nursery
async with trio.open_nursery() as nursery:
main_nursery = nursery
trio_nursery_future.set_result(nursery)
await trio_finish.wait()

trio.lowlevel.start_guest_run(
trio_async_root,
run_sync_soon_threadsafe=loop.call_soon_threadsafe,
run_sync_soon_not_threadsafe=loop.call_soon,
strict_exception_groups=True,
done_callback=trio_done_callback,
)

main_nursery = loop.run_until_complete(trio_nursery_future)

assert main_nursery is not None

bot = StatusBot(
BOT_PREFIX,
loop=loop,
intents=intents,
main_nursery=main_nursery,
trio_finish=trio_finish,
)

bot_run_task = loop.create_task(bot.start(TOKEN))
assert bot_run_task is not None

return bot, bot_run_task
return (bot, bot_run_task), (trio_finish, trio_done_future, main_nursery)

Check failure on line 2536 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2536:12 - 2536:77): Incompatible return value type (got "tuple[tuple[StatusBot, Task[None]], tuple[Event, Future[Any], Nursery | Any]]", expected "tuple[tuple[StatusBot, Task[None]], tuple[Event, Future[Outcome[Any]], Future[Nursery]]]") [return-value]

Check failure on line 2536 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2536:12 - 2536:77): Incompatible return value type (got "tuple[tuple[StatusBot, Task[None]], tuple[Event, Future[Any], Nursery | Any]]", expected "tuple[tuple[StatusBot, Task[None]], tuple[Event, Future[Outcome[Any]], Future[Nursery]]]") [return-value]


def run() -> None:
Expand All @@ -2481,14 +2542,23 @@

loop = asyncio.new_event_loop()

bot, bot_run_task = setup_bot(loop)
(bot, bot_run_task), (trio_finish, trio_done_future, main_nursery) = (
setup_bot(loop)
)

try:
loop.run_until_complete(bot_run_task)
except KeyboardInterrupt:
print("Received KeyboardInterrupt\nShutting down bot...")
loop.run_until_complete(bot.close())
trio_finish.set()
finally:
print("\nShutting down bot...")
trio_finish.set()
loop.run_until_complete(bot.close())
# Cancel trio nursery
main_nursery.cancel_scope.cancel()

Check notice on line 2559 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2559:9 - 2559:33): Maybe you forgot to use "await"?

Check failure on line 2559 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2559:9 - 2559:33): "Future[Nursery]" has no attribute "cancel_scope" [attr-defined]

Check notice on line 2559 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2559:9 - 2559:33): Maybe you forgot to use "await"?

Check failure on line 2559 in src/statusbot/bot.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.12, check formatting)

Mypy-Linux+Mac+Windows

src/statusbot/bot.py:(2559:9 - 2559:33): "Future[Nursery]" has no attribute "cancel_scope" [attr-defined]
# Ensure closed
loop.run_until_complete(trio_done_future)
# cancel all lingering tasks
loop.close()
print("\nBot has been deactivated.")
Expand Down
Loading