Added the ability to get server/member count across all shards
This commit is contained in:
parent
d0f81a3f1a
commit
d8b2fcb3a2
3 changed files with 26 additions and 14 deletions
17
cogs/core.py
17
cogs/core.py
|
@ -63,19 +63,22 @@ class Core:
|
||||||
# The only real use of doing it this way is easier editing if the info in this command is changed
|
# The only real use of doing it this way is easier editing if the info in this command is changed
|
||||||
fmt = {}
|
fmt = {}
|
||||||
|
|
||||||
all_members = list(self.bot.get_all_members())
|
bot_data = config.get_content('bot_data')
|
||||||
|
total_data = {}
|
||||||
|
for shard, values in bot_data.items():
|
||||||
|
for key, value in values.items():
|
||||||
|
if key in total_data:
|
||||||
|
total_data[key] += value
|
||||||
|
else:
|
||||||
|
total_data[key] = value
|
||||||
|
|
||||||
# We can pretty safely assume that the author is going to be in at least one channel with the bot
|
# We can pretty safely assume that the author is going to be in at least one channel with the bot
|
||||||
# So find the author based on that list
|
# So find the author based on that list
|
||||||
authors = []
|
|
||||||
for author_id in config.owner_ids:
|
|
||||||
authors.append(discord.utils.get(all_members, id=author_id).name)
|
|
||||||
|
|
||||||
fmt['Official Bot Server'] = config.dev_server
|
fmt['Official Bot Server'] = config.dev_server
|
||||||
fmt['Author'] = ", ".join(authors)
|
|
||||||
fmt['Uptime'] = (pendulum.utcnow() - self.bot.uptime).in_words()
|
fmt['Uptime'] = (pendulum.utcnow() - self.bot.uptime).in_words()
|
||||||
fmt['Total Servers'] = len(self.bot.servers)
|
fmt['Total Servers'] = total_data.get('server_count')
|
||||||
fmt['Total Members'] = len(all_members)
|
fmt['Total Members'] = total_data.get('member_count')
|
||||||
fmt['Description'] = self.bot.description
|
fmt['Description'] = self.bot.description
|
||||||
|
|
||||||
servers_playing_music = len([server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if
|
servers_playing_music = len([server_id for server_id, state in self.bot.get_cog('Music').voice_states.items() if
|
||||||
|
|
|
@ -44,12 +44,27 @@ class StatsUpdate:
|
||||||
|
|
||||||
async def on_server_join(self, server):
|
async def on_server_join(self, server):
|
||||||
await self.update()
|
await self.update()
|
||||||
|
data = await config.get_content('bot_data')
|
||||||
|
shard_data = data.get('shard_{}'.format(config.shard_id))
|
||||||
|
shard_data['server_count'] = len(self.bot.servers)
|
||||||
|
shard_data['member_count'] = len(list(self.bot.get_all_members()))
|
||||||
|
await config.save_content('bot_data')
|
||||||
|
|
||||||
async def on_server_leave(self, server):
|
async def on_server_leave(self, server):
|
||||||
await self.update()
|
await self.update()
|
||||||
|
data = await config.get_content('bot_data')
|
||||||
|
shard_data = data.get('shard_{}'.format(config.shard_id))
|
||||||
|
shard_data['server_count'] = len(self.bot.servers)
|
||||||
|
shard_data['member_count'] = len(list(self.bot.get_all_members()))
|
||||||
|
await config.save_content('bot_data')
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
await self.update()
|
await self.update()
|
||||||
|
data = await config.get_content('bot_data')
|
||||||
|
shard_data = data.get('shard_{}'.format(config.shard_id))
|
||||||
|
shard_data['server_count'] = len(self.bot.servers)
|
||||||
|
shard_data['member_count'] = len(list(self.bot.get_all_members()))
|
||||||
|
await config.save_content('bot_data')
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
|
|
@ -93,12 +93,6 @@ sharded_data = {}
|
||||||
# cache[k] = Cache(k)
|
# cache[k] = Cache(k)
|
||||||
|
|
||||||
|
|
||||||
def get_bot_data(key):
|
|
||||||
# This method will handle data that we'd like to get across all shards
|
|
||||||
# First get the bot's data from cache or the database
|
|
||||||
bot_data = get_content('bot_data')
|
|
||||||
|
|
||||||
|
|
||||||
def command_prefix(bot, message):
|
def command_prefix(bot, message):
|
||||||
# We do not want to make a query for every message that is sent
|
# We do not want to make a query for every message that is sent
|
||||||
# So assume it's in cache, or it doesn't exist
|
# So assume it's in cache, or it doesn't exist
|
||||||
|
@ -138,7 +132,7 @@ async def save_content(table: str, content):
|
||||||
|
|
||||||
async def disabled_get_content(key: str):
|
async def disabled_get_content(key: str):
|
||||||
cached = cache.get('key')
|
cached = cache.get('key')
|
||||||
if cached is None:
|
if cached is None or len(cached) == 0:
|
||||||
value = await _get_content(key)
|
value = await _get_content(key)
|
||||||
# If we found this object not cached, cache it
|
# If we found this object not cached, cache it
|
||||||
cache['key'] = value
|
cache['key'] = value
|
||||||
|
|
Loading…
Reference in a new issue