#Get invite command

1 messages · Page 1 of 1 (latest)

willow mauve
#

I tried a thing to get an invite link from a server where my bot is with the id of this serv but with every id teh bot say me that the id is invalid

@bot.slash_command(name="get_invite", description="Get an invite link for the specified server ID")
@discord.default_permissions(administrator = True)
async def get_invite(ctx, server_id : str):
    if ctx.author.id == 707985125299716212 :
        try:
            server = bot.get_guild(server_id)
        
            if server:
                channel = discord.utils.get(server.channels, type=discord.ChannelType.text)
            
                if channel:
                    invite = await channel.create_invite(max_age=86400, max_uses=1, reason="Creating invite for the server")
                    await ctx.respond(f"Invite link for server '{server.name}': {invite.url}")
                else:
                    await ctx.respond("No available text channels to create an invite.")
            else:
                await ctx.respond("Server not found. Please provide a valid server ID.")
        except discord.HTTPException as e:
            await ctx.respond(f"An error occurred: {str(e)}")
        except discord.NotFound:
            await ctx.respond("Server not found. Please provide a valid server ID.")
    else : 
        ctx.respond("You don't have the permission")
#

for those who will said to read the doc i read it

abstract quarry
#

?tag get_x

junior wedgeBOT
#

Any function that starts with get_ in Py-cord is retrieving the related object from your bots cache. If the object is not in the bots cache the get_ method will return None. Because of this behavior you should check if the get_x method is None and if it is use the fetch_x method.

Why Is Using fetch_ Without Using get_ First Bad?
The fetch_ method makes a call to the discord API. This API call is unneeded if you already have the information. It will also make your command take longer because it will have to send and than wait for a response from the discord API. It will also contribute to the discord APIs global rate limit of 50 requests per second.

What Is Cache?
The cache is a temporary storage inside your bot. It holds many objects from members to messages. When you restart your bot the cache will be empty. When the cache is full it will delete older objects to make space for the new objects.

willow mauve
calm dragon
#

First you get server_id as a string and get_guild needs it to be an int

Otherwise the guild is not in the cache so you need to fetch it.