136 lines
4.3 KiB
Python
Executable file
136 lines
4.3 KiB
Python
Executable file
import discord
|
|
from discord import app_commands
|
|
from discord.ext import commands, tasks
|
|
import requests
|
|
import threading
|
|
import random
|
|
import time
|
|
import json
|
|
from llama_cpp import Llama
|
|
|
|
with open("settings.json", "r") as file:
|
|
settings = json.load(file)
|
|
|
|
LLM = Llama(
|
|
model_path=settings["llama"]["model"],
|
|
n_ctx=settings["llama"]["ctx"],
|
|
n_threads=settings["llama"]["threads"],
|
|
n_gpu_layers=settings["llama"]["gpu"],
|
|
top_k=settings["llama"]["top_k"],
|
|
repeat_penality=settings["llama"]["repeat_penality"],
|
|
echo=False,
|
|
verbose=False)
|
|
if len(list(settings["llama"]["system_token"])) == 2:
|
|
SYS_PROMPT = list(settings["llama"]["system_token"])[0] + settings["llama"]["system_prompt"] + list(settings["llama"]["system_token"])[1]
|
|
elif len(list(settings["llama"]["system_token"])) == 1:
|
|
SYS_PROMPT = list(settings["llama"]["system_token"])[0] + settings["llama"]["system_prompt"]
|
|
else:
|
|
SYS_PROMPT = settings["llama"]["system_prompt"]
|
|
USER_TOKEN = list(settings["llama"]["user_token"])
|
|
SYNC = settings["bot"]["sync"]
|
|
UWU_ACT = list(settings["uwu"]["act"])
|
|
UWU_REPLACE = dict(settings["uwu"]["replace"])
|
|
CONSONNES = list(settings["uwu"]["consonnes"])
|
|
LISTE_GIF = list(settings["gifs"])
|
|
|
|
intents = discord.Intents.default()
|
|
bot = commands.Bot(command_prefix="+", intents=intents, help_command=None)
|
|
tree = bot.tree
|
|
queue = []
|
|
|
|
def texttouwu(text: str) -> str:
|
|
uwu_text = ""
|
|
for i in range(1, len(text) - 1):
|
|
if random.randint(0, 2) == 1:
|
|
if text[i] in UWU_REPLACE:
|
|
uwu_text += UWU_REPLACE[text[i]]
|
|
elif text[i - 1] == " " and text[i] in CONSONNES:
|
|
uwu_text += f"{text[i]}-{text[i]}"
|
|
else:
|
|
uwu_text += text[i]
|
|
else:
|
|
if text[i - 1] == " " and random.randint(0, 10) == 0:
|
|
uwu_text += random.choice(UWU_ACT)
|
|
uwu_text += text[i]
|
|
return text[0] + uwu_text + text[-1]
|
|
|
|
|
|
def get_gif(gif):
|
|
if gif.lower() == "list":
|
|
output = "Here is the list of all possible gifs:\n"
|
|
for name in LISTE_GIF:
|
|
output += name + ", "
|
|
return output[:-2]
|
|
gif = requests.get(f"https://api.otakugifs.xyz/gif?reaction={gif.lower()}").json()
|
|
try:
|
|
return gif["url"]
|
|
except:
|
|
return f"No gif '{gif}'"
|
|
|
|
|
|
def generate(prompt):
|
|
if len(USER_TOKEN) == 2:
|
|
user_prompt = USER_TOKEN[0] + prompt + USER_TOKEN[1]
|
|
elif len(USER_TOKEN) == 1:
|
|
user_prompt = USER_TOKEN[0] + prompt
|
|
else:
|
|
user_prompt = prompt
|
|
res = LLM(SYS_PROMPT + user_prompt, max_tokens=128, stop=["</s>"])
|
|
story = str(res["choices"][0]["text"])
|
|
return story[2:].capitalize()
|
|
|
|
|
|
def send_answer(id, token, content):
|
|
url = f"https://discord.com/api/v10/webhooks/{id}/{token}/messages/@original"
|
|
json = {
|
|
"content": content,
|
|
}
|
|
code = requests.patch(url, data=json)
|
|
return code
|
|
|
|
|
|
def work():
|
|
global queue
|
|
while True:
|
|
if not queue == []:
|
|
output = queue[0][1](queue[0][0])
|
|
send_answer(queue[0][2], queue[0][3], output)
|
|
queue.pop(0)
|
|
time.sleep(0.1)
|
|
return None
|
|
|
|
|
|
@bot.tree.command(name="speak", description="Speak with Whispers!")
|
|
@app_commands.describe(message="The message you want to send to Whispers.")
|
|
async def speak(interaction, message: str):
|
|
global queue
|
|
await interaction.response.defer(thinking=True)
|
|
queue.append([message, generate, interaction.application_id, interaction.token])
|
|
|
|
|
|
@bot.tree.command(name="uwu", description="Translate your message to uwu")
|
|
@app_commands.describe(nonuwu="The message you want to translate.")
|
|
async def uwu(interaction, nonuwu: str):
|
|
global queue
|
|
await interaction.response.defer(thinking=True)
|
|
queue.append([nonuwu, texttouwu, interaction.application_id, interaction.token])
|
|
|
|
|
|
@bot.tree.command(
|
|
name="gif",
|
|
description="Send a gif corresponding to a topic, type 'list' to get the possible gifs",
|
|
)
|
|
@app_commands.describe(topic="The the topic of the gif")
|
|
async def gif(interaction, topic: str):
|
|
global queue
|
|
await interaction.response.defer(thinking=True)
|
|
queue.append([topic, get_gif, interaction.application_id, interaction.token])
|
|
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
if SYNC == "1":
|
|
await tree.sync()
|
|
|
|
threading.Thread(target=work).start()
|
|
bot.run(settings["bot"]["token"])
|