mirror of
https://github.com/Paillat-dev/pycord-rest.git
synced 2026-01-02 00:56:19 +00:00
📝 Add examples
This commit is contained in:
46
examples/basic_bot.py
Normal file
46
examples/basic_bot.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""Basic Discord bot example using Pycord REST.
|
||||
|
||||
This is a minimal example showing how to create slash commands.
|
||||
"""
|
||||
|
||||
import os
|
||||
from pydoc import describe
|
||||
|
||||
import discord
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from pycord_rest import App
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
app = App()
|
||||
|
||||
|
||||
# Simple ping command
|
||||
@app.slash_command(name="ping", description="Responds with pong!")
|
||||
async def ping(ctx: discord.ApplicationContext) -> None:
|
||||
await ctx.respond("Pong!")
|
||||
|
||||
|
||||
# Command with parameters
|
||||
@app.slash_command(name="greet", description="Greets a user")
|
||||
@discord.option("name", input_type=str, description="The name of the user to greet", required=False)
|
||||
async def greet(ctx: discord.ApplicationContext, name: str | None = None) -> None:
|
||||
if name:
|
||||
await ctx.respond(f"Hello, {name}!")
|
||||
else:
|
||||
await ctx.respond(f"Hello, {ctx.author.display_name}!")
|
||||
|
||||
|
||||
# Run the app
|
||||
if __name__ == "__main__":
|
||||
app.run(
|
||||
token=os.environ["DISCORD_TOKEN"],
|
||||
public_key=os.environ["DISCORD_PUBLIC_KEY"],
|
||||
uvicorn_options={
|
||||
"host": "0.0.0.0", # noqa: S104
|
||||
"port": 8000,
|
||||
"log_level": "info",
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user