import discord import asyncio async def guild_create(bot: discord.Client, guild_name: str, user_id: int): """ Creates a guild and sends an invite link to the specified user via DM. Args: bot: The bot instance. guild_name: Name of the new guild. user_id: Discord user ID to DM the invite to. """ # Create the guild guild = await bot.create_guild(name=guild_name) # Wait for guild to be fully available in cache (adjust time if needed) for _ in range(10): await asyncio.sleep(2) if bot.get_guild(guild.id): break # Try to find a text channel where we can create an invite for channel in guild.text_channels: if channel.permissions_for(guild.me).create_instant_invite: invite = await channel.create_invite(max_age=3600, max_uses=1, unique=True) break else: raise RuntimeError("No channel found with permission to create invites.") # Send invite via DM user = await bot.fetch_user(user_id) try: await user.send(f"Here is your invite to **{guild_name}**: {invite.url}") except discord.Forbidden: raise RuntimeError("Unable to send DM to user.") return guild