pythondiscorddiscord.py

Make bot automatically quit after it has done its work


I'd like to use my bot not as a daemon that runs forever, but as a kind of "shell script". So it should automatically quit after it has done its work.

My approach so far in Python 3.5 / Python 3.6:

import discord
TOKEN = '<redacted>'
client = discord.Client()

@client.event
async def on_ready():
    for member in client.get_all_members():
        print(member.display_name)
    # please, please quit now!

client.run(TOKEN)

What I want here is, that the script should just quit after printing all the members' display_names, so I can further process them elsewhere. At the moment it DOES print the desired information, but continues to run forever.


Solution

  • Answering my question myself:

    Just replace the line # please, please quit now! with await client.logout() does the job.