Welcome back, developers.
Greetings to all Steem enthusiasts! Today, we will learn how to format our generated report and write it into a .txt file and how to attach this file via Discord bot.

Summary |
---|
We established the necessary environment for developing a Python-based Discord bot during our first lecture. In our second lecture we create a discord bot token, configure the bot and generate the invite link. We used the invite link and added the bot to our server. In our third lecture, we will bring the Community-Bot online in our local environment.
In the 4th lecture, we successfully developed our initial bot command, !info. We also learned how to create functions and retrieve data using the requests library in 5th lecture. We implemented the !info command in 6th lecture which is used fetch the information of any steem user.
In the 7th lecture, we learned how to send an embedded message with columns, images, and a profile image. In lecture 8th we learn how to generate a report for the community. In today's lecture, we will process that report and write the response intro in a .txt file.
Procedure |
---|
- First, we need to format our report according to our requirements. In this report, I want to display usernames, posts, comments, and unique comments in separate columns. Let's utilize markdown styling to generate the report.
async def get_community_report(self, community):
api = self.utils.sds_base + f"/feeds_api/getActiveCommunityReport/{community}"
response = requests.get(api).json()
community_report = map_sds_response(response)
formatted_report = ''
for item in community_report:
formatted_report = (
f'| {item.get("author")} | {item.get("total_post_count")} | {item.get("total_comment_count")} | '
f'{item.get("unique_comment_count")} |\n') + formatted_report
return '|Username|Posts|Comments|Unique Comments|\n|-|-|-|-|\n' + formatted_report
We have obtained the formatted community report, which can also be customized. Now, our task is to write the report into a text file. To do this, we must create an empty directory within our project. We need to specify the path and use Python code to create the directory.
Add a path variable in utils.py file.
self.folder_path = 'community-bot/files/'
- Now add and use the create directory code in main.py.
def create_directory(path):
# Check whether the specified path exists or not
is_exist = os.path.exists(path)
if not is_exist:
# Create a new directory because it does not exist
os.makedirs(path)
# Commit
create_directory(utils.folder_path)
- To write to a text file, we need to first create the file using this code. The function requires two parameters: the file name and the content we wish to write to the file.
def create_file(file_name, content):
f = open(utils.folder_path + file_name, "w")
f.write(content)
f.close()
- Now we can get community reports in the defined format and create and write the file. Here how can we do this?
if command == utils.commands[1]:
if len(props) >= 2:
await message.channel.typing()
community = str(props[1]).lower().replace('@', '')
community_report = await steemfun.get_community_report(community)
create_file(f"{community}-report.txt", community_report)
await message.reply(f'**{community} Report (7-Days):**',
file=discord.File(utils.folder_path + f"{community}-report.txt"))
- Here is the final result
Github |
---|
Steem Discord Bot Series |
---|
Lecture #1: Develop a Discord bot for your Steem Community
Lecture #2: How to Add a Bot to Your Discord Server
Lecture #3: Bring the Steem Discord Bot Online
Lecture #4: First Bot Command
lecture #5: Retrieving Steemit Account Information using SDS
Lecture #6: Implementing !info Command
Lecture #7: Discord Embed Reply with User Info
Lecture #8: Generate Community Report
SteemPro Official |
---|
Download SteemPro Mobile
https://play.google.com/store/apps/details?id=com.steempro.mobileVisit here.
https://www.steempro.comSteemPro Discord
Official Discord Server
Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01
Cc: @pennsif
Cc: @future.witness
Cc: @stephenkendal
Cc: @justyy
Best Regards @faisalamin