'How to make a discord bot generate invite links from other servers, (Discord.py or py-cord)

I am making a discord bot in discord.py, py-cord, and I was wondering if it's possible to have the bot generate a discord invite for a server and send it to you and how to do it?

Is it possible? And if so how do I do it?

I have figured it out thx for the help.

@client.command()
async def getinv(ctx):
    invites = []
    for guild in client.guilds:
        for c in guild.text_channels:
            # make sure the bot can actually create an invite
            if c.permissions_for(guild.me).create_instant_invite:
                invite = await c.create_invite()
                invites.append(invite)
                await ctx.send(invite)
                break


Solution 1:[1]

A slightly improved version of the previous guys code

@client.event
async def on_ready():
    for guild in client.guilds:
        discord_guild = client.get_guild(int(guild.id))
        link = await discord_guild.text_channels[0].create_invite()
        print(link)

Solution 2:[2]

I've wanted to do the same thing, and I put together this script that prints invites to every server the bot is in.

from discord.utils import get
import discord
import asyncio


client = discord.Client()
TOKEN = "token here"

@client.event
async def on_ready():
  print('Bot is up and running.')
  print(f'Logged in as {client.user}')

@client.event
async def on_message(message, *ctx):
    for guild in client.guilds:
        discord_guild = client.get_guild(int(guild.id))
        link = await discord_guild.text_channels[0].create_invite()
        print(link)
        asyncio.sleep(750)

client.run(TOKEN)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Migi
Solution 2 Dharman