disagreement/examples/example_from_readme.py
Slipstream 3c638d17ce
Adds latency display and improves bot configuration
Adds millisecond latency property to gateway and client for more precise monitoring.

Enhances ping command to display gateway latency for better debugging.

Configures message content intent and mention replies for improved bot functionality.

Adds dotenv support for cleaner environment variable management.

Formats error classes for better code consistency and readability.
2025-06-11 15:13:37 -06:00

42 lines
1.1 KiB
Python

# Example from the README file of the disagreement library
# This example demonstrates a simple bot that responds to the "!ping" command with "Pong!".
import asyncio
import os
import disagreement
from disagreement.ext import commands
from dotenv import load_dotenv
load_dotenv()
class Basics(commands.Cog):
def __init__(self, client: disagreement.Client) -> None:
super().__init__(client)
@commands.command()
async def ping(self, ctx: commands.CommandContext) -> None:
await ctx.reply(f"Pong! Gateway Latency: {self.client.latency_ms} ms.") # type: ignore (latency is None during static analysis)
token = os.getenv("DISCORD_BOT_TOKEN")
if not token:
raise RuntimeError("DISCORD_BOT_TOKEN environment variable not set")
intents = (
disagreement.GatewayIntent.default() | disagreement.GatewayIntent.MESSAGE_CONTENT
)
client = disagreement.Client(
token=token, command_prefix="!", intents=intents, mention_replies=True
)
async def main() -> None:
client.add_cog(Basics(client))
await client.run()
if __name__ == "__main__":
asyncio.run(main())