From a6bae4b91b111904272f02e3a1c960dbd501bc87 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 18 Nov 2018 17:38:02 +0100 Subject: [PATCH] Add Discord Activity --- db.py | 16 +++++++++++ discordbot.py | 43 +++++++++++++++++++++++++++ templates/activity.html | 64 +++++++++++++++++++++++++++++++++++++++++ templates/base.html | 8 ------ templates/main.html | 33 +++++++++++---------- 5 files changed, 139 insertions(+), 25 deletions(-) create mode 100644 templates/activity.html diff --git a/db.py b/db.py index bfb4b4a8..5d2a65b8 100644 --- a/db.py +++ b/db.py @@ -967,6 +967,22 @@ class Halloween(Base): completed[i] = True return started, completed + +class ActivityReport(Base): + __tablename__ = "activityreports" + + timestamp = Column(DateTime, primary_key=True) + + discord_members_online = Column(Integer) + discord_members_ingame = Column(Integer) + discord_cv = Column(Integer) + discord_members_cv = Column(Integer) + discord_channels_used = Column(Integer) + + def __repr__(self): + return f"" + + # If run as script, create all the tables in the db if __name__ == "__main__": print("Creating new tables...") diff --git a/discordbot.py b/discordbot.py index 2e5e64d5..e32b510e 100644 --- a/discordbot.py +++ b/discordbot.py @@ -336,6 +336,7 @@ class RoyalDiscordBot(discord.Client): asyncio.ensure_future(self.queue_predownload_videos()) asyncio.ensure_future(self.queue_play_next_video()) asyncio.ensure_future(self.inactivity_countdown()) + asyncio.ensure_future(self.activity_task()) async def on_ready(self): # Get the main channel @@ -597,6 +598,48 @@ class RoyalDiscordBot(discord.Client): await self.change_presence(status=discord.Status.online, activity=None) await self.main_channel.send("💤 Mi sono disconnesso dalla cv per inattività.") + async def create_activityreport(self): + logger.debug("Fetching Discord users...") + discord_users = list(self.main_guild.members) + online_members_count = 0 + ingame_members_count = 0 + cv_count = 0 + cv_members_count = 0 + non_empty_channels = [] + for member in discord_users: + if member.bot: + continue + if member.voice is not None: + cv_count += 1 + if member.voice.channel.id not in non_empty_channels: + non_empty_channels.append(member.voice.channel.id) + if len(member.roles) >= 2: + if member.voice is not None: + cv_members_count += 1 + if member.status != discord.Status.offline and member.status != discord.Status.idle: + online_members_count += 1 + if member.activity is not None and member.activity.type == discord.ActivityType.playing: + ingame_members_count += 1 + logger.debug("Creating and committing db.ActivityReport...") + session = db.Session() + activityreport = db.ActivityReport(timestamp=datetime.datetime.now(), + discord_members_online=online_members_count, + discord_members_ingame=ingame_members_count, + discord_cv=cv_count, + discord_members_cv=cv_members_count, + discord_channels_used=len(non_empty_channels)) + session.add(activityreport) + await loop.run_in_executor(executor, session.commit) + await loop.run_in_executor(executor, session.close) + logger.info("ActivityReport created.") + + async def activity_task(self): + time_to_wait = config["Discord"]["activityreport_sample_time"] + while True: + await self.create_activityreport() + logger.debug(f"Waiting {time_to_wait} seconds before the next record.") + await asyncio.sleep(time_to_wait) + async def add_video_from_url(self, url, index: typing.Optional[int] = None, enqueuer: discord.Member = None): # Retrieve info logger.debug(f"Retrieving info for {url}.") diff --git a/templates/activity.html b/templates/activity.html new file mode 100644 index 00000000..c374522c --- /dev/null +++ b/templates/activity.html @@ -0,0 +1,64 @@ +{% extends 'base.html' %} + +{% block prehead %} + + +{% endblock %} + +{% block pagetitle %} + {{ game_name }} +{% endblock %} + +{% block body %} +
+

+ Attività su Discord +

+ + +
+{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index c96578fc..8dd48f50 100644 --- a/templates/base.html +++ b/templates/base.html @@ -17,14 +17,6 @@ Royalnet Home - - {% if session.get('username') is not none %} - Telegram - Discord - Steam - /r/RoyalGames - {% endif %} -
{% if config["DEBUG"] %} diff --git a/templates/main.html b/templates/main.html index de9ab5ae..aa0e8903 100644 --- a/templates/main.html +++ b/templates/main.html @@ -8,21 +8,6 @@ {% endblock %} -{% block posthead %} - -{% endblock %} - {% block body %}

Royal Games @@ -49,9 +34,24 @@ Visualizza tutto

-