custom_invite.py

mail@pastecode.io avatar
unknown
python
a month ago
2.1 kB
4
Indexable
Never
import discord
from discord import app_commands
from discord.ext import commands
from datetime import datetime, timedelta, timezone

class CustomInvite(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @app_commands.command(name='custominvite', description="Create a custom invite link with role assignment")
    @app_commands.checks.has_permissions(administrator=True)
    async def custominvite(self, interaction: discord.Interaction, invite_link: str, role: discord.Role, expire: int):
        guild = interaction.guild
        owner = guild.owner

        invite_code = invite_link.split('/')[-1]

        try:
            invite = await self.bot.fetch_invite(invite_code)
            expire_time = None
            if expire > 0:
                expire_time = datetime.now(timezone.utc) + timedelta(seconds=expire)

            embed = discord.Embed(title="Custom Invite Created", description=f"Welcome to **{guild.name}**!", color=discord.Color.blue())
            embed.add_field(name="Invite Link", value=f"[Click here to join]({invite_link})", inline=False)
            embed.add_field(name="Role", value=role.mention, inline=False)
            if expire_time:
                embed.add_field(name="Expiry", value=f"<t:{int(expire_time.timestamp())}:R>", inline=False)
            else:
                embed.add_field(name="Expiry", value="No expiry", inline=False)
            embed.add_field(name="Server Owner", value=owner.display_name, inline=False)

            await interaction.response.send_message(embed=embed, ephemeral=True)

        except discord.NotFound:
            await interaction.response.send_message("Invalid invite link provided. Please check the link and try again.")
        except discord.HTTPException as e:
            await interaction.response.send_message(f"Failed to create custom invite: {e}")

    async def cog_load(self):
        print(f"Slash command '/custominvite' is being registered.")

async def setup(bot):
    await bot.add_cog(CustomInvite(bot))
    print("CustomInvite Cog loaded successfully.")
Leave a Comment