I am writing a small discord bot for me and my friends but I have gotten carried away with it's feautres and right now am trying to create commands that will allow the user to ask to look at a "bank" and it will respond with the total in the bank. i can easily take away the string from the txt file and split it to my needs but my only issue is deleting the line and then after trying to add on. for context, the txt file will normally say "[party name] bank total: [total of moneyz]"
@commands.command()
async def bankadd(self, ctx, party, total):
total = int(total)
party = str(party)
if party == "help":
await ctx.send("the parties that have banks open are: [parties here]")
await ctx.send("please select one of these parties")
elif party == "[partyName]":
IB = open("INBank.txt","r")
i = IB.read()
d = i.split(" ")
num = d[3]
num = int(num)
IB.close()
newTotal = total + num
I have tried to do some research on my own but I have been going in circles and I am not certain on how to use the functions in future, so rather than just an explanation on the solution, I want to know how I can use that solution and more bits of information that are grinded down for my brain to process and understand.
You can use the aiofiles library for asynchronous file reading and writing. Otherwise your bot will stop until the file is read/written/closed/opened. Aiofiles library also provides a thread pool for file processes so it is reliable when you have multiple people using the same command to edit the file.
def update_party_data(file_lines, party_name, new_value):
edited_file_lines = file_lines
for i,line in enumerate(file_lines): #Get the line with index
if line.startswith(party_name):
index = i
selected_line = line.rstrip("\n") #Remove newline character
break
info = selected_line.split("|")
info[2] = str(new_value)
edited_file_lines[index] = "|".join(info) + "\n"
return edited_file_lines
def get_parties_from_file_content(file_lines):
#We will perform some formatting here to extract information
parties = {}
for line in file_lines:
info = line.split("|")
party_name = str(info[0])
money = int(info[2])
parties[party_name] = money
return parties
@commands.command()
async def bankadd(self, ctx, party, total=0):
total = int(total)
party = str(party)
async with aiofiles.open('INBank.txt', mode='r') as f:
file_lines = await f.readlines()
all_parties = get_parties_from_file_content(file_lines)
if party == "help":
await ctx.send(f"The parties that have banks open are:\n {list(all_parties.keys())}")
await ctx.send("Please select one of these parties.")
else:
print(all_parties)
party_balance = all_parties[party]
file_lines = update_party_data(file_lines,party,party_balance+total)
async with aiofiles.open('INBank.txt', mode='w') as f:
await f.writelines(file_lines)
I used the '|' pipe character to seperate information. This allows us to use spaces between party names. When inputting spaced party names to bot just use quotes around arguments. The text file should look something like this:
the bulls|Balance|204
second party|Balance|237
my party|Balance|10
Use import aiofiles
to import it.