From b7a6f3313ba62954bac78da5c148fd657e484652 Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 9 Apr 2021 15:34:03 +0200 Subject: [PATCH] factorized help and triple-quote multi-line --- src/scanners/__init__.py | 2 +- src/scanners/channels_scanner.py | 16 +++---- src/scanners/composition_scanner.py | 12 +---- src/scanners/emotes_scanner.py | 25 +++++----- src/scanners/first_scanner.py | 5 +- src/scanners/frequency_scanner.py | 14 ++---- src/scanners/full_scanner.py | 12 +---- src/scanners/history_scanner.py | 13 ----- src/scanners/last_scanner.py | 5 +- src/scanners/mentioned_scanner.py | 20 ++++---- src/scanners/mentions_scanner.py | 21 ++++----- src/scanners/messages_scanner.py | 16 +++---- src/scanners/presence_scanner.py | 12 +---- src/scanners/random_scanner.py | 5 +- src/scanners/reactions_scanner.py | 15 +++--- src/scanners/words_scanner.py | 21 ++++----- src/utils/gdpr.py | 73 ++++++++++++++--------------- src/utils/utils.py | 32 ++++++++++--- 18 files changed, 134 insertions(+), 185 deletions(-) diff --git a/src/scanners/__init__.py b/src/scanners/__init__.py index 21fd922..ed9141d 100644 --- a/src/scanners/__init__.py +++ b/src/scanners/__init__.py @@ -11,4 +11,4 @@ from .reactions_scanner import ReactionsScanner from .first_scanner import FirstScanner from .last_scanner import LastScanner from .random_scanner import RandomScanner -from .words_scanner import WordsScanner \ No newline at end of file +from .words_scanner import WordsScanner diff --git a/src/scanners/channels_scanner.py b/src/scanners/channels_scanner.py index b3e7763..c766fb4 100644 --- a/src/scanners/channels_scanner.py +++ b/src/scanners/channels_scanner.py @@ -8,21 +8,17 @@ import discord from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter -from utils import COMMON_HELP_ARGS, mention, channel_mention +from utils import generate_help, mention, channel_mention class ChannelsScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%chan: Rank channels by their messages\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - top , default is 10\n" - + "* all/everyone - include bots\n" - + "Example: %chan 10 @user\n" - + "```" + return generate_help( + "chan", + "Rank channels by their messages", + args=[" - top , default is 10", "all/everyone - include bots"], + example="5 @user", ) def __init__(self): diff --git a/src/scanners/composition_scanner.py b/src/scanners/composition_scanner.py index daec2f7..a2f3822 100644 --- a/src/scanners/composition_scanner.py +++ b/src/scanners/composition_scanner.py @@ -8,21 +8,13 @@ import discord from .scanner import Scanner from data_types import Composition from logs import ChannelLogs, MessageLog -from utils import emojis, COMMON_HELP_ARGS +from utils import emojis, generate_help class CompositionScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%compo: Show composition statistics\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* all/everyone - include bots\n" - + "Example: %compo #mychannel1 @user\n" - + "```" - ) + return generate_help("compo", "Show composition statistics") def __init__(self): super().__init__( diff --git a/src/scanners/emotes_scanner.py b/src/scanners/emotes_scanner.py index 8c7b93f..b126812 100644 --- a/src/scanners/emotes_scanner.py +++ b/src/scanners/emotes_scanner.py @@ -8,24 +8,23 @@ import discord from logs import ChannelLogs, MessageLog from data_types import Emote, get_emote_dict from .scanner import Scanner -from utils import emojis, COMMON_HELP_ARGS, plural, precise +from utils import emojis, generate_help, plural, precise class EmotesScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%emojis: Rank emojis by their usage\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - top emojis, default is 20\n" - + "* all - list all common emojis in addition to this guild's\n" - + "* members - show top member for each emojis\n" - + "* sort:usage/reaction - other sorting methods\n" - + "* everyone - include bots\n" - + "Example: %emojis 10 all #mychannel1 #mychannel2 @user\n" - + "```" + return generate_help( + "emojis", + "Rank emojis by their usage", + args=[ + " - top emojis, default is 20", + "all - list all common emojis in addition to this guild's", + "members - show top member for each emojis", + "sort:usage/reaction - other sorting methods", + "everyone - include bots", + ], + example="10 all #mychannel1 #mychannel2 @user", ) def __init__(self): diff --git a/src/scanners/first_scanner.py b/src/scanners/first_scanner.py index 766b145..1048e2c 100644 --- a/src/scanners/first_scanner.py +++ b/src/scanners/first_scanner.py @@ -3,14 +3,13 @@ from typing import List # Custom libs from .history_scanner import HistoryScanner +from utils import generate_help class FirstScanner(HistoryScanner): @staticmethod def help() -> str: - return super(FirstScanner, FirstScanner).help( - cmd="first", text="Read first message" - ) + return generate_help("first", "Read first message") def __init__(self): super().__init__(help=FirstScanner.help()) diff --git a/src/scanners/frequency_scanner.py b/src/scanners/frequency_scanner.py index fac0a27..e0a4818 100644 --- a/src/scanners/frequency_scanner.py +++ b/src/scanners/frequency_scanner.py @@ -8,21 +8,13 @@ import discord from .scanner import Scanner from data_types import Frequency from logs import ChannelLogs, MessageLog -from utils import COMMON_HELP_ARGS +from utils import generate_help class FrequencyScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%freq: Show frequency-related statistics\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* all/everyone - include bots\n" - + "Example: %freq #mychannel1 @user\n" - + "```" - ) + return generate_help("freq", "Show frequency-related statistics") def __init__(self): super().__init__( @@ -55,7 +47,7 @@ class FrequencyScanner(Scanner): freq: Frequency, raw_members: List[int], *, - all_messages: bool + all_messages: bool, ) -> bool: impacted = False # If author is included in the selection (empty list is all) diff --git a/src/scanners/full_scanner.py b/src/scanners/full_scanner.py index 22149bd..ac5cb0e 100644 --- a/src/scanners/full_scanner.py +++ b/src/scanners/full_scanner.py @@ -8,21 +8,13 @@ from .scanner import Scanner from . import FrequencyScanner, CompositionScanner, PresenceScanner from data_types import Frequency, Composition, Presence from logs import ChannelLogs, MessageLog -from utils import COMMON_HELP_ARGS +from utils import generate_help class FullScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%scan: Show full statistics\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* all/everyone - include bots\n" - + "Example: %scan #mychannel1 @user\n" - + "```" - ) + return generate_help("scan", "Show full statistics") def __init__(self): super().__init__( diff --git a/src/scanners/history_scanner.py b/src/scanners/history_scanner.py index c61872e..5a3ae4c 100644 --- a/src/scanners/history_scanner.py +++ b/src/scanners/history_scanner.py @@ -7,22 +7,9 @@ import discord from .scanner import Scanner from data_types import History from logs import ChannelLogs, MessageLog -from utils import COMMON_HELP_ARGS class HistoryScanner(Scanner, ABC): - @staticmethod - def help(*, cmd: str, text: str) -> str: - return ( - "```\n" - + f"%{cmd}: {text}\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* all/everyone - include bots\n" - + "Example: %{cmd} #mychannel1 @user\n" - + "```" - ) - def __init__(self, *, help: str): super().__init__( has_digit_args=True, diff --git a/src/scanners/last_scanner.py b/src/scanners/last_scanner.py index 7713195..3d8cbf0 100644 --- a/src/scanners/last_scanner.py +++ b/src/scanners/last_scanner.py @@ -3,14 +3,13 @@ from typing import List # Custom libs from .history_scanner import HistoryScanner +from utils import generate_help class LastScanner(HistoryScanner): @staticmethod def help() -> str: - return super(LastScanner, LastScanner).help( - cmd="last", text="Read last message" - ) + return generate_help("last", "Read last message") def __init__(self): super().__init__(help=LastScanner.help()) diff --git a/src/scanners/mentioned_scanner.py b/src/scanners/mentioned_scanner.py index 8cf74d4..fa6c09e 100644 --- a/src/scanners/mentioned_scanner.py +++ b/src/scanners/mentioned_scanner.py @@ -8,22 +8,18 @@ import discord from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter -from utils import COMMON_HELP_ARGS, plural, precise, mention, alt_mention +from utils import generate_help, plural, precise, mention, alt_mention class MentionedScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%mentioned: Rank specific user's mentions by their usage\n" - + "arguments:\n" - + "* @member/me - (required) one or more member\n" - + "\n".join(COMMON_HELP_ARGS.split("\n")[1:]) - + "* - top mentions, default is 10\n" - + "* all - include bots mentions\n" - + "Example: %mentioned 10 @user\n" - + "```" + return generate_help( + "mentioned", + "Rank specific user's mentions by their usage", + args=[" - top , default is 10", "all/everyone - include bots"], + example="5 @user", + replace_args=[" @member/me - (required) one or more member"], ) def __init__(self): @@ -45,7 +41,7 @@ class MentionedScanner(Scanner): "You need to mention at least one member or use `me`", reference=message ) return False - self.all_mentions = "all" in args + self.all_mentions = "all" in args or "everyone" in args # Create mentions dict self.mentions = defaultdict(Counter) return True diff --git a/src/scanners/mentions_scanner.py b/src/scanners/mentions_scanner.py index 8890390..50a0f5c 100644 --- a/src/scanners/mentions_scanner.py +++ b/src/scanners/mentions_scanner.py @@ -9,7 +9,7 @@ from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter from utils import ( - COMMON_HELP_ARGS, + generate_help, plural, precise, mention, @@ -22,16 +22,15 @@ from utils import ( class MentionsScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%mentions: Rank mentions by their usage\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - top mentions, default is 10\n" - + "* all - show role/channel/everyone/here mentions\n" - + "* everyone - include bots mentions\n" - + "Example: %mentions 10 #mychannel1 #mychannel2 @user\n" - + "```" + return generate_help( + "mentions", + "Rank mentions by their usage", + args=[ + " - top , default is 10", + "all - show role/channel/everyone/here mentions", + "everyone - include bots mentions", + ], + example="10 #mychannel1 #mychannel2 @user", ) def __init__(self): diff --git a/src/scanners/messages_scanner.py b/src/scanners/messages_scanner.py index f576057..a79735e 100644 --- a/src/scanners/messages_scanner.py +++ b/src/scanners/messages_scanner.py @@ -8,21 +8,17 @@ import discord from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter -from utils import COMMON_HELP_ARGS, mention, channel_mention +from utils import generate_help, mention, channel_mention class MessagesScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%msg: Rank users by their messages\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - top , default is 10\n" - + "* all/everyone - include bots\n" - + "Example: %msg 10 #channel\n" - + "```" + return generate_help( + "msg", + "Rank users by their messages", + args=[" - top , default is 10", "all/everyone - include bots"], + example="10 #channel", ) def __init__(self): diff --git a/src/scanners/presence_scanner.py b/src/scanners/presence_scanner.py index b19e723..5e39931 100644 --- a/src/scanners/presence_scanner.py +++ b/src/scanners/presence_scanner.py @@ -7,21 +7,13 @@ import discord from .scanner import Scanner from data_types import Presence from logs import ChannelLogs, MessageLog -from utils import COMMON_HELP_ARGS +from utils import generate_help class PresenceScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%pres: Show presence statistics\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* all/everyone - include bots\n" - + "Example: %pres #mychannel1 @user\n" - + "```" - ) + return generate_help("pres", "Show presence statistics") def __init__(self): super().__init__( diff --git a/src/scanners/random_scanner.py b/src/scanners/random_scanner.py index 9ef520b..f4fb7a9 100644 --- a/src/scanners/random_scanner.py +++ b/src/scanners/random_scanner.py @@ -3,14 +3,13 @@ from typing import List # Custom libs from .history_scanner import HistoryScanner +from utils import generate_help class RandomScanner(HistoryScanner): @staticmethod def help() -> str: - return super(RandomScanner, RandomScanner).help( - cmd="rand", text="Read a random message" - ) + return generate_help("rand", "Read a random message") def __init__(self): super().__init__(help=RandomScanner.help()) diff --git a/src/scanners/reactions_scanner.py b/src/scanners/reactions_scanner.py index bb84387..3603a06 100644 --- a/src/scanners/reactions_scanner.py +++ b/src/scanners/reactions_scanner.py @@ -8,20 +8,17 @@ import discord from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter -from utils import COMMON_HELP_ARGS, mention, channel_mention +from utils import generate_help, mention, channel_mention class ReactionsScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%react: Rank users by their reactions\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - top , default is 10\n" - + "Example: %react 10 #channel\n" - + "```" + return generate_help( + "react", + "Rank users by their reactions", + args=[" - top , default is 10"], + example="10 #channel", ) def __init__(self): diff --git a/src/scanners/words_scanner.py b/src/scanners/words_scanner.py index c31bf9b..f7f6dd7 100644 --- a/src/scanners/words_scanner.py +++ b/src/scanners/words_scanner.py @@ -9,7 +9,7 @@ from logs import ChannelLogs, MessageLog from .scanner import Scanner from data_types import Counter from utils import ( - COMMON_HELP_ARGS, + generate_help, plural, precise, ) @@ -18,16 +18,15 @@ from utils import ( class WordsScanner(Scanner): @staticmethod def help() -> str: - return ( - "```\n" - + "%words: (BETA) Rank words by their usage\n" - + "arguments:\n" - + COMMON_HELP_ARGS - + "* - words containings or more letters, default is 3\n" - + "* - top words, default is 10\n" - + "* everyone - include bots\n" - + "Example: %words 5 10 #mychannel1 #mychannel2 @user\n" - + "```" + return generate_help( + "words", + "(BETA) Rank words by their usage", + args=[ + " - words containings or more letters, default is 3", + " - top words, default is 10", + "all/everyone - include bots", + ], + example="5 10 #mychannel1 #mychannel2 @user", ) def __init__(self): diff --git a/src/utils/gdpr.py b/src/utils/gdpr.py index 968582c..5ae85c6 100644 --- a/src/utils/gdpr.py +++ b/src/utils/gdpr.py @@ -3,45 +3,42 @@ import discord from logs import GuildLogs -HELP = ( - "```\n" - + "%gdpr: Displays GDPR information\n" - + "arguments:\n" - + "* agree - agree to GDPR\n" - + "* revoke - remove this server's data\n" - + "```" -) +HELP = """``` +%gdpr: Displays GDPR information +arguments: +* agree - agree to GDPR +* revoke - remove this server's data +```""" -TEXT = ( - "" - + "__**About Analyst-bot's data usage**__\n" - + "**TL;DR**\n" - + "Analyst-bot collects text message information. It does not share collected data with any third-party and data is retained 12 months or until the bot is leaving the guild/server.\n" - + "**Data collection**\n" - + "Analyst-bot collects a Discord guild/server's history when asked to.\n" - + "This includes:\n" - + "- Visible text channel names\n" - + "- Visible text messages: date and time of creation and edition, author, content, reactions and other available metadata (pinned, tts, etc.)\n" - + "This does __not__ includes:\n" - + "- Voice channels and not visible channels\n" - + "- Not visible text messages\n" - + "- Visible text messages' embedded content, images and other attachments\n" - + "**Data processing**\n" - + "Any data collected is only processed in order to produce a one-time report sent to the user immediately. No temporary data are retained.\n" - + "**Data storage and retain policy**\n" - + "Analyst-bot stores the collected data in files that are accessible by the software and its administrator only.\n" - + "Any collected data are retained maximum 12 months until deletion or when the bot is leaving a guild/server.\n" - + "**Data sharing**\n" - + "Analyst-bot does not share the data collected with any third-party.\n" - + "**Right to retract**\n" - + "If you want to have your data removed, you can use the `%gdpr revoke` command or remove this bot from your guild/server.\n" - + "**Terms agreement**\n" - + "By agreeing to these terms, you ensure having the legal age if you are in a country that does have one and you also ensure having the consent of every member involved.\n" - + "\n" - + "*If you want more information, please contact the creator of this bot: .*\n" - + "\n" - + "Type `%gdpr agree` to agree to these terms, `%gdpr revoke` to remove this guild/server's collected data or `%gdpr` to see this message again." -) +TEXT = """ +__**About Analyst-bot's data usage**__ +**TL;DR** +Analyst-bot collects text message information. It does not share collected data with any third-party and data is retained 18 months or until the bot is leaving the guild/server. +**Data collection** +Analyst-bot collects a Discord guild/server's history when asked to. +This includes: +- Visible text channel names +- Visible text messages: date and time of creation and edition, author, content, reactions and other available metadata (pinned, tts, etc.) +This does __not__ includes: +- Voice channels and not visible channels +- Not visible text messages +- Visible text messages' embedded content, images and other attachments +**Data processing** +Any data collected is only processed in order to produce a one-time report sent to the user immediately. No temporary data are retained. +**Data storage and retain policy** +Analyst-bot stores the collected data in files that are accessible by the software and its administrator only. +Any collected data are retained maximum 18 months until deletion or when the bot is leaving a guild/server. +**Data sharing** +Analyst-bot does not share the data collected with any third-party. +**Right to retract** +If you want to have your data removed, you can use the `%gdpr revoke` command or remove this bot from your guild/server. +**Terms agreement** +By agreeing to these terms, you ensure having the legal age if you are in a country that does have one and you also ensure having the consent of every member involved. + +*If you want more information, please contact the creator of this bot: .* + +Type `%gdpr agree` to agree to these terms, `%gdpr revoke` to remove this guild/server's collected data or `%gdpr` to see this message again. +""" AGREE_TEXT = "Thanks for agreeing for these terms, you can now run analysis on this guild/server." diff --git a/src/utils/utils.py b/src/utils/utils.py index 880d892..1447a40 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -7,13 +7,31 @@ from datetime import datetime # OTHER -COMMON_HELP_ARGS = ( - "" - + "* @member/me - filter for one or more member\n" - + "* #channel/here - filter for one or more channel\n" - + "* fast - only read cache\n" - + "* fresh - does not read cache (long)\n" -) +COMMON_HELP_ARGS = [ + "@member/me - filter for one or more member", + "#channel/here - filter for one or more channel", + "fast - only read cache", + "fresh - does not read cache (long)", +] + + +def generate_help( + cmd: str, + info: str, + *, + args=["all/everyone - include bots"], + example="#mychannel1 @user", + replace_args=[], +): + arg_list = "* " + "\n* ".join( + replace_args + COMMON_HELP_ARGS[len(replace_args) :] + args + ) + return f"""``` +%{cmd}: {info} +arguments: +{arg_list} +Example: %{cmd} {example} +```""" def delta(t0: datetime):