Cheering up my gaming community with PYTHON

Due to the COVID-19 Pandemic, the general mood of my community has been very solemn. So one day an idea of a bot came upon me, and I knew I just had to find a way to implement it.

So I started with a very simple algorithm that grabs a random quote from a hand-picked list of quote strings in a CSV file. From there, the script loops through each one of it's connected servers and it posts one quote to each server per day. Or at least, this is the way I intended for it to work.

However, after my first test this happened Screen Shot 2020-05-11 at 2.59.23 PM.png

To fixed this, I just needed to make a few adjustments to the code. So I ended up with this logic:

```class MyClient(discord.Client): async def on_ready(self): print('Logged on as {0}!'.format(self.user)) print(self.user.id) print('------')

loops through connected servers and assigns increments server count variable

    server_count = 0
    quote_count = 0
    for guild in client.guilds:
        server_count += 1
    print("The server count is {}".format(server_count))
    for quote in quotes:
        quote_count += 1
    print("There number of quotes available is: " + str(quote_count))
    print('------')
    while not client.is_closed():
        now = datetime.strftime(datetime.now(), '%H:%M')
        iter_count = 0
        print("current time is: " + now)
        if now == wakeup_time:  # enter for-loop at specific time
            for guild in client.guilds:
                for channel in guild.channels:
                    if str(channel) == 'chat':
                        await channel.send('Good Morning/Afternoon/Good Night. Here is today\'s random quote:')
                        await channel.send('```' + str(random.choice(quotes)).strip('[]') + '```')
                        print("{} Server Greeted.").format(guild.name)
                        iter_count += 1
                        if iter_count == server_count:
                            await asyncio.sleep(7200)  # puts loop on hold for 1 hour
        else:
            await asyncio.sleep(10)```

After testing locally, the script ran successfully and now I was ready to put this into production and have it on my active servers for all to enjoy. So I scheduled the bot to run at 9am the next morning and set my alarm so that I would be up to see people's reactions when it happened.

After waking up the next morning, I checked to see that it malfunctioned YET again. Each server seven back-to-back quotes posted to them.

Screen Shot 2020-05-11 at 3.10.46 PM.png

I was baffled. Even though the script was working correctly during my local tests. When I let it sit on the server overnight, it began to malfunction the next day. So I decided to incorporate some logging and have my logs output to a file to monitor which background processes were being triggered overnight.

It turns out that the servers which host my scripts constantly go down, causing my scripts to re-run themselves periodically. The only problem is that the last instance isn't closed before this happens. This caused there to be several instances of the same script to be running simultaneously. This explains why this is not happening during testing, but happening later on the servers.

I consulted with more experienced devs on the matter and received conflicting ideas on how to approach the issue. None of them could agree on a proper way to check for instances running and killing them before running the script again. Ultimately what I decided to do for the time being is use a Cron service on the server end to run the python script on a set schedule. This ensure that it only runs one time and at a specified time. It also drastically decreases the complexity of the python code itself.

After implementing the Cron Job, I checked for the next two days and it seemed to be working as intended. As of now, it is on a few of the community servers and members are getting a good laugh from it every morning. As usual, I will be tweaking and adding functionality to the bot, as I'm sure the group will appreciate it.

Screen Shot 2020-05-11 at 2.31.51 PM.png

This article is my 8th oldest. It is 611 words long, and it’s got 0 comments for now.