refactoring
This commit is contained in:
@@ -1,14 +1,12 @@
|
|||||||
from typing import Dict, List, Tuple, Optional, Union
|
from typing import Dict, List, Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import discord
|
import discord
|
||||||
import re
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
|
|
||||||
# Custom libs
|
# Custom libs
|
||||||
|
|
||||||
from utils import aggregate, no_duplicate
|
from utils import no_duplicate, mention, plural, day_interval, get_intro
|
||||||
from log_manager import GuildLogs, ChannelLogs
|
from log_manager import GuildLogs, ChannelLogs
|
||||||
import emojis
|
import emojis
|
||||||
|
|
||||||
@@ -99,10 +97,18 @@ async def compute(client: discord.client, message: discord.Message, *args: str):
|
|||||||
)
|
)
|
||||||
msg_count += count
|
msg_count += count
|
||||||
chan_count += 1 if count > 0 else 0
|
chan_count += 1 if count > 0 else 0
|
||||||
await progress.edit(content=f"```Computing results...```")
|
await progress.edit(content="```Computing results...```")
|
||||||
# Display results
|
# Display results
|
||||||
await tell_results(
|
await tell_results(
|
||||||
get_intro(emotes, full, channels, members, msg_count, chan_count),
|
get_intro(
|
||||||
|
"emotes usage",
|
||||||
|
emotes,
|
||||||
|
full,
|
||||||
|
channels,
|
||||||
|
members,
|
||||||
|
msg_count,
|
||||||
|
chan_count,
|
||||||
|
),
|
||||||
emotes,
|
emotes,
|
||||||
message.channel,
|
message.channel,
|
||||||
total_msg,
|
total_msg,
|
||||||
@@ -176,25 +182,16 @@ class Emote:
|
|||||||
output += f" {name} - "
|
output += f" {name} - "
|
||||||
if not self.used():
|
if not self.used():
|
||||||
output += "never used "
|
output += "never used "
|
||||||
elif self.usages == 1:
|
|
||||||
output += "1 time "
|
|
||||||
else:
|
else:
|
||||||
output += f"{self.usages:,} times "
|
output += f"{plural(self.usages, 'time')} "
|
||||||
if self.reactions == 1:
|
if self.reactions >= 1:
|
||||||
output += "and 1 reaction "
|
output += f"and {plural(self.usages, 'reaction')} "
|
||||||
elif self.reactions > 1:
|
|
||||||
output += f"and {self.reactions:,} reactions "
|
|
||||||
if show_life and not self.default:
|
if show_life and not self.default:
|
||||||
output += f"(in {self.life_days()} days) "
|
output += f"(in {plural(self.life_days(), 'day')}) "
|
||||||
if self.used():
|
if self.used():
|
||||||
if self.use_days() == 0:
|
output += f"(last used {day_interval(self.use_days())})"
|
||||||
output += "(last used today)"
|
|
||||||
elif self.use_days() == 1:
|
|
||||||
output += "(last used yesterday)"
|
|
||||||
else:
|
|
||||||
output += f"(last used {self.use_days()} days ago)"
|
|
||||||
if show_members:
|
if show_members:
|
||||||
output += f" (mostly by <@{self.get_top_member()}>: {self.members[self.get_top_member()]})"
|
output += f" (mostly by {mention(self.get_top_member())}: {self.members[self.get_top_member()]})"
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@@ -273,54 +270,6 @@ async def tell_results(
|
|||||||
await channel.send(response)
|
await channel.send(response)
|
||||||
|
|
||||||
|
|
||||||
def get_intro(
|
|
||||||
emotes: Dict[str, Emote],
|
|
||||||
full: bool,
|
|
||||||
channels: List[discord.TextChannel],
|
|
||||||
members: List[discord.Member],
|
|
||||||
nmm: int, # number of messages impacted
|
|
||||||
nc: int, # number of impacted channels
|
|
||||||
) -> str:
|
|
||||||
"""
|
|
||||||
Get the introduction sentence of the response
|
|
||||||
"""
|
|
||||||
# Show all data (members, channels) when it's less than 5 units
|
|
||||||
if len(members) == 0:
|
|
||||||
# Full scan of the server
|
|
||||||
if full:
|
|
||||||
return f"{len(emotes)} emotes in this server ({nc} channels, {nmm:,} messages):"
|
|
||||||
elif len(channels) < 5:
|
|
||||||
return f"{aggregate([c.mention for c in channels])} emotes usage in {nmm:,} messages:"
|
|
||||||
else:
|
|
||||||
return f"These {len(channels)} channels emotes usage in {nmm:,} messages:"
|
|
||||||
elif len(members) < 5:
|
|
||||||
if full:
|
|
||||||
return f"{aggregate([m.mention for m in members])} emotes usage in {nmm:,} messages:"
|
|
||||||
elif len(channels) < 5:
|
|
||||||
return (
|
|
||||||
f"{aggregate([m.mention for m in members])} on {aggregate([c.mention for c in channels])} "
|
|
||||||
f"emotes usage in {nmm:,} messages:"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return (
|
|
||||||
f"{aggregate([m.mention for m in members])} on these {len(channels)} channels "
|
|
||||||
f"emotes usage in {nmm:,} messages:"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
if full:
|
|
||||||
return f"These {len(members)} members emotes usage in {nmm:,} messages:"
|
|
||||||
elif len(channels) < 5:
|
|
||||||
return (
|
|
||||||
f"These {len(members)} members on {aggregate([c.mention for c in channels])} "
|
|
||||||
f"emotes usage in {nmm:,} messages:"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return (
|
|
||||||
f"These {len(members)} members on these {len(channels)} channels "
|
|
||||||
f"emotes usage in {nmm:,} messages:"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_total(emotes: Dict[str, Emote], nmm: int) -> str:
|
def get_total(emotes: Dict[str, Emote], nmm: int) -> str:
|
||||||
"""
|
"""
|
||||||
Get the total of all emotes used
|
Get the total of all emotes used
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from typing import List
|
from typing import List, Dict
|
||||||
import logging
|
import logging
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
@@ -13,6 +13,10 @@ async def code_message(message: discord.Message, content: str):
|
|||||||
await message.edit(content=f"```\n{content}\n```")
|
await message.edit(content=f"```\n{content}\n```")
|
||||||
|
|
||||||
|
|
||||||
|
def mention(member_id: int) -> str:
|
||||||
|
return f"<@{member_id}>"
|
||||||
|
|
||||||
|
|
||||||
# LISTS
|
# LISTS
|
||||||
|
|
||||||
|
|
||||||
@@ -43,3 +47,68 @@ def aggregate(names: List[str]) -> str:
|
|||||||
return names[0]
|
return names[0]
|
||||||
else:
|
else:
|
||||||
return ", ".join(names[:-1]) + " & " + names[-1]
|
return ", ".join(names[:-1]) + " & " + names[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def plural(count: int, word: str) -> str:
|
||||||
|
return str(count) + " " + word + "s" if count == 1 else ""
|
||||||
|
|
||||||
|
|
||||||
|
def day_interval(interval: int) -> str:
|
||||||
|
if interval == 0:
|
||||||
|
return "today"
|
||||||
|
elif interval == 1:
|
||||||
|
return "yesterday"
|
||||||
|
else:
|
||||||
|
return f"{interval} days ago"
|
||||||
|
|
||||||
|
|
||||||
|
# APP SPECIFIC
|
||||||
|
|
||||||
|
|
||||||
|
def get_intro(
|
||||||
|
subject: str,
|
||||||
|
emotes: Dict[str],
|
||||||
|
full: bool,
|
||||||
|
channels: List[discord.TextChannel],
|
||||||
|
members: List[discord.Member],
|
||||||
|
nmm: int, # number of messages impacted
|
||||||
|
nc: int, # number of impacted channels
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Get the introduction sentence of the response
|
||||||
|
"""
|
||||||
|
# Show all data (members, channels) when it's less than 5 units
|
||||||
|
if len(members) == 0:
|
||||||
|
# Full scan of the server
|
||||||
|
if full:
|
||||||
|
return f"{subject} in this server ({nc} channels, {nmm:,} messages):"
|
||||||
|
elif len(channels) < 5:
|
||||||
|
return f"{aggregate([c.mention for c in channels])} {subject} in {nmm:,} messages:"
|
||||||
|
else:
|
||||||
|
return f"These {len(channels)} channels {subject} in {nmm:,} messages:"
|
||||||
|
elif len(members) < 5:
|
||||||
|
if full:
|
||||||
|
return f"{aggregate([m.mention for m in members])} {subject} in {nmm:,} messages:"
|
||||||
|
elif len(channels) < 5:
|
||||||
|
return (
|
||||||
|
f"{aggregate([m.mention for m in members])} on {aggregate([c.mention for c in channels])} "
|
||||||
|
f"{subject} in {nmm:,} messages:"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
f"{aggregate([m.mention for m in members])} on these {len(channels)} channels "
|
||||||
|
f"{subject} in {nmm:,} messages:"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if full:
|
||||||
|
return f"These {len(members)} members {subject} in {nmm:,} messages:"
|
||||||
|
elif len(channels) < 5:
|
||||||
|
return (
|
||||||
|
f"These {len(members)} members on {aggregate([c.mention for c in channels])} "
|
||||||
|
f"{subject} in {nmm:,} messages:"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
f"These {len(members)} members on these {len(channels)} channels "
|
||||||
|
f"{subject} in {nmm:,} messages:"
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user