countdown-bot

A Discord bot that runs countdown games and generates analytics
git clone https://git.ashermorgan.net/countdown-bot/
Log | Files | Refs | README

commit e5286c020ef4149826a1047b824fb26d76ef9f12
parent 2aa6e6832241ce1f1c18bcd508b84fa0f34b0f53
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Thu, 22 Apr 2021 14:13:44 -0700

Use separate figures for each command call

Diffstat:
Mbot.py | 55++++++++++++++++++++++++++-----------------------------
1 file changed, 26 insertions(+), 29 deletions(-)

diff --git a/bot.py b/bot.py @@ -697,11 +697,10 @@ async def contributors(ctx, option=""): if (len(channel["countdown"].messages) == 0): embed.description = "The countdown is empty." elif (option.lower() in ["h", "history"]): - # Create plot - plt.close() - plt.xlabel("Progress") - plt.ylabel("Percentage of Contributions") - ax = plt.gca() + # Create figure + fig, ax = plt.subplots() + ax.set_xlabel("Progress") + ax.set_ylabel("Percentage of Contributions") ax.yaxis.set_major_formatter(PercentFormatter()) # Get historical contributor data @@ -718,29 +717,29 @@ async def contributors(ctx, option=""): # Plot data and add legend for author in list(authors.keys())[:min(len(authors), 15)]: # Top 15 contributors get included in the legend - plt.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]], label=await getUsername(author)) + ax.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]], label=await getUsername(author)) for author in list(authors.keys())[15:max(len(authors), 15)]: - plt.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]]) - plt.legend(bbox_to_anchor=(1,1.025), loc="upper left") + ax.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]]) + ax.legend(bbox_to_anchor=(1,1.025), loc="upper left") # Save graph - plt.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) + fig.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) file = discord.File(tmp.name, filename="image.png") # Add content to embed embed.description = f"**Countdown Channel:** <#{id}>" embed.set_image(url="attachment://image.png") elif (option == ""): - # Create plot - plt.close() + # Create figure + fig, ax = plt.subplots() # Add data to graph x = [x["author"] for x in contributors] y = [x["contributions"] for x in contributors] - plt.pie(y, labels=[await getUsername(i) for i in x], autopct="%1.1f%%", startangle = 90) + ax.pie(y, labels=[await getUsername(i) for i in x], autopct="%1.1f%%", startangle = 90) # Save graph - plt.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) + fig.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) file = discord.File(tmp.name, filename="image.png") # Add content to embed @@ -1089,20 +1088,19 @@ async def progress(ctx): # Get progress stats stats = channel["countdown"].progress() - # Create plot - plt.close() - plt.title("Countdown Progress") - plt.xlabel("Time") - plt.ylabel("Progress") - plt.gcf().autofmt_xdate() + # Create figure + fig, ax = plt.subplots() + ax.set_xlabel("Time") + ax.set_ylabel("Progress") + fig.autofmt_xdate() # Add data to graph x = [stats["start"] + timedelta(hours=channel["timezone"])] + [x["time"] + timedelta(hours=channel["timezone"]) for x in stats["progress"]] y = [0] + [x["progress"] for x in stats["progress"]] - plt.plot(x, y) + ax.plot(x, y) # Save graph - plt.savefig(tmp.name) + fig.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) file = discord.File(tmp.name, filename="image.png") # Calculate embed data @@ -1203,19 +1201,18 @@ async def speed(ctx, period="24.0"): period = timedelta(hours=period) speed = channel["countdown"].speed(period, tz=timedelta(hours=channel["timezone"])) - # Create plot - plt.close() - plt.title("Countdown Speed") - plt.xlabel("Time") - plt.ylabel("Progress per Period") - plt.gcf().autofmt_xdate() + # Create figure + fig, ax = plt.subplots() + ax.set_xlabel("Time") + ax.set_ylabel("Progress per Period") + fig.autofmt_xdate() # Add data to graph for i in range(0, len(speed[0])): - plt.bar(speed[0][i], speed[1][i], width=period, align="edge", color="#1f77b4") + ax.bar(speed[0][i], speed[1][i], width=period, align="edge", color="#1f77b4") # Save graph - plt.savefig(tmp.name) + fig.savefig(tmp.name, bbox_inches="tight", pad_inches=0.2) file = discord.File(tmp.name, filename="image.png") # Add content to embed