discordbot/example_global_commands.py
2025-05-13 08:08:17 -06:00

98 lines
3.7 KiB
Python

"""
Example module demonstrating how to create global commands that work in DMs and private channels.
This module shows how to use the AppCommandContext system to create commands
that can be used in both guild and private contexts.
"""
import discord
from discord import app_commands
from discord.ext import commands
from command_context import AppCommandContext, create_global_command
from typing import Optional
class GlobalCommandsCog(commands.Cog):
"""A cog that demonstrates global commands that work in DMs and private channels."""
def __init__(self, bot: commands.Bot):
self.bot = bot
print("GlobalCommandsCog initialized!")
# Register global commands
self.register_global_commands()
def register_global_commands(self):
"""Register global commands that work in DMs and private channels."""
# Create a help command that works in DMs and private channels
create_global_command(
self.bot.tree,
name="dmhelp",
description="Get help with bot commands (works in DMs)",
callback=self.dm_help_callback,
context=AppCommandContext.ALL
)
# Create a ping command that works in DMs and private channels
create_global_command(
self.bot.tree,
name="dmping",
description="Check if the bot is responsive (works in DMs)",
callback=self.dm_ping_callback,
context=AppCommandContext.ALL
)
# Create a command that only works in private contexts
create_global_command(
self.bot.tree,
name="privateonly",
description="This command only works in DMs and private channels",
callback=self.private_only_callback,
context=AppCommandContext.PRIVATE
)
print("GlobalCommandsCog: Registered global commands")
async def dm_help_callback(self, interaction: discord.Interaction):
"""Callback for the /dmhelp command."""
is_dm = isinstance(interaction.channel, discord.DMChannel)
is_private = not interaction.guild
help_text = (
"# Bot Help\n\n"
"This help command works in both DMs and servers!\n\n"
f"**Current context:** {'DM' if is_dm else 'Private Channel' if is_private else 'Server'}\n\n"
"## Available Commands\n"
"- `/dmhelp` - This help command\n"
"- `/dmping` - Check if the bot is responsive\n"
"- `/privateonly` - Only works in DMs and private channels\n"
)
await interaction.response.send_message(help_text, ephemeral=True)
async def dm_ping_callback(self, interaction: discord.Interaction):
"""Callback for the /dmping command."""
is_dm = isinstance(interaction.channel, discord.DMChannel)
is_private = not interaction.guild
await interaction.response.send_message(
f"🏓 Pong! Bot is responsive.\n"
f"**Current context:** {'DM' if is_dm else 'Private Channel' if is_private else 'Server'}",
ephemeral=True
)
async def private_only_callback(self, interaction: discord.Interaction):
"""Callback for the /privateonly command."""
is_dm = isinstance(interaction.channel, discord.DMChannel)
await interaction.response.send_message(
f"This command only works in private contexts.\n"
f"**Current context:** {'DM' if is_dm else 'Private Channel'}",
ephemeral=True
)
async def setup(bot: commands.Bot):
"""Add the GlobalCommandsCog to the bot."""
await bot.add_cog(GlobalCommandsCog(bot))
print("GlobalCommandsCog setup complete.")