Saving bot generated text to an external file
Question:
I just started learning and using Python today and I am working on a discord bot that allows for custom user queries. In this current iteration, the user can specify how many dice and how many sides are rolled. The bot then displays the results in Discord. I am wanting to save these results in an external log file for future use.
Currently, everything works, including writing "Dice results:" in the log, but nothing else gets recorded.
This is the bot command that receives the user input and then generates results. It also outputs part of what I want it to, but not the message or rather the results themselves.
@bot.command(name='diceroll', help='Simulates rolling dice.')
async def roll(ctx, number_of_dice: int, number_of_sides: int, *args, **kwargs):
dice = [
str(random.choice(range(1, number_of_sides + 1)))
for _ in range(number_of_dice)
]
with open('dicerolls.log', 'a') as f:
f.write('Dice results:n')
await ctx.send(', '.join(dice))
If I posted this wrong I apologize, but I believe I shared the code in question correctly. Also, please let me know if any other info is needed.
Answers:
f.write('Dice results:n')
– see you only write to the log the string ‘Dice results:n’ and nothing else. You can add what you want by this pattern f'Dice results: {dice}n'
On a side note, you don’t want to do it this way but rather use the logging
library and log to a file. Similar to this:
Somewhere in the beginning of the program:
import logging
logging.basicConfig(filename='dicerolls.log', encoding='utf-8', level=logging.DEBUG)
Inside your function:
logging.info(f'Dice results: %s', dice)
It looks like ctx.send(', '.join(dice))
would just be sending a comma-separated string of dice values. I’m guessing you would want to return the sum of the dice values? If so, then you could calculate the the sum before sending and before logging by changing `str(random.choice(range(1, number_of_sides + 1)))
change to this
random.choice(range(1, number_of_sides + 1))
calculate result
result = sum(dice)
`
Then you can write the str(result) to the file and send the str(result) in ctx.send().
I just started learning and using Python today and I am working on a discord bot that allows for custom user queries. In this current iteration, the user can specify how many dice and how many sides are rolled. The bot then displays the results in Discord. I am wanting to save these results in an external log file for future use.
Currently, everything works, including writing "Dice results:" in the log, but nothing else gets recorded.
This is the bot command that receives the user input and then generates results. It also outputs part of what I want it to, but not the message or rather the results themselves.
@bot.command(name='diceroll', help='Simulates rolling dice.')
async def roll(ctx, number_of_dice: int, number_of_sides: int, *args, **kwargs):
dice = [
str(random.choice(range(1, number_of_sides + 1)))
for _ in range(number_of_dice)
]
with open('dicerolls.log', 'a') as f:
f.write('Dice results:n')
await ctx.send(', '.join(dice))
If I posted this wrong I apologize, but I believe I shared the code in question correctly. Also, please let me know if any other info is needed.
f.write('Dice results:n')
– see you only write to the log the string ‘Dice results:n’ and nothing else. You can add what you want by this pattern f'Dice results: {dice}n'
On a side note, you don’t want to do it this way but rather use the logging
library and log to a file. Similar to this:
Somewhere in the beginning of the program:
import logging
logging.basicConfig(filename='dicerolls.log', encoding='utf-8', level=logging.DEBUG)
Inside your function:
logging.info(f'Dice results: %s', dice)
It looks like ctx.send(', '.join(dice))
would just be sending a comma-separated string of dice values. I’m guessing you would want to return the sum of the dice values? If so, then you could calculate the the sum before sending and before logging by changing `str(random.choice(range(1, number_of_sides + 1)))
change to this
random.choice(range(1, number_of_sides + 1))
calculate result
result = sum(dice)
`
Then you can write the str(result) to the file and send the str(result) in ctx.send().