SheepBotFrame/run.py

122 lines
3.7 KiB
Python
Raw Normal View History

import os
import sys
import yaml
import discord
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer, ListTrainer
with open("config.yml", "r") as file:
config = yaml.load(file)
2021-01-12 12:49:03 -06:00
bot = discord.Client()
tasks = [False, False]
2021-01-19 12:42:53 -06:00
finished = False
def printhelp():
print("Help lol")
def printversion():
print("Somer Version")
2021-01-19 12:57:54 -06:00
def remove_mention(m, s, i):
if m == "s":
return s[0:i] + s[i+21:]
elif m == "l":
return s[:i] + s[i+22:]
async def download():
await bot.change_presence(activity=discord.Game(name=" downloading Message History"))
2021-01-12 13:37:19 -06:00
os.system("rm chatlogs/*.tmp.yml")
for trainchannel in config["trainchannels"]:
channel = await bot.fetch_channel(trainchannel[0])
counter = 0
messages = [None] * trainchannel[1]
async for message in channel.history(limit=trainchannel[1]):
messages[counter] = message.content
counter += 1
messages.reverse()
2021-01-12 13:36:52 -06:00
with open("chatlogs/" + str(trainchannel[0]) + ".tmp.yml", "w") as logfile:
yaml.dump(messages, logfile)
2021-01-12 13:36:52 -06:00
async def train():
corpustrainer = ChatterBotCorpusTrainer(chatbot)
listtrainer = ListTrainer(chatbot)
for corpus in config["corpus"]:
corpustrainer.train(corpus)
for log in os.listdir("chatlogs"):
with open(f"chatlogs/{log}", "r") as logfile:
2021-01-12 13:36:52 -06:00
log = yaml.load(logfile)
listtrainer.train(log)
@bot.event
async def on_ready():
global chatbot
print("Logged in!")
args = sys.argv
args.pop(0)
for arg in args:
if arg == "--help" or arg == "-?":
printhelp()
await bot.close()
raise SystemExit(0)
elif arg == "--version" or arg == "-v":
printversion()
await bot.close()
raise SystemExit(0)
elif arg == "--download" or arg == "-d":
tasks[0] = True
elif arg == "--train" or arg == "-t":
tasks[1] = True
else:
print(f"Unexspected {arg}")
printhelp()
await bot.close()
raise SystemExit(1)
if tasks[0]:
await download()
print("Finished download!")
2021-01-12 13:36:52 -06:00
if tasks[1]:
2021-01-13 12:54:25 -06:00
os.system("rm db.sqlite3")
chatbot = ChatBot(config["botname"])
2021-01-12 13:36:52 -06:00
await train()
print("Finished train!")
2021-01-13 12:54:25 -06:00
else:
chatbot = ChatBot(config["botname"])
2021-01-19 13:00:06 -06:00
global finished
finished = True
print("Starting to listen to messages...")
2021-01-12 12:49:03 -06:00
2021-01-19 12:57:54 -06:00
@bot.event
async def on_message(message):
2021-01-19 13:00:06 -06:00
if finished:
if message.author != bot.user:
if message.channel.id in config["usechannels"]:
if message.content.startswith(config["excludeprefix"]):
2021-01-19 13:00:06 -06:00
return
else:
response = chatbot.get_response(message.content)
response = response.__str__()
if config["filterpings"]:
2021-01-19 12:57:54 -06:00
position = response.find("<@!")
2021-01-19 13:00:06 -06:00
while position != -1:
response = remove_mention("l", response, position)
position = response.find("<@!")
2021-01-19 12:57:54 -06:00
position = response.find("<@")
2021-01-19 13:00:06 -06:00
while position != -1:
response = remove_mention("s", response, position)
position = response.find("<@")
await message.channel.send(response)
print(f"Input was given: '{message.content}', this output was found: '{response}'")
else:
await message.channel.send("Sorry, the Bot is still performing some tasks.")
2021-01-19 12:57:54 -06:00
bot.run(config["token"])