Programming: Python - Xieons-Gaming-Corner/Wiki GitHub Wiki
-
Synchronous Commands:
- Definition: Run one at a time, blocking execution until complete.
-
Behavior: Halts the bot, waiting for the task (e.g.,
time.sleep(5)
blocks 5 seconds). - Pros: Simple, predictable order.
- Cons: Freezes bot during delays, unresponsive to other events.
-
Example:
ctx.send("Pong!")
aftertime.sleep(5)
delays all bot actions.
-
Asynchronous Commands:
-
Definition: Run concurrently, non-blocking, using
async
/await
. -
Behavior: Pauses only the current task (e.g.,
await asyncio.sleep(5)
), letting the bot handle other events. - Pros: Keeps bot responsive, ideal for multi-user tasks.
- Cons: More complex syntax.
-
Example:
await ctx.send("Pong!")
afterawait asyncio.sleep(5)
allows other actions during wait.
-
Definition: Run concurrently, non-blocking, using
-
In XGCC Bot: Uses async (e.g.,
await asyncio.sleep(5)
inon_message
), ensuring no blocking during 5-second delays for<<
/>>
messages.
What Are Constants?
Constants in Python are variables meant to stay unchanged during execution. Unlike other languages (e.g., C++ with const
), Python doesn’t enforce immutability—it’s a convention signaled by naming.
Naming Rules (PEP 8):
-
ALL_UPPERCASE: Use uppercase letters with underscores separating words (e.g.,
LOG_CHANNEL_ID
). - Signals intent: "This shouldn’t change!"
- Example from XGC Bot:
XGC_ADMINS_ROLE_ID = 1069107794525036567
.
Key Points:
-
Case Doesn’t Affect Code: Python is case-sensitive (
LOG_CHANNEL_ID
≠log_channel_id
), but capitalization alone doesn’t make it a constant—convention does. -
No Enforcement: You can reassign
LOG_CHANNEL_ID = 123
, but you shouldn’t. -
Contrast with Variables: Lowercase (e.g.,
temp_value
) is for mutable values.
Best Practices:
- Define constants at the module level (top of your file).
- Group related constants (e.g.,
DEVOPS_ROLE_ID
,XGC_ADMINS_ROLE_ID
). - Avoid redundancy—don’t define
LOG_CHANNEL
andLOG_CHANNEL_ID
for the same value. - Use comments for clarity (e.g.,
# XGC Bot Logs channel ID
).
Why It Matters:
- Readability:
SERVER_ID
screams "constant" vs.server_id
. - Teamwork: Consistent naming helps others (or future you) maintain code like
XGCC.py
Example (XGC Bot):
SERVER_ID = 123123123 # Xieon's Gaming Corner server ID
XIEON_DUID = 123123123 # Xieon's Discord ID
LOG_CHANNEL_ID = 123123123 # XGC Bot Logs channel ID```