diff --git a/.trunk/.gitignore b/.trunk/.gitignore index 8130ba6..695b519 100644 --- a/.trunk/.gitignore +++ b/.trunk/.gitignore @@ -5,4 +5,4 @@ plugins user_trunk.yaml user.yaml -shims +tools diff --git a/.trunk/configs/.markdownlint.yaml b/.trunk/configs/.markdownlint.yaml new file mode 100644 index 0000000..fb94039 --- /dev/null +++ b/.trunk/configs/.markdownlint.yaml @@ -0,0 +1,10 @@ +# Autoformatter friendly markdownlint config (all formatting rules disabled) +default: true +blank_lines: false +bullet: false +html: false +indentation: false +line_length: false +spaces: false +url: false +whitespace: false diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 79fe373..23ac0b2 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -1,13 +1,17 @@ version: 0.1 cli: - version: 1.11.1 + version: 1.12.1 plugins: sources: - id: trunk - ref: v0.0.20 + ref: v0.0.22 uri: https://github.com/trunk-io/plugins lint: enabled: + - actionlint@1.6.25 + - markdownlint@0.35.0 + - trivy@0.43.1 + - trufflehog@3.44.0 - pylint - codespell - cspell @@ -31,7 +35,7 @@ runtimes: enabled: - go@1.19.5 - node@18.12.1 - - python@3.10.6 + - python@3.10.8 actions: disabled: - trunk-announce diff --git a/bot.py b/bot.py index d75113f..d067a5b 100644 --- a/bot.py +++ b/bot.py @@ -68,6 +68,11 @@ MEMORY_FIVE_MUZYKA = "" SETTINGS_FILE = "" ENCODING = "" GRAPHICS_PATH = "" +MUZYKA_MOJEGO_LUDU_HISTORIA = 1500 +MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 30 +MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30 + + # *=========================================== Platform Specific Predefines if platform in ("linux", "linux2"): @@ -187,7 +192,6 @@ async def on_ready(): logger.debug("%s has connected to Discord!", client.user) await client.change_presence(activity=discord.Game(name="Axe Throwing Darts")) await client.tree.sync() - # await tree.sync(guild=discord.Object(id=Your guild id)) await check() @@ -756,10 +760,42 @@ async def async_iterator_generator(range_of_iterable): """ +async def get_stats(ctx, history_limit): + """ + The `get_stats` function retrieves the message history of a specific channel and counts the + frequency of each word in the messages. + + :param ctx: The `ctx` parameter is an object that represents the context of the command being + executed. It contains information such as the message, the author, the server, and other relevant + details + :param history_limit: The `history_limit` parameter is the maximum number of messages to retrieve + from the channel history. It determines how far back in time the statistics will be calculated + :return: The function `get_stats` returns a dictionary `stats` that contains the frequency count of + words found in the messages from the specified channel. + """ + channel_id = 1062047367337095268 + async with ctx.typing(): + stats = {} + channel = client.get_channel(channel_id) + messages = [message async for message in channel.history(limit=history_limit)] + # traverse the iterable of awaitables + for message in messages: + # await and get the result from the awaitable + result = message.content + words = result.split() + for word in words: + if word in stats: + stats[word] += 1 + else: + stats[word] = 1 + for name, how_many in stats.items(): + yield name, how_many + + # trunk-ignore(pylint/R0914) # trunk-ignore(pylint/R0915) # trunk-ignore(pylint/R0912) -async def wyszukaj(ctx, how_many=0): +async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None): """ Take in a context object and an optional integer parameter "how_many" and perform search operation on music library. @@ -773,7 +809,10 @@ async def wyszukaj(ctx, how_many=0): """ # TODO: Potem dorobić wyszukiwania po kawałkach słów # i sieć neuronową z możliwością wyrażenia opinii o dopasowaniu. - word_list = ctx.message.content.split() + if slowa_kluczowe: + word_list = slowa_kluczowe + else: + word_list = ctx.message.content.split() logger.info("Wyszukuje") search_weight = [] for _ in range(len(music_file_list)): @@ -1489,7 +1528,13 @@ async def get_image_stable_diffusion(ctx): pass -# @client.hybrid_command() +# trunk-ignore(mypy/arg-type) +@client.hybrid_command( + name="zagraj_muzyke_mego_ludu", + description="Gra muzyke wyszukana na bazie tematow konwersacji na kanale NZ", + guild=discord.Object(id=664789470779932693), +) +# trunk-ignore(pylint/R0912) async def zagraj_muzyke_mego_ludu(ctx): """ Play completeley random playlist based on messaging history. @@ -1499,9 +1544,93 @@ async def zagraj_muzyke_mego_ludu(ctx): channel, the author, and other relevant details. In this case, it is being passed as a parameter to the `get_image """ + stats = {} + if ctx: + stat_iter = get_stats(ctx, MUZYKA_MOJEGO_LUDU_HISTORIA) + async for name, how_many in stat_iter: + if len(name) > 3: + stats[name] = how_many + + sorted_stats = sorted(stats.items(), key=lambda x: x[1]) + key_words = [] + for stat in sorted_stats: + key_words.append(stat[0]) + logger.info(sorted_stats[:30]) + logger.info(key_words[:30]) + reply = "Wygenerowana plejlista:\n" + async with ctx.typing(): + logger.info("Zaczynam szukać timestamp %s", datetime.now()) + search_time_glob = datetime.now() + file = await wyszukaj(ctx=ctx, how_many=MUZYKA_MOJEGO_LUDU_PLAJLISTA, slowa_kluczowe=key_words[:MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE]) + logger.info( + "Koniec szukania(timestamp %s zajęło %s", + datetime.now(), + datetime.now() - search_time_glob, + ) + index = 1 + if file: + for plik in file: + global MUZYKA # pylint: disable=global-variable-not-assigned + MUZYKA["queue"].insert(0, plik[1]) + MUZYKA["requestor"].insert(0, ctx.author) + metadata = eyed3.load(plik[1]) + if metadata and metadata.tag: + if metadata.tag.title: + reply += f"{index} {metadata.tag.title}" + else: + reply += f"{index}. {plik[1]} z prywatnej kolekcji Hammera." + + if metadata.tag.artist: + reply += f" wykonawcy {metadata.tag.artist}" + if metadata.tag.album: + reply += f" z albumu {metadata.tag.album}" + if metadata.tag.title: + reply += " z prywatnej kolekcji Hammera i na Twoją specjalną rpośbę.\n" + else: + reply += f"{index}. {plik[1]} z prywatnej kolekcji Hammera." + + index += 1 + if len(reply) > 1800: + await ctx.send(reply) + reply = "" + else: + await ctx.send( + "Obawiam się że nie mogę nic znaleźć u siebie. Spróbuj komendy '$dej_co_ze_spotifaja' jeśli masz link" + ) + await ctx.send(reply) + logger.info("Plejlista zrobiona") + + +# trunk-ignore(mypy/arg-type) +@client.hybrid_command( + name="parametry_muzyki_mego_ludu", + description="Konfiguruje komende zagraj muzyke mojego ludu", + guild=discord.Object(id=664789470779932693), +) +async def parametry_muzyki_mego_ludu(ctx, ile_historii=1500, ile_slow_kluczowych=30, jak_dluga_plejlista=30): + """ + The function `parametry_muzyki_mego_ludu` sets global variables for the number of history entries, + number of keywords, and length of playlist for a music application. + + :param ctx: The `ctx` parameter is typically used in Discord.py, a Python library for creating + Discord bots. It represents the context of the command being executed, including information about + the message, the server, and the user who invoked the command + :param ile_historii: The parameter "ile_historii" represents the number of history items for the + music playlist, defaults to 1500 (optional) + :param ile_slow_kluczowych: The parameter "ile_slow_kluczowych" represents the number of keywords or + key phrases related to music that you want to include in your analysis or search, defaults to 30 + (optional) + :param jak_dluga_plejlista: The parameter "jak_dluga_plejlista" determines the length of the + playlist. It specifies how many songs should be included in the playlist, defaults to 30 (optional) + """ if ctx: - pass + global MUZYKA_MOJEGO_LUDU_HISTORIA # pylint: disable=global-statement + MUZYKA_MOJEGO_LUDU_HISTORIA = ile_historii + global MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE # pylint: disable=global-statement + MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = ile_slow_kluczowych + global MUZYKA_MOJEGO_LUDU_PLAJLISTA # pylint: disable=global-statement + MUZYKA_MOJEGO_LUDU_PLAJLISTA = jak_dluga_plejlista # *================================== Run