Sure, here's an example of a Discord.py command group that sets and removes a message with a button on it, and on interaction it creates a text channel only visible to that user that interacted and sends a welcome message mentioning the user:
import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle, InteractionType
bot = commands.Bot(command_prefix='!')
DiscordComponents(bot)
welcome_messages = {}
async def create_welcome_channel(guild, member):
# Get category for creating the text channel
category_name = "Welcome Channels"
category = discord.utils.get(guild.categories, name=category_name)
if category is None:
# Category doesn't exist, create it
category = await guild.create_category(category_name)
# Create text channel for the user
channel_name = f"{member.name.lower()}-welcome"
channel = await guild.create_text_channel(channel_name, category=category)
await channel.set_permissions(member, read_messages=True, send_messages=True)
# Send welcome message in the new channel
welcome_message = f"Welcome <@{member.id}>! This is your private welcome channel."
await channel.send(welcome_message)
@bot.event
async def on_button_click(interaction):
# Check if button was clicked on a welcome message
if interaction.message.id in welcome_messages:
guild = interaction.guild
member = interaction.author
await create_welcome_channel(guild, member)
await interaction.respond(
type=InteractionType.ChannelMessageWithSource,
content=f"<@{member.id}> Your private welcome channel has been created."
)
@bot.group()
async def welcome(ctx):
"""Manage welcome messages"""
if ctx.invoked_subcommand is None:
await ctx.send('Invalid welcome command.')
@welcome.command()
async def set(ctx, message):
"""Set a welcome message with a button"""
# Add welcome message with button for server
button = Button(style=ButtonStyle.green, label="Welcome!")
message_embed = discord.Embed(description=message)
message_embed.set_footer(text="Click the button to create your private welcome channel.", icon_url=ctx.author.avatar_url)
message = await ctx.send(embed=message_embed, components=[[button]])
welcome_messages[message.id] = message.content
@welcome.command()
async def remove(ctx):
"""Remove the welcome message with button"""
# Remove welcome message with button for server
for message_id in welcome_messages:
try:
message = await ctx.channel.fetch_message(message_id)
await message.delete()
welcome_messages.pop(message_id)
except discord.NotFound:
pass
await ctx.send('Welcome message removed.')
To use this command group, you would need to add it to your existing Discord.py bot implementation and run the bot, then use the following commands in a Discord server where the bot is a member:
!welcome set <message>: Sets a welcome message with a button that creates a private welcome channel for the user who clicks the button. The <message> parameter can be any text you want to display in the welcome message.
!welcome remove: Removes the welcome message with button for the server.