📝 Add examples

This commit is contained in:
2025-03-08 20:42:14 +01:00
parent bfcb7cc33d
commit 7dde4fd16f
5 changed files with 217 additions and 6 deletions

46
examples/basic_bot.py Normal file
View 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",
},
)

View File

@@ -0,0 +1,52 @@
"""Example demonstrating how to use buttons with Pycord REST."""
import os
from typing import Any
import discord
from dotenv import load_dotenv
from pycord_rest import App
# Load environment variables from .env file
load_dotenv()
app = App()
class MyView(discord.ui.View):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.add_item(
discord.ui.Button(
style=discord.ButtonStyle.link, label="GitHub", url="https://github.com/Paillat-dev/pycord-rest"
)
)
@discord.ui.button(label="Green", style=discord.ButtonStyle.success)
async def green_button(self, button: "discord.ui.Button[MyView]", interaction: discord.Interaction) -> None:
await interaction.respond("You clicked the green button!", ephemeral=True)
@discord.ui.button(label="Red", style=discord.ButtonStyle.danger)
async def red_button(self, button: "discord.ui.Button[MyView]", interaction: discord.Interaction) -> None:
await interaction.respond("You clicked the red button!", ephemeral=True)
# Create a slash command that shows buttons
@app.slash_command(name="buttons", description="Shows interactive buttons")
async def buttons(ctx: discord.ApplicationContext) -> None:
# Create a view with buttons
view = MyView()
await ctx.respond("Choose a button:", view=view)
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",
},
)

60
examples/modal_example.py Normal file
View File

@@ -0,0 +1,60 @@
"""Example showing how to work with modals in Pycord REST."""
import asyncio
import os
from typing import Any
import discord
from dotenv import load_dotenv
from pycord_rest import App
# Load environment variables from .env file
load_dotenv()
app = App()
class MyModal(discord.ui.Modal):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(
discord.ui.InputText(
label="Name", placeholder="Enter your name", style=discord.InputTextStyle.short, custom_id="name_input"
),
discord.ui.InputText(
label="Feedback",
placeholder="Please provide your feedback here...",
style=discord.InputTextStyle.paragraph,
custom_id="feedback_input",
),
*args,
**kwargs,
)
async def callback(self, interaction: discord.Interaction) -> None:
name = self.children[0].value
await interaction.respond(
f"Thank you for your feedback, {name}! Your submission has been received.", ephemeral=True
)
# Command that shows a form modal
@app.slash_command(name="feedback", description="Submit feedback through a form")
async def feedback(ctx: discord.ApplicationContext) -> None:
# Create a modal
modal = MyModal(title="Feedback Form")
await ctx.send_modal(modal)
await ctx.respond("Opening feedback form...", ephemeral=True)
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",
},
)