Add async start and sync run (#94)
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled
This commit is contained in:
parent
5d66eb79cc
commit
1464937f6f
@ -61,13 +61,8 @@ if not token:
|
|||||||
|
|
||||||
intents = disagreement.GatewayIntent.default() | disagreement.GatewayIntent.MESSAGE_CONTENT
|
intents = disagreement.GatewayIntent.default() | disagreement.GatewayIntent.MESSAGE_CONTENT
|
||||||
client = disagreement.Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
client = disagreement.Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
||||||
async def main() -> None:
|
client.add_cog(Basics(client))
|
||||||
client.add_cog(Basics(client))
|
client.run()
|
||||||
await client.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Global Error Handling
|
### Global Error Handling
|
||||||
|
@ -158,9 +158,9 @@ class Client:
|
|||||||
**(http_options or {}),
|
**(http_options or {}),
|
||||||
)
|
)
|
||||||
self._event_dispatcher: EventDispatcher = EventDispatcher(client_instance=self)
|
self._event_dispatcher: EventDispatcher = EventDispatcher(client_instance=self)
|
||||||
self._gateway: Optional[GatewayClient] = (
|
self._gateway: Optional[GatewayClient] = (
|
||||||
None # Initialized in run() or connect()
|
None # Initialized in start() or connect()
|
||||||
)
|
)
|
||||||
self.shard_count: Optional[int] = shard_count
|
self.shard_count: Optional[int] = shard_count
|
||||||
self.gateway_max_retries: int = gateway_max_retries
|
self.gateway_max_retries: int = gateway_max_retries
|
||||||
self.gateway_max_backoff: float = gateway_max_backoff
|
self.gateway_max_backoff: float = gateway_max_backoff
|
||||||
@ -268,8 +268,8 @@ class Client:
|
|||||||
await self._initialize_gateway()
|
await self._initialize_gateway()
|
||||||
assert self._gateway is not None # Should be initialized by now
|
assert self._gateway is not None # Should be initialized by now
|
||||||
|
|
||||||
retry_delay = 5 # seconds
|
retry_delay = 5 # seconds
|
||||||
max_retries = 5 # For initial connection attempts by Client.run, Gateway has its own internal retries for some cases.
|
max_retries = 5 # For initial connection attempts by Client.start, Gateway has its own internal retries for some cases.
|
||||||
|
|
||||||
for attempt in range(max_retries):
|
for attempt in range(max_retries):
|
||||||
try:
|
try:
|
||||||
@ -300,12 +300,11 @@ class Client:
|
|||||||
if max_retries == 0: # If max_retries was 0, means no retries attempted
|
if max_retries == 0: # If max_retries was 0, means no retries attempted
|
||||||
raise DisagreementException("Connection failed with 0 retries allowed.")
|
raise DisagreementException("Connection failed with 0 retries allowed.")
|
||||||
|
|
||||||
async def run(self) -> None:
|
async def start(self) -> None:
|
||||||
"""
|
"""
|
||||||
A blocking call that connects the client to Discord and runs until the client is closed.
|
Connect the client to Discord and run until the client is closed.
|
||||||
This method is a coroutine.
|
This method is a coroutine containing the main run loop logic.
|
||||||
It handles login, Gateway connection, and keeping the connection alive.
|
"""
|
||||||
"""
|
|
||||||
if self._closed:
|
if self._closed:
|
||||||
raise DisagreementException("Client is already closed.")
|
raise DisagreementException("Client is already closed.")
|
||||||
|
|
||||||
@ -362,15 +361,19 @@ class Client:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error checking gateway receive task: {e}")
|
print(f"Error checking gateway receive task: {e}")
|
||||||
break # Exit on other errors
|
break # Exit on other errors
|
||||||
await asyncio.sleep(1) # Main loop check interval
|
await asyncio.sleep(1) # Main loop check interval
|
||||||
except DisagreementException as e:
|
except DisagreementException as e:
|
||||||
print(f"Client run loop encountered an error: {e}")
|
print(f"Client run loop encountered an error: {e}")
|
||||||
# Error already logged by connect or other methods
|
# Error already logged by connect or other methods
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
print("Client run loop was cancelled.")
|
print("Client run loop was cancelled.")
|
||||||
finally:
|
finally:
|
||||||
if not self._closed:
|
if not self._closed:
|
||||||
await self.close()
|
await self.close()
|
||||||
|
|
||||||
|
def run(self) -> None:
|
||||||
|
"""Synchronously start the client using :func:`asyncio.run`."""
|
||||||
|
asyncio.run(self.start())
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -60,13 +60,8 @@ if not token:
|
|||||||
|
|
||||||
intents = GatewayIntent.default() | GatewayIntent.MESSAGE_CONTENT
|
intents = GatewayIntent.default() | GatewayIntent.MESSAGE_CONTENT
|
||||||
client = Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
client = Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
||||||
async def main() -> None:
|
client.add_cog(Basics(client))
|
||||||
client.add_cog(Basics(client))
|
client.run()
|
||||||
await client.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Global Error Handling
|
### Global Error Handling
|
||||||
|
@ -8,13 +8,8 @@ manually.
|
|||||||
and configures the `ShardManager` automatically.
|
and configures the `ShardManager` automatically.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import asyncio
|
|
||||||
import disagreement
|
import disagreement
|
||||||
|
|
||||||
bot = disagreement.AutoShardedClient(token="YOUR_TOKEN")
|
bot = disagreement.AutoShardedClient(token="YOUR_TOKEN")
|
||||||
|
bot.run()
|
||||||
async def main():
|
|
||||||
await bot.run()
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
@ -67,9 +67,7 @@ BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN")
|
|||||||
# --- Intents Configuration ---
|
# --- Intents Configuration ---
|
||||||
# Define the intents your bot needs. For basic message reading and responding:
|
# Define the intents your bot needs. For basic message reading and responding:
|
||||||
intents = (
|
intents = (
|
||||||
GatewayIntent.GUILDS
|
GatewayIntent.GUILDS | GatewayIntent.GUILD_MESSAGES | GatewayIntent.MESSAGE_CONTENT
|
||||||
| GatewayIntent.GUILD_MESSAGES
|
|
||||||
| GatewayIntent.MESSAGE_CONTENT
|
|
||||||
) # MESSAGE_CONTENT is privileged!
|
) # MESSAGE_CONTENT is privileged!
|
||||||
|
|
||||||
# If you don't need message content and only react to commands/mentions,
|
# If you don't need message content and only react to commands/mentions,
|
||||||
@ -210,14 +208,14 @@ async def on_guild_available(guild: Guild):
|
|||||||
|
|
||||||
|
|
||||||
# --- Main Execution ---
|
# --- Main Execution ---
|
||||||
async def main():
|
def main():
|
||||||
print("Starting Disagreement Bot...")
|
print("Starting Disagreement Bot...")
|
||||||
try:
|
try:
|
||||||
# Add the Cog to the client
|
# Add the Cog to the client
|
||||||
client.add_cog(ExampleCog(client)) # Pass client instance to Cog constructor
|
client.add_cog(ExampleCog(client)) # Pass client instance to Cog constructor
|
||||||
# client.add_cog is synchronous, but it schedules cog.cog_load() if it's async.
|
# client.add_cog is synchronous, but it schedules cog.cog_load() if it's async.
|
||||||
|
|
||||||
await client.run()
|
client.run()
|
||||||
except AuthenticationError:
|
except AuthenticationError:
|
||||||
print(
|
print(
|
||||||
"Authentication failed. Please check your bot token and ensure it's correct."
|
"Authentication failed. Please check your bot token and ensure it's correct."
|
||||||
@ -232,7 +230,7 @@ async def main():
|
|||||||
finally:
|
finally:
|
||||||
if not client.is_closed():
|
if not client.is_closed():
|
||||||
print("Ensuring client is closed...")
|
print("Ensuring client is closed...")
|
||||||
await client.close()
|
asyncio.run(client.close())
|
||||||
print("Bot has been shut down.")
|
print("Bot has been shut down.")
|
||||||
|
|
||||||
|
|
||||||
@ -244,4 +242,4 @@ if __name__ == "__main__":
|
|||||||
# if os.name == 'nt':
|
# if os.name == 'nt':
|
||||||
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||||
|
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -263,7 +263,7 @@ class ComponentCommandsCog(Cog):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
if client.user:
|
if client.user:
|
||||||
@ -283,8 +283,8 @@ async def main():
|
|||||||
)
|
)
|
||||||
|
|
||||||
client.add_cog(ComponentCommandsCog(client))
|
client.add_cog(ComponentCommandsCog(client))
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -65,11 +65,9 @@ client.app_command_handler.add_command(user_info)
|
|||||||
client.app_command_handler.add_command(quote)
|
client.app_command_handler.add_command(quote)
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
def main() -> None:
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import asyncio
|
main()
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
|
@ -27,10 +27,10 @@ intents = GatewayIntent.default() | GatewayIntent.MESSAGE_CONTENT
|
|||||||
client = Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
client = Client(token=token, command_prefix="!", intents=intents, mention_replies=True)
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
def main() -> None:
|
||||||
client.add_cog(Basics(client))
|
client.add_cog(Basics(client))
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -230,7 +230,7 @@ class TestCog(Cog):
|
|||||||
|
|
||||||
|
|
||||||
# --- Main Bot Script ---
|
# --- Main Bot Script ---
|
||||||
async def main():
|
def main():
|
||||||
bot_token = os.getenv("DISCORD_BOT_TOKEN")
|
bot_token = os.getenv("DISCORD_BOT_TOKEN")
|
||||||
application_id = os.getenv("DISCORD_APPLICATION_ID")
|
application_id = os.getenv("DISCORD_APPLICATION_ID")
|
||||||
|
|
||||||
@ -291,7 +291,7 @@ async def main():
|
|||||||
client.add_cog(TestCog(client))
|
client.add_cog(TestCog(client))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await client.run()
|
client.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Bot shutting down...")
|
logger.info("Bot shutting down...")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -300,7 +300,7 @@ async def main():
|
|||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
if not client.is_closed():
|
if not client.is_closed():
|
||||||
await client.close()
|
asyncio.run(client.close())
|
||||||
logger.info("Bot has been closed.")
|
logger.info("Bot has been closed.")
|
||||||
|
|
||||||
|
|
||||||
@ -310,6 +310,6 @@ if __name__ == "__main__":
|
|||||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
asyncio.run(main())
|
main()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Main loop interrupted. Exiting.")
|
logger.info("Main loop interrupted. Exiting.")
|
||||||
|
@ -62,9 +62,9 @@ async def on_ready():
|
|||||||
print("------")
|
print("------")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -63,6 +63,4 @@ async def on_ready():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import asyncio
|
client.run()
|
||||||
|
|
||||||
asyncio.run(client.run())
|
|
||||||
|
@ -9,7 +9,15 @@ from typing import Set
|
|||||||
if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__file__)):
|
if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__file__)):
|
||||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||||
|
|
||||||
from disagreement import Client, GatewayIntent, Member, Message, Cog, command, CommandContext
|
from disagreement import (
|
||||||
|
Client,
|
||||||
|
GatewayIntent,
|
||||||
|
Member,
|
||||||
|
Message,
|
||||||
|
Cog,
|
||||||
|
command,
|
||||||
|
CommandContext,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@ -26,9 +34,7 @@ if not BOT_TOKEN:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
intents = (
|
intents = (
|
||||||
GatewayIntent.GUILDS
|
GatewayIntent.GUILDS | GatewayIntent.GUILD_MESSAGES | GatewayIntent.MESSAGE_CONTENT
|
||||||
| GatewayIntent.GUILD_MESSAGES
|
|
||||||
| GatewayIntent.MESSAGE_CONTENT
|
|
||||||
)
|
)
|
||||||
client = Client(token=BOT_TOKEN, command_prefix="!", intents=intents)
|
client = Client(token=BOT_TOKEN, command_prefix="!", intents=intents)
|
||||||
|
|
||||||
@ -78,10 +84,10 @@ async def on_message(message: Message) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
def main() -> None:
|
||||||
client.add_cog(ModerationCog(client))
|
client.add_cog(ModerationCog(client))
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -137,11 +137,11 @@ async def on_reaction_remove(reaction: Reaction, user: User | Member):
|
|||||||
|
|
||||||
|
|
||||||
# --- Main Execution ---
|
# --- Main Execution ---
|
||||||
async def main():
|
def main():
|
||||||
print("Starting Reaction Bot...")
|
print("Starting Reaction Bot...")
|
||||||
try:
|
try:
|
||||||
client.add_cog(ReactionCog(client))
|
client.add_cog(ReactionCog(client))
|
||||||
await client.run()
|
client.run()
|
||||||
except AuthenticationError:
|
except AuthenticationError:
|
||||||
print("Authentication failed. Check your bot token.")
|
print("Authentication failed. Check your bot token.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -149,9 +149,9 @@ async def main():
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
if not client.is_closed():
|
if not client.is_closed():
|
||||||
await client.close()
|
asyncio.run(client.close())
|
||||||
print("Bot has been shut down.")
|
print("Bot has been shut down.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -34,12 +34,12 @@ async def on_ready():
|
|||||||
print("Shard bot ready")
|
print("Shard bot ready")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
def main():
|
||||||
if not TOKEN:
|
if not TOKEN:
|
||||||
print("DISCORD_BOT_TOKEN environment variable not set")
|
print("DISCORD_BOT_TOKEN environment variable not set")
|
||||||
return
|
return
|
||||||
await client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
@ -53,9 +53,7 @@ BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN")
|
|||||||
|
|
||||||
# --- Intents Configuration ---
|
# --- Intents Configuration ---
|
||||||
intents = (
|
intents = (
|
||||||
GatewayIntent.GUILDS
|
GatewayIntent.GUILDS | GatewayIntent.GUILD_MESSAGES | GatewayIntent.MESSAGE_CONTENT
|
||||||
| GatewayIntent.GUILD_MESSAGES
|
|
||||||
| GatewayIntent.MESSAGE_CONTENT
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# --- Initialize the Client ---
|
# --- Initialize the Client ---
|
||||||
@ -106,11 +104,11 @@ async def on_ready():
|
|||||||
|
|
||||||
|
|
||||||
# --- Main Execution ---
|
# --- Main Execution ---
|
||||||
async def main():
|
def main():
|
||||||
print("Starting Typing Indicator Bot...")
|
print("Starting Typing Indicator Bot...")
|
||||||
try:
|
try:
|
||||||
client.add_cog(TypingCog(client))
|
client.add_cog(TypingCog(client))
|
||||||
await client.run()
|
client.run()
|
||||||
except AuthenticationError:
|
except AuthenticationError:
|
||||||
print("Authentication failed. Check your bot token.")
|
print("Authentication failed. Check your bot token.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -118,9 +116,9 @@ async def main():
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
if not client.is_closed():
|
if not client.is_closed():
|
||||||
await client.close()
|
asyncio.run(client.close())
|
||||||
print("Bot has been shut down.")
|
print("Bot has been shut down.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user