How to quit a discord bot without using ‘except’ python

need quick help this is the code that simply posts to a discord channel I am using it as a subpart of my project all I want from this to post and shuts (without killing the program) so I can’t use ‘exit’, I have used while loops and all that but nothing is working.

def post_discord():
    print('POSTING NEW OFFER TO DISCORD')
    token = 'token here'
    while True:
        bot = commands.Bot(command_prefix='!')
        @bot.command()
        async def on_ready():

            await bot.wait_until_ready()
            channel = bot.get_channel(12345678) # replace with channel ID that you want to send to
                # await channel.send()
            await channel.send(text,file =discord.File('promo.png'))

            await asyncio.sleep(2)

            # exit()
            # await bot.close()





        bot.loop.create_task(on_ready())

        bot.run(token)
        time.sleep(3)

Answer

If you really want to just stop the bot from running after it finishes doing whatever you want it to do, what you’re looking for is discord.Client.logout(). In your scenario, the usage would be bot.logout()

Although, as someone who replied to your post said, a webhook may be more suitable for your use case. A simple webhook for sending a message using requests would look something like this:

import requests


data = {"content": "Message Content"}
requests.post("Webhook URL", data=data)

Discord’s documentation for this can be found here, notable limitations for this route are that you’re unable to attach files, but if you’re just needing to send an image along with some text, a .png link may suffice as Discord will hide the link in the message.