1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add Discord Activity

This commit is contained in:
Steffo 2018-11-18 17:38:02 +01:00
parent 601281fe33
commit a6bae4b91b
5 changed files with 139 additions and 25 deletions

16
db.py
View file

@ -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"<ActivityReport at {self.timestamp.isoformat()}>"
# If run as script, create all the tables in the db
if __name__ == "__main__":
print("Creating new tables...")

View file

@ -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}.")

64
templates/activity.html Normal file
View file

@ -0,0 +1,64 @@
{% extends 'base.html' %}
{% block prehead %}
<meta name="description" content="Statistiche sull'attività della Royal Games">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.js" integrity="sha384-DhLzB4p1BEBSNugP/gw0UBLxHx7kHgldDGYtGinCssfp4HK+sG4ByZDau4pWmpqG" crossorigin="anonymous"></script>
{% endblock %}
{% block pagetitle %}
{{ game_name }}
{% endblock %}
{% block body %}
<div class="discord-activity">
<h1>
Attività su Discord
</h1>
<canvas class="members-graph-7d" id="discord-members-graph-7d"></canvas>
<script>
new Chart("discord-members-graph-7d",
{
"type": "line",
"data": {
"labels": [
{% for point in activityreports %}
{{ point.timestamp.strftime("%a %H:%M") }}
{% endfor %}
],
"datasets": [
{
"label": "Online",
"borderColor": "#6dcff6",
"data": [
{% for point in activityreports %}
{{ point.discord_members_online }},
{% endfor %}
]
},
{
"label": "In game",
"borderColor": "#9ae915",
"data": [
{% for point in activityreports %}
{{ point.discord_members_ingame }},
{% endfor %}
]
},
{
"label": "In cv",
"borderColor": "#fe7f00",
"data": [
{% for point in activityreports %}
{{ point.discord_members_cv }},
{% endfor %}
]
},
]
},
"options":{
}
});
</script>
</div>
{% endblock %}

View file

@ -17,14 +17,6 @@
<img src="{{ url_for('static', filename='FixMeRYGLogo.jpg') }}" class="ryg-logo">
<b>Royalnet</b>
<a href="/">Home</a>
<span class="desktop-only">
{% if session.get('username') is not none %}
<a href="{{ g.rygconf['Telegram']['invite_link'] }}">Telegram</a>
<a href="{{ g.rygconf['Discord']['invite_link'] }}">Discord</a>
<a href="https://steamcommunity.com/groups/royalgamescastle">Steam</a>
<a href="https://new.reddit.com/r/RoyalGames/">/r/RoyalGames</a>
{% endif %}
</span>
</div>
<div class="right">
{% if config["DEBUG"] %}

View file

@ -8,21 +8,6 @@
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
{% endblock %}
{% block posthead %}
<script>
function timer() {
let now = new Date().getTime();
let to = new Date("Oct 31, 2018 15:30:00").getTime();
let result = to - now;
document.getElementById("time-left").innerHTML = Math.floor(String((result / 3600000))) + "h "
+ Math.floor(String((result / 60000) % 60)) + "m "
+ Math.floor(String((result / 1000) % 60)) + "s";
}
setInterval(timer, 100);
</script>
{% endblock %}
{% block body %}
<h1 id="main-title">
Royal Games
@ -49,9 +34,24 @@
<a href="/diario">Visualizza tutto</a>
</div>
</div>
</div>
<div class="right">
<div class="box">
<div class="upper-box">
Link utili
</div>
<div class="lower-box">
<ul>
<li><a href="{{ g.rygconf['Telegram']['invite_link'] }}">Link di unione a Telegram</a></li>
<li><a href="{{ g.rygconf['Discord']['invite_link'] }}">Link di invito a Discord</a></li>
<li><a href="https://steamcommunity.com/groups/royalgamescastle">Gruppo Steam Community</a></li>
<li><a href="https://new.reddit.com/r/RoyalGames/">/r/RoyalGames</a></li>
<li><a href="/music">Statistiche su Royal Music</a></li>
<li><a href="http://amazon.steffo.eu/royal-music-cache/">File in cache di Royal Music</a></li>
<li><a href="/activity">Statistiche sull'attività</a></li>
</ul>
</div>
</div>
<div class="box">
<div class="upper-box">
Membri
@ -76,7 +76,6 @@
<li><a href="/game/discord">Discord</a></li>
<li><a href="/game/steam">Steam</a></li>
<li><a href="/game/dota">Dota 2</a></li>
<!--li><a href="/game/rl">Rocket League</a></li-->
<li><a href="/game/lol">League of Legends</a></li>
<li><a href="/game/ow">Overwatch</a></li>
<li><a href="/game/osu">osu!</a></li>