Live Updates
Working on a bot can become troublesome when attempting to implement a new feature or when working on a new command that entails many changes. Some commands may also be refactors of pre-existing ones which will make the deployed bot go a little crazy.
The best way that I've found to working on my bot while currently running is to run two separate instances of the bot. I have the live bot running on a Raspberry Pi with up to date Python and Git installed (which was a bit of a challenge to work around).
In the past I've had a separate line that was commented out during my commits for the bot until I needed to develop it using a different prefix. This worked for a while but was a bit tiresome to keep switching the comment on and off. Sometimes forgetting entirely and pushing the uncommented line up to the live code. This called for a git --amend --no-edit
commit which was again another step out of the way.
The Git fix
As of Git version 2.22, calling git branch --show-current
would output the current name of the checked out branch. With a few lines, that name can be placed in a file and checked during each command to tell the bot what prefix to use.
For my use I placed this in a generic Functions.py file and import the function for when I call on the Client().
import nextcord
import json
import os
client = nextcord.Client()
def getPrefix(client, message):
try:
os.system("git branch --show-current > ./references/git-current-branch")
with open("./reference/git-current-branch", "r") as currentBranch:
repo = currentBranch.read()
finally:
# If on development branch or
# If not main branch whichever is easier
if repo == "development\n":
return "." # Prefix to separate from live code
# Return the prefix for whichever server called the command
else:
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
The os.system
is a neat way of having the bot pass commands directly to the command line, in this case to show the current git branch name.
NOTE:
When using the '>' this will mean to overwrite the data in that file which is what we want.
Using '>>' instead will append to the existing file and cause a lot of clutter and won't be usable without the use of writing another function to trim it out.
This will be just another tool to help aid with development of a live bot without disruption and prevent errors like sending the wrong prefix like I had done a few too many times.
Top comments (0)