pull/1/head
Bobloy 7 years ago
parent b5c2d3d10b
commit e5a16c908e

@ -1,16 +1,13 @@
import asyncio import asyncio
from typing import List, Union from datetime import datetime, timedelta
import discord import discord
from discord.ext import commands from discord.ext import commands
from redbot.core import Config from redbot.core import Config
from redbot.core.bot import Red
from .source import ChatBot from .source import ChatBot
from .source.trainers import ListTrainer from .source.trainers import ListTrainer
from datetime import datetime,timedelta
class Chatter: class Chatter:
""" """
@ -24,17 +21,17 @@ class Chatter:
default_guild = { default_guild = {
"whitelist": None, "whitelist": None,
"days": 1 "days": 1
} }
self.chatbot = ChatBot("ChatterBot") self.chatbot = ChatBot("ChatterBot")
self.chatbot.set_trainer(ListTrainer) self.chatbot.set_trainer(ListTrainer)
self.config.register_global(**default_global) self.config.register_global(**default_global)
self.config.register_guild(**default_guild) self.config.register_guild(**default_guild)
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
async def _get_conversation(self, ctx, in_channel: discord.TextChannel=None): async def _get_conversation(self, ctx, in_channel: discord.TextChannel = None):
""" """
Compiles all conversation in the Guild this bot can get it's hands on Compiles all conversation in the Guild this bot can get it's hands on
Currently takes a stupid long time Currently takes a stupid long time
@ -42,9 +39,8 @@ class Chatter:
""" """
out = [] out = []
after = datetime.today() - timedelta(days=(await self.config.guild(ctx.guild).days())) after = datetime.today() - timedelta(days=(await self.config.guild(ctx.guild).days()))
for channel in ctx.guild.text_channels: for channel in ctx.guild.text_channels:
if in_channel: if in_channel:
channel = in_channel channel = in_channel
await ctx.send("Gathering {}".format(channel.mention)) await ctx.send("Gathering {}".format(channel.mention))
@ -52,7 +48,7 @@ class Chatter:
try: try:
async for message in channel.history(limit=None, reverse=True, after=after): async for message in channel.history(limit=None, reverse=True, after=after):
if user == message.author: if user == message.author:
out[-1] += "\n"+message.clean_content out[-1] += "\n" + message.clean_content
else: else:
user = message.author user = message.author
out.append(message.clean_content) out.append(message.clean_content)
@ -60,12 +56,12 @@ class Chatter:
pass pass
except discord.HTTPException: except discord.HTTPException:
pass pass
if in_channel: if in_channel:
break break
return out return out
def _train(self, data): def _train(self, data):
try: try:
self.chatbot.train(data) self.chatbot.train(data)
@ -80,45 +76,46 @@ class Chatter:
""" """
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send_help() await ctx.send_help()
@chatter.command() @chatter.command()
async def age(self, ctx: commands.Context, days: int): async def age(self, ctx: commands.Context, days: int):
""" """
Sets the number of days to look back Sets the number of days to look back
Will train on 1 day otherwise Will train on 1 day otherwise
""" """
await self.config.guild(ctx.guild).days.set(days) await self.config.guild(ctx.guild).days.set(days)
await ctx.send("Success") await ctx.send("Success")
@chatter.command() @chatter.command()
async def train(self, ctx: commands.Context, channel: discord.TextChannel = None): async def train(self, ctx: commands.Context, channel: discord.TextChannel = None):
""" """
Trains the bot based on language in this guild Trains the bot based on language in this guild
""" """
conversation = await self._get_conversation(ctx, channel) conversation = await self._get_conversation(ctx, channel)
if not conversation: if not conversation:
await ctx.send("Failed to gather training data") await ctx.send("Failed to gather training data")
return return
await ctx.send("Gather successful! Training begins now\n(**This will take a long time, be patient**)") await ctx.send("Gather successful! Training begins now\n(**This will take a long time, be patient**)")
embed=discord.Embed(title="Loading") embed = discord.Embed(title="Loading")
embed.set_image(url="http://www.loop.universaleverything.com/animations/1295.gif") embed.set_image(url="http://www.loop.universaleverything.com/animations/1295.gif")
temp_message = await ctx.send(embed=embed) temp_message = await ctx.send(embed=embed)
future = await self.loop.run_in_executor(None, self._train, conversation) future = await self.loop.run_in_executor(None, self._train, conversation)
try: try:
await temp_message.delete() await temp_message.delete()
except: except:
pass pass
if future: if future:
await ctx.send("Training successful!") await ctx.send("Training successful!")
else: else:
await ctx.send("Error occurred :(") await ctx.send("Error occurred :(")
async def on_message(self, message): async def on_message(self, message):
""" """
Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py Credit to https://github.com/Twentysix26/26-Cogs/blob/master/cleverbot/cleverbot.py
for on_message recognition of @bot for on_message recognition of @bot
@ -137,5 +134,3 @@ class Chatter:
if not response: if not response:
response = ":thinking:" response = ":thinking:"
await channel.send(response) await channel.send(response)

Loading…
Cancel
Save