Nowy lepszy trunk

Additional lines of code

1

AAA
This commit is contained in:
2024-02-02 22:35:19 +01:00
committed by migatu
parent 432abc73b9
commit f55c4ce1a7
9 changed files with 1937 additions and 92 deletions
+89 -57
View File
@@ -1,12 +1,10 @@
# This Python file uses the following encoding: utf-8
# trunk-ignore-all(bandit/B311)
# pylint: disable=line-too-long
# pylint: disable=too-many-lines
"""
Module of a python bot named Conjurer - used to work on BDSM discord servers.
"""
# This Python file uses the following encoding: utf-8
# trunk-ignore-all(flake8/E501)
# trunk-ignore-all(bandit/B311)
# trunk-ignore-all(mypy/index)
# pylint: disable=line-too-long
# pylint: disable=too-many-lines
import asyncio
import io
import json
@@ -40,8 +38,8 @@ from discord.ext import commands
from spotipy.oauth2 import SpotifyClientCredentials
import yt_dlp
from spotify_dl import youtube as youtube_download
from spotify_dl import spotify
from spotify_dl import youtube as youtube_download
Music_Config = TypedDict(
"Music_Config",
@@ -54,12 +52,10 @@ Music_Config = TypedDict(
)
# *=========================================== Predefines
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
MASTER_TIMEOUT = datetime.now()
INITIAL_TIME_WAIT = 5
MUZYKA: Music_Config = {"playing": False,
"ctx": None, "queue": [], "requester": []}
MUZYKA: Music_Config = {"playing": False, "ctx": None, "queue": [], "requester": []}
LOGFILE = ""
NETRC_FILE = ""
@@ -209,7 +205,6 @@ async def on_ready():
# trunk-ignore(pylint/R0914)
# trunk-ignore(pylint/R0912)
async def on_message(message):
"""
Handle incoming messages in a Discord server, perform various
checks and actions based on the content and context of the message, and respond accordingly.
@@ -315,14 +310,16 @@ async def on_message(message):
logger.info("Poczatek procedury obrazkowej")
message.content = message.content.replace("imaginuje sobie: ", "")
logger.debug("Wywolanie obrazka: %s", message.content)
response = openAIClient.images.generate(
response = await openAIClient.images.generate(
model="dall-e-3",
prompt=message.content,
size="1024x1024",
quality="standard",
n=1,
)
image_url = response["data"][0]["url"]
logger.info(response)
image_url = response.data[0].url
logger.debug("Wynikowy obrazek pod url: %s", image_url)
response = requests.get(image_url, timeout=360)
@@ -340,6 +337,7 @@ async def on_message(message):
# *=========================================== Define Functions
def num_tokens_from_string(message, model):
"""
The function takes a string message and a model as input and returns the number of tokens in the
@@ -493,6 +491,25 @@ async def play(ctx, zamawial=None, arg=None):
await ctx.send(result)
async def archive_channel(channel_no):
"""
The function `archive_channel` retrieves messages from a specified channel, processes them, and
saves the data in a JSON file with a timestamp in the filename.
:param channel_no: The `archive_channel` function you provided seems to be incomplete. It looks like
you are trying to archive messages from a Discord channel into a JSON file
"""
channel = client.get_channel(channel_no)
messages = [message async for message in channel.history(limit=None)]
path_newfile = f"{LOGSTORE}channeldump_{channel_no}_{datetime.now()}.json"
new_data = []
for message in messages:
pass
with open(path_newfile, "x", encoding=ENCODING) as new_file:
new_file.seek(0)
json.dump(new_data, new_file, indent=4)
async def check():
"""Funkcja sprawdzająca czy grać następny kawałek."""
last_spontaneous_call = datetime.now()
@@ -578,7 +595,7 @@ async def get_random_cyclic_message():
words.
:return: a random cyclic message from the list `cyclic_words`.
"""
messnum = random.randint(0, len(cyclic_words)-1)
messnum = random.randint(0, len(cyclic_words) - 1)
return cyclic_words[messnum][0]
@@ -630,30 +647,47 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
history.append(GPT_SETTINGS[0])
chat_gpt_config_request_size = num_tokens_from_string(GPT_SETTINGS[0], "gpt-4")
for (slowo, reakcja) in word_reactions.items():
for slowo, reakcja in word_reactions.items():
if not reakcja[3]:
content = "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]
content = (
"Kiedy słyszysz "
+ slowo
+ " to reagujesz lub dzieje się to "
+ reakcja[0]
)
temp = {"role": "system", "content": content}
chat_gpt_config_request_size += num_tokens_from_string(temp, "gpt-4")
history.append(temp)
final_prompt = username + ":" + prompt
logger.info("Rozmiar zapytania przed dodaniem historii %s", chat_gpt_config_request_size)
logger.info(
"Rozmiar zapytania przed dodaniem historii %s", chat_gpt_config_request_size
)
if music:
algorithm = "gpt-3.5-turbo-0125"
table = message_table_muzyka
token_amount = 10700
else:
table = MESSAGE_TABLE
algorithm = "gpt-4-turbo-preview"
token_amount = 28000
algorithm = "gpt-4"
token_amount = 7000
prompt_gpt_request_size = num_tokens_from_string({"role": "user", "content": final_prompt}, "gpt-4")
prompt_gpt_request_size = num_tokens_from_string(
{"role": "user", "content": final_prompt}, "gpt-4"
)
temptable = []
for i in reversed(table):
temp_token = num_tokens_from_string(i, "gpt-4")
logger.info("Rozmiar zapytania %s prompt %s temp %s", chat_gpt_config_request_size, prompt_gpt_request_size, temp_token)
if chat_gpt_config_request_size < token_amount + prompt_gpt_request_size + temp_token:
logger.info(
"Rozmiar zapytania %s prompt %s temp %s",
chat_gpt_config_request_size,
prompt_gpt_request_size,
temp_token,
)
if (
chat_gpt_config_request_size
< token_amount + prompt_gpt_request_size + temp_token
):
temptable.insert(1, i)
chat_gpt_config_request_size += temp_token
history.extend(temptable)
@@ -661,8 +695,8 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
history.append(temp)
logger.info("Rozmiar zapytania po wyslaniu %s", chat_gpt_config_request_size)
response = await openAIClient.chat.completions.create(
model=algorithm,
messages=history)
model=algorithm, messages=history
)
logger.info("Historia wysłana:")
logger.info(history)
@@ -725,8 +759,7 @@ async def get_file(ctx, source, link):
item_type, item_id = spotify.parse_spotify_url(link)
if item_id:
file_list = spotify.fetch_tracks(spotify_ctrl, item_type, link)
directory_name = spotify.get_item_name(
spotify_ctrl, item_type, item_id)
directory_name = spotify.get_item_name(spotify_ctrl, item_type, item_id)
url_data = {"urls": []}
url_dict = {}
url_dict["save_path"] = Path(
@@ -961,15 +994,13 @@ async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None):
if re.match(pattern, itm, re.IGNORECASE):
temp_weight = (
search_weight[itr][0]
+ (token_weight + (pingu**1.5) /
pattern_len) / matched_times
+ (token_weight + (pingu**1.5) / pattern_len) / matched_times
)
search_weight[itr] = temp_weight, music_file_list[itr]
matched_times += 1
itr += 1
logger.info("Stworzylem tablice wag zajęło mi to %s",
datetime.now() - time_start)
logger.info("Stworzylem tablice wag zajęło mi to %s", datetime.now() - time_start)
time_start = datetime.now()
item_to_search = await max_weight(search_weight)
itr = 0
@@ -1022,7 +1053,6 @@ async def max_weight(lista):
# *=========================================== Define Commands
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="dej_co_ze_spotifaja",
description="Podaj link do spotify - sciagnie i doda muzyke",
@@ -1068,7 +1098,6 @@ async def dej_co_ze_spotifaja(ctx):
logger.info("Spotifaj udany")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="dej_co_z_jutuba",
description="Podaj link do youtube - sciagnie i doda muzyke",
@@ -1101,7 +1130,6 @@ async def dej_co_z_jutuba(ctx):
logger.info("Jutub udany")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="update_banlist",
description="Jeśli nie wiesz jak użyć tej komendy to nawet nie próbuj",
@@ -1141,7 +1169,7 @@ async def update_banlist(ctx):
for line in lines:
match = re.search(
"\\d{18}",
line
line,
# trunk-ignore(codespell/misspelled)
) # poprawilem backslash na podwojny bo linter sie czepial
if match:
@@ -1192,7 +1220,6 @@ async def update_banlist(ctx):
await ctx.send("Zrobione tak czy inaczej")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
nsfw=True,
name="get_image_sadox",
@@ -1237,7 +1264,6 @@ async def get_image_sadox(ctx):
logger.info("Get sadox completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="graj_muzyko",
description="Włącza muzykę na kanale #nocna-zmiana.",
@@ -1263,7 +1289,6 @@ async def graj_muzyko(ctx):
logger.info("Press play on tape completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="cisza",
description="Wyłącza muzykę i czyści plejliste.",
@@ -1291,7 +1316,6 @@ async def cisza(ctx):
logger.info("Stop completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="dalej",
description="Przerzuca na następny kawałek",
@@ -1316,7 +1340,6 @@ async def dalej(ctx):
logger.info("Next completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="daj_mi_chwile",
description="Pauzuje",
@@ -1341,7 +1364,6 @@ async def daj_mi_chwile(ctx):
logger.info("Pause completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="graj_dalej",
description="Odpauzowuje",
@@ -1365,7 +1387,6 @@ async def graj_dalej(ctx):
logger.info("Unpause completed")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="zagraj_mi_kawalek",
description="Wyszukuje w bibliotece muzycznej Hammera kawałek który ma zostać zagrany",
@@ -1412,7 +1433,6 @@ async def zagraj_mi_kawalek(ctx):
)
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="zrob_mi_plejliste",
description="Generuje playliste - param1 - dlugosc, potem slowa wyszukiwania",
@@ -1474,7 +1494,6 @@ async def zrob_mi_plejliste(ctx):
logger.info("Plejlista zrobiona")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="przytul", description="Przytul kogoś - daj mention po komendzie :)"
)
@@ -1511,7 +1530,6 @@ async def przytul(ctx, arg: Optional[discord.Member] = None):
)
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(name="fabryczka", description="Historia fabryczki")
async def fabryczka(ctx):
"""
@@ -1525,7 +1543,6 @@ async def fabryczka(ctx):
await ctx.send(historia_fabryczki)
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="chata_hammera",
description="Czas od ostatniego incydentu w AbsinthHammerTimeSpaceContinuum",
@@ -1549,7 +1566,6 @@ async def chata_hammera(ctx):
)
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="historia_incydentow_u_hammera",
description="Wyswietla liste incydentów które miały miejsce w AbsinthHammerTimeSpaceContinuum.",
@@ -1577,7 +1593,6 @@ async def historia_incydentow_u_hammera(ctx):
await ctx.send(return_data)
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="reset_the_clock",
description="Resetowanie zegara - dostępne wyłącznie dla Hammera",
@@ -1630,7 +1645,6 @@ async def get_image_stable_diffusion(ctx):
pass
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="zagraj_muzyke_mego_ludu",
description="Gra muzyke wyszukana na bazie tematow konwersacji na kanale NZ",
@@ -1671,7 +1685,11 @@ async def zagraj_muzyke_mego_ludu(ctx):
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=final_key_words)
file = await wyszukaj(
ctx=ctx,
how_many=MUZYKA_MOJEGO_LUDU_PLAJLISTA,
slowa_kluczowe=final_key_words,
)
logger.info("Zagraj muzyke mego ludu:wygenerowano plejliste")
logger.info(file)
logger.info(
@@ -1713,13 +1731,14 @@ async def zagraj_muzyke_mego_ludu(ctx):
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=15, jak_dluga_plejlista=30):
async def parametry_muzyki_mego_ludu(
ctx, ile_historii=1500, ile_slow_kluczowych=15, 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.
@@ -1748,18 +1767,31 @@ async def parametry_muzyki_mego_ludu(ctx, ile_historii=1500, ile_slow_kluczowych
global MUZYKA_MOJEGO_LUDU_HISTORIA # pylint: disable=global-statement
global MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE # pylint: disable=global-statement
global MUZYKA_MOJEGO_LUDU_PLAJLISTA # pylint: disable=global-statement
logger.info("Długość historii wyszukiwania: %s\n Ilość słów kluczowych: %s\n Długość plejlisty do wygenerowanie: %s", MUZYKA_MOJEGO_LUDU_HISTORIA, MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE, MUZYKA_MOJEGO_LUDU_PLAJLISTA)
ctx.send(f"Dotychczasowe wartośći:\n Długość historii wyszukiwania: {MUZYKA_MOJEGO_LUDU_HISTORIA}\n Ilość słów kluczowych: {MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE}\n Długość plejlisty do wygenerowanie:{MUZYKA_MOJEGO_LUDU_PLAJLISTA}")
logger.info(
"Długość historii wyszukiwania: %s\n Ilość słów kluczowych: %s\n Długość plejlisty do wygenerowanie: %s",
MUZYKA_MOJEGO_LUDU_HISTORIA,
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE,
MUZYKA_MOJEGO_LUDU_PLAJLISTA,
)
ctx.send(
f"Dotychczasowe wartośći:\n Długość historii wyszukiwania: {MUZYKA_MOJEGO_LUDU_HISTORIA}\n Ilość słów kluczowych: {MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE}\n Długość plejlisty do wygenerowanie:{MUZYKA_MOJEGO_LUDU_PLAJLISTA}"
)
MUZYKA_MOJEGO_LUDU_HISTORIA = ile_historii
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = ile_slow_kluczowych
MUZYKA_MOJEGO_LUDU_PLAJLISTA = jak_dluga_plejlista
logger.info("Długość historii wyszukiwania: %s\n Ilość słów kluczowych: %s\n Długość plejlisty do wygenerowanie: %s", MUZYKA_MOJEGO_LUDU_HISTORIA, MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE, MUZYKA_MOJEGO_LUDU_PLAJLISTA)
ctx.send(f"Zmienione wartośći:\n Długość historii wyszukiwania: {MUZYKA_MOJEGO_LUDU_HISTORIA}\n Ilość słów kluczowych: {MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE}\n Długość plejlisty do wygenerowanie:{MUZYKA_MOJEGO_LUDU_PLAJLISTA}")
logger.info(
"Długość historii wyszukiwania: %s\n Ilość słów kluczowych: %s\n Długość plejlisty do wygenerowanie: %s",
MUZYKA_MOJEGO_LUDU_HISTORIA,
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE,
MUZYKA_MOJEGO_LUDU_PLAJLISTA,
)
ctx.send(
f"Zmienione wartośći:\n Długość historii wyszukiwania: {MUZYKA_MOJEGO_LUDU_HISTORIA}\n Ilość słów kluczowych: {MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE}\n Długość plejlisty do wygenerowanie:{MUZYKA_MOJEGO_LUDU_PLAJLISTA}"
)
except discord.ext.commands.errors.BadArgument as exce:
ctx.send("Spierdalaj")
logger.info(exce)
# *================================== Run
# trunk-ignore(mypy/arg-type)
client.run(TOKEN)