diff --git a/bot/modules/guild_admin/__init__.py b/bot/modules/guild_admin/__init__.py index 8a83dc3e..9f66e5c4 100644 --- a/bot/modules/guild_admin/__init__.py +++ b/bot/modules/guild_admin/__init__.py @@ -1,3 +1,4 @@ from .module import module from . import guild_config +from . import statreset diff --git a/bot/modules/guild_admin/statreset.py b/bot/modules/guild_admin/statreset.py new file mode 100644 index 00000000..d578f5fb --- /dev/null +++ b/bot/modules/guild_admin/statreset.py @@ -0,0 +1,65 @@ +from io import StringIO + +import discord +from wards import guild_admin +from data import tables +from core import Lion + +from .module import module + + +@module.cmd("statreset", + desc="Perform a complete reset of the server's study statistics.", + group="Guild Admin") +@guild_admin() +async def cmd_statreset(ctx): + """ + Usage``: + {prefix}statreset + Description: + Perform a complete reset of the server's member statistics. + This includes tracked time, coins, and workout counts. + + This may be used to set "seasons" of study. + + Before the reset, I will send a csv file with the previous member statistics. + + **This is not reversible.** + """ + if not await ctx.ask("Are you sure you want to completely reset the member statistics? " + "**THIS IS NOT REVERSIBLE!**"): + return + # Build the data csv + rows = tables.lions.select_where( + select_columns=('userid', 'tracked_time', 'coins', 'workout_count', 'b.roleid AS badge_roleid'), + _extra=( + "LEFT JOIN study_badges b ON last_study_badgeid = b.badgeid " + "WHERE members.guildid={}" + ).format(ctx.guild.id) + ) + header = "userid, tracked_time, coins, workouts, rank_roleid\n" + csv_rows = [ + ', '.join(str(data) for data in row) + for row in rows + ] + + with StringIO() as stats_file: + stats_file.write(header) + stats_file.write('\n'.join(csv_rows)) + stats_file.seek(0) + + out_file = discord.File(stats_file, filename="member_statistics.csv") + await ctx.reply(file=out_file) + + # Reset the statistics + tables.lions.update_where( + {'tracked_time': 0, 'coins': 0, 'workout_count': 0}, + guildid=ctx.guild.id + ) + + Lion.sync() + + await ctx.embed_reply( + "The server member statistics have been reset!\n" + "(It may take a while for the member studybadges to update.)" + ) diff --git a/bot/modules/meta/help.py b/bot/modules/meta/help.py index 6600dc1d..ce71efa6 100644 --- a/bot/modules/meta/help.py +++ b/bot/modules/meta/help.py @@ -14,6 +14,7 @@ group_hints = { 'Statistics': "*StudyLion leaderboards and study statistics.*", 'Economy': "*Buy, sell, and trade with your hard-earned coins!*", 'Personal Settings': "*Tell me about yourself!*", + 'Guild Admin': "*Dangerous administration commands!*", 'Guild Configuration': "*Control how I behave in your server.*", 'Meta': "*Information about me!*" } @@ -28,7 +29,7 @@ mod_group_order = ( ) admin_group_order = ( - ('Guild Configuration', 'Moderation', 'Meta'), + ('Guild Admin', 'Guild Configuration', 'Moderation', 'Meta'), ('Productivity', 'Statistics', 'Economy', 'Personal Settings') )