Skip to content

Commit 2de1155

Browse files
committed
Bump version to 0.0.5 in __init__.py and pyproject.toml, track guild join and remove events, and add guild stats tracking
1 parent b14c6c2 commit 2de1155

File tree

3 files changed

+60
-35
lines changed

3 files changed

+60
-35
lines changed

discordanalytics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = 'discordanalytics'
22
__author__ = "ValDesign"
33
__license__ = "MIT"
4-
__version__ = "0.0.4"
4+
__version__ = "0.0.5"
55

66
from .client import *

discordanalytics/client.py

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from datetime import datetime
3+
from typing import Literal
34
import discord
45
from discord.enums import InteractionType
56
import requests
@@ -44,7 +45,10 @@ def __init__(self, client: discord.Client, api_key: str, debug: bool = False):
4445
"medium": 0,
4546
"big": 0,
4647
"huge": 0
47-
}
48+
},
49+
"guildsStats": [], # {guildId:str, name:str, icon:str, members:int, interactions: int}[]
50+
"addedGuilds": 0,
51+
"removedGuilds": 0,
4852
}
4953

5054
def track_events(self):
@@ -55,8 +59,14 @@ async def on_ready():
5559
else:
5660
self.init()
5761
@self.client.event
58-
async def on_interaction(interaction):
62+
async def on_interaction(interaction: discord.Interaction):
5963
self.track_interactions(interaction)
64+
@self.client.event
65+
async def on_guild_join(guild: discord.Guild):
66+
self.trackGuilds(guild, "create")
67+
@self.client.event
68+
async def on_guild_remove(guild: discord.Guild):
69+
self.trackGuilds(guild, "delete")
6070

6171
def init(self):
6272
if not isinstance(self.client, discord.Client):
@@ -126,7 +136,10 @@ async def send_stats(self):
126136
"interactions": [],
127137
"locales": [],
128138
"guildsLocales": [],
129-
"guildMembers": self.calculate_guild_members_repartition()
139+
"guildMembers": self.calculate_guild_members_repartition(),
140+
"guildsStats": [],
141+
"addedGuilds": 0,
142+
"removedGuilds": 0,
130143
}
131144

132145
await asyncio.sleep(10 if "--dev" in sys.argv else 300)
@@ -151,7 +164,7 @@ def calculate_guild_members_repartition(self):
151164

152165
return result
153166

154-
def track_interactions(self, interaction):
167+
def track_interactions(self, interaction: discord.Interaction):
155168
if self.debug:
156169
print("[DISCORDANALYTICS] Track interactions triggered")
157170
if not self.is_ready:
@@ -160,54 +173,66 @@ def track_interactions(self, interaction):
160173
guilds = []
161174
for guild in self.client.guilds:
162175
if guild.preferred_locale is not None:
163-
found = False
164-
for g in guilds:
165-
if g["locale"] == guild.preferred_locale.value:
166-
g["number"] += 1
167-
found = True
168-
break
169-
if not found:
176+
guild_locale = next((x for x in guilds if x["locale"] == guild.preferred_locale.value), None)
177+
if guild_locale is not None:
178+
guild_locale["number"] += 1
179+
else:
170180
guilds.append({
171181
"locale": guild.preferred_locale.value,
172182
"number": 1
173183
})
174184
self.stats["guildsLocales"] = guilds
175185

176-
found = False
177-
for data in self.stats["locales"]:
178-
if data["locale"] == interaction.locale.value:
179-
data["number"] += 1
180-
found = True
181-
break
182-
if not found:
186+
locale = next((x for x in self.stats["locales"] if x["locale"] == interaction.locale.value), None)
187+
if locale is not None:
188+
locale["number"] += 1
189+
else:
183190
self.stats["locales"].append({
184191
"locale": interaction.locale.value,
185192
"number": 1
186193
})
187194

188195
if interaction.type == InteractionType.application_command or interaction.type == InteractionType.autocomplete:
189-
found = False
190-
for data in self.stats["interactions"]:
191-
if data["name"] == interaction.data["name"] and data["type"] == interaction.type.value:
192-
data["number"] += 1
193-
found = True
194-
break
195-
if not found:
196+
interaction_data = next((x for x in self.stats["interactions"] if x["name"] == interaction.data["name"] and x["type"] == interaction.type.value), None)
197+
if interaction_data is not None:
198+
interaction_data["number"] += 1
199+
else:
196200
self.stats["interactions"].append({
197201
"name": interaction.data["name"],
198202
"number": 1,
199203
"type": interaction.type.value
200204
})
201205
elif interaction.type == InteractionType.component or interaction.type == InteractionType.modal_submit:
202-
found = False
203-
for data in self.stats["interactions"]:
204-
if data["name"] == interaction.data["custom_id"] and data["type"] == interaction.type.value:
205-
data["number"] += 1
206-
found = True
207-
break
208-
if not found:
206+
interaction_data = next((x for x in self.stats["interactions"] if x["name"] == interaction.data["custom_id"] and x["type"] == interaction.type.value), None)
207+
if interaction_data is not None:
208+
interaction_data["number"] += 1
209+
else:
209210
self.stats["interactions"].append({
210211
"name": interaction.data["custom_id"],
211212
"number": 1,
212213
"type": interaction.type.value
213-
})
214+
})
215+
216+
guild_data = next((x for x in self.stats["guildsStats"] if x["guildId"] == str(interaction.guild.id)), None)
217+
if guild_data is not None:
218+
guild_data["interactions"] += 1
219+
else:
220+
self.stats["guildsStats"].append({
221+
"guildId": str(interaction.guild.id),
222+
"name": interaction.guild.name,
223+
"icon": interaction.guild.icon,
224+
"members": interaction.guild.member_count,
225+
"interactions": 1
226+
})
227+
228+
# type = delete or create
229+
def trackGuilds(self, guild: discord.Guild, type: Literal["create", "delete"]):
230+
if self.debug:
231+
print(f"[DISCORDANALYTICS] trackGuilds({type}) triggered")
232+
233+
if type == "create":
234+
self.stats["addedGuilds"] += 1
235+
elif type == "delete":
236+
self.stats["removedGuilds"] += 1
237+
else:
238+
raise ValueError(ErrorCodes.INVALID_EVENTS_COUNT)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55
[project]
66
name = "discordanalytics"
77
description = "A Python package for interacting with Discord Analytics API"
8-
version = "0.0.4"
8+
version = "0.0.5"
99
authors = [
1010
{name = "ValDesign", email = "valdesign.dev@gmail.com"}
1111
]

0 commit comments

Comments
 (0)