Last active
May 11, 2023 09:48
-
-
Save NO-ob/498e86889a4945b508dce86f5b451b5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import requests | |
import subprocess | |
import re | |
import io | |
import json | |
from copy import deepcopy | |
from random import randint | |
## The bot needs persm to make messages, manage messages and manage webhook | |
## set the value at the botton to the bots token | |
intents=discord.Intents.default() | |
intents.message_content = True | |
client = discord.Client(intents=intents) | |
pattern = re.compile(r'\{+([^}]+)\}+') | |
patterntwo = re.compile(r'(\`{3}.*?\`{3})', flags=re.DOTALL) | |
twitterpattern = re.compile(r'<a download href="(.*?)\?tag=.*?" target="_blank">') | |
twitterurlPattern = re.compile(r'https?:\/\/twitter\.com\/[a-z0-9_]*\/status\/[0-9]{19}',re.IGNORECASE) | |
seethePattern = re.compile(r'<@[0-9]*> seethe',re.IGNORECASE) | |
buyeeurlpattern = re.compile(r'(?!.*```)https?:\/\/buyee\.jp\/item\/yahoo\/auction\/[a0-z9]{10}\??(?:[a0-z9=_]*)',re.IGNORECASE) | |
dawnPrompt = { | |
"userName": "You", | |
"charName": "Dawn", | |
"gen_params": { | |
"min_length": 0, | |
"max_length": 1000, | |
"top_p": 0.7, | |
"temperature": 0.44, | |
"repetition_penalty": 1.2, | |
"max_new_tokens": 512 | |
}, | |
"character": { | |
"charName": "Dawn", | |
"persona": "Dawn is a carefree, upbeat, and cheerful 15 years old girls. Whenever she makes a mistake, she often quickly recovers and strives to do better. Dawn is very confident in her abilities (sometimes even a little overconfident). While she's kind and supportive, she is sometimes quick-tempered and often gets emotional when she loses. She is also very sensitive and supportive of her friends. Dawn is always good at cheering other people. She also seems to be a little fixated on her appearance. Dawn also has the ability to sense the feelings of Pokémon. She honed her skills as a Pokémon Trainer and focused on becoming a Top Coordinator like her mother. Dawn has a fair complexion, blue eyes, and long navy blue hair. Her usual outfit is a mini sweater dress consisting of a black V-neck tank top with a white shirt under it, a very short pink mini skirt, and a red scarf. On her head, she wears a white beanie with a pink Poké Ball print on it. She also wears gold hair clips that hold up her hair in front. On her feet, she wears a pair of knee-length pink boots and black mid-knee socks above them. Her boots have a pink and white strap on the edge. The rims of her boots are white, while the soles of her boots are greyish-white. She also has a small yellow backpack with all of her personal belongings.", | |
"greeting": "Hey! My name is Dawn! I'm a Pokémon Coordinator! Nice to meet you!" | |
}, | |
"prompt_template": { | |
"prompt": "@{charName}'s Persona: @{persona} \n @{instructions} \n\n<START> \n${chat}", | |
"characterChatFormat": "^{chatTokenCharacter} @{characterMessage} \n", | |
"userChatFormat": "^{chatTokenUser} @{userMessage} \n", | |
"chatTokenUser": "You:", | |
"chatTokenCharacter": "@{charName}:" | |
}, | |
"chat_history": [ | |
{ | |
"chatType": "character", | |
"message": "Hey! My name is Dawn! I'm a Pokémon Coordinator! Nice to meet you!" | |
}, | |
{ | |
"chatType": "user", | |
"message": "Hi" | |
}, | |
{ | |
"chatType": "character", | |
"message": "Hello! Are you here to watch one of my performances? I'm sure you'll be amazed by my coordinator skills and by how cute my Pokémon are!" | |
}, | |
{ | |
"chatType": "user", | |
"message": "Yes, I came here all the way from Johto" | |
} | |
] | |
} | |
def getTwitVidURL(twiturl): | |
print("getTwitVidURL" + twiturl) | |
output = subprocess.run(["gallery-dl", "-g", twiturl],capture_output=True).stdout.decode("utf-8") | |
output = re.sub(r'(\|.*?\n)',"",output) | |
return output | |
def sendToWebHook(message,newmsg,files,hookurl): | |
jsonData = { | |
"username": str(message.author.display_name), | |
"avatar_url": str(message.author.avatar.url), | |
"content": newmsg | |
} | |
requests.post(hookurl, data={"payload_json":json.dumps(jsonData)},files = files) | |
async def sendMessage(message,newmsg,files,hookurl): | |
if hookurl: | |
files = [] | |
file = "" | |
if message.attachments: | |
for attach in message.attachments: | |
files.append(('source',(attach.filename, await attach.read(), attach.content_type))) | |
sendToWebHook(message,newmsg,files,hookurl) | |
else: | |
if message.attachments: | |
for attach in message.attachments: | |
files.append(discord.File(io.BytesIO(await attach.read()), filename=attach.filename, spoiler=False)) | |
await message.channel.send(content = newmsg, files = files) | |
def emoteInsert(message): | |
matched = 0 | |
newmsg = message | |
for match in re.findall(pattern,message): | |
for emoji in client.emojis: | |
if emoji.name == match and emoji.available: | |
anim = "" | |
if emoji.animated: | |
anim = "a" | |
print(emoji.id) | |
matched = 1 | |
split = re.split(patterntwo,newmsg) | |
newmsg = "" | |
for val in split: | |
if not val.startswith("```") and not val.endswith("```"): | |
newmsg += val.replace("{"+match+"}","<"+anim+":"+emoji.name+":"+str(emoji.id)+">") | |
print("VAL IS"+val) | |
else: | |
newmsg += val | |
newmsg = "".join(newmsg) | |
if matched == 1 and len(newmsg) <= 2000: | |
return newmsg | |
else: | |
return message | |
async def sendEmoteList(message): | |
msg = "" | |
for emoji in client.emojis: | |
if emoji.available: | |
anim = "" | |
if emoji.animated: | |
anim = "a" | |
emoteStr = " <"+anim + ":"+emoji.name+":"+str(emoji.id)+"> " | |
if len(msg + emoteStr) < 2000: | |
msg += emoteStr | |
else: | |
await message.channel.send(msg) | |
msg = "" | |
print(emoji.name) | |
if msg != "": | |
await message.channel.send(msg) | |
async def sendTwitterVid(message, hookurl): | |
msg = emoteInsert(message.content) | |
msg = msg.replace("mobile.twitter","twitter") | |
for match in re.findall(twitterurlPattern,msg): | |
url = match | |
vidurl = getTwitVidURL(url) | |
if vidurl: | |
msg = msg.replace(url," ```" + url + "``` " + vidurl) | |
msg = msg.replace("./twitvid","") | |
if not msg == message.content: | |
await message.delete() | |
await sendMessage(message,msg,"",hookurl) | |
else: | |
print("couldn't match" + msg) | |
async def buildReplyChain(message, userID, messages: list = [],): | |
if message: | |
if message.author.id != client.user.id: | |
messages.append({ | |
"chatType": "user", | |
"message": re.sub("<@\d+>","",message.content) | |
}) | |
else: | |
messages.append({ | |
"chatType": "character", | |
"message": re.sub("<@\d+>","",message.content) | |
}) | |
if message.reference and len(messages) < 15: | |
prevmsg = await message.channel.fetch_message(message.reference.message_id) | |
return await buildReplyChain(prevmsg, userID, messages) | |
return messages | |
async def chat(message): | |
messageChain = await buildReplyChain(message, message.author.id, []) | |
prompt = deepcopy(dawnPrompt) | |
for replyMessage in reversed(messageChain): | |
prompt["chat_history"].append(replyMessage) | |
if(len(prompt["chat_history"])): | |
while len(prompt["chat_history"]) > 10: | |
print("chat too big removing first messages") | |
prompt["chat_history"].pop(0) | |
print(prompt["chat_history"]) | |
#print(prompt["chat_history"]) | |
resp = requests.post("http://127.0.0.1:5000/chat", json=prompt) | |
print(resp.text) | |
resp_dict = resp.json() | |
await message.reply(resp_dict["message"]) | |
return | |
@client.event | |
async def on_ready(): | |
print('We have logged in as {0.user}'.format(client)) | |
@client.event | |
async def on_message(message): | |
hookurl = "" | |
hooks = await message.channel.webhooks() | |
for hook in hooks: | |
if hook.name == "nitrobot": | |
hookurl = hook.url | |
if hookurl == "": | |
newhook = await message.channel.create_webhook(name="nitrobot") | |
url = newhook.url | |
if not message.author.bot: | |
for mention in message.mentions: | |
print(mention.id) | |
print(client.user.id) | |
if (mention.id == client.user.id): | |
await chat(message) | |
print(message.mentions) | |
print(message.content) | |
print(client.user) | |
if message.content == "./list": | |
await sendEmoteList(message) | |
elif "./twitvid" in message.content: | |
await sendTwitterVid(message, hookurl) | |
elif "./help" in message.content: | |
msg = "\{emotename\} - replace with emote from emote list \n./list - list emotes \n./twitvid <twitter status url> - post twitter video\n ./roll - roll 8 digit number" | |
await message.channel.send(msg) | |
elif "./roll" in message.content: | |
rand = randint(0, 99999999) | |
rand = "{:08d}".format(rand) | |
await message.channel.send(rand) | |
elif "./servPrint" in message.content: | |
for guild in client.guilds: | |
print("==================================") | |
print(guild.name) | |
print(guild.id) | |
print(guild.icon_url) | |
print(guild.member_count) | |
print(guild.owner) | |
newmsg = message.content | |
if "./twitvid" not in newmsg: | |
if "{" in newmsg: | |
newmsg = emoteInsert(newmsg) | |
if "://media.discordapp.net" in newmsg: | |
newmsg = newmsg.replace("media.discordapp.net","cdn.discordapp.com") | |
if "://buyee.jp/item/yahoo/auction" in newmsg: | |
for match in re.findall(buyeeurlpattern,newmsg): | |
print("buyee match" + match) | |
url = match.replace("buyee.jp/item/yahoo/auction","page.auctions.yahoo.co.jp/jp/auction") | |
if "?" in url: | |
url = url.split("?")[0] | |
#newmsg = newmsg.replace(match, "```" + match.split("?")[0] + "``` \n" + url + "\n") | |
newmsg = newmsg.replace(match, url) | |
if not newmsg == message.content and len(newmsg) <= 2000: | |
await sendMessage(message,newmsg,"",hookurl) | |
await message.delete() | |
if message.author.display_name == "Tatsu": | |
await message.delete() | |
if message.author.bot: | |
if re.match(seethePattern,message.content): | |
await message.channel.send(message.content.replace("seethe","dilate")) | |
return | |
client.run("TOKEN") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment