From 305bfff5162614db9efa0f648dea2a20c92a2b45 Mon Sep 17 00:00:00 2001 From: NodeStorm <36058792+QLG1@users.noreply.github.com> Date: Sat, 15 Aug 2020 21:00:20 +0300 Subject: [PATCH] first --- bot.py | 32 ++++++++++++++++++++++++++++++++ cogs/basic.py | 14 ++++++++++++++ cogs/lydia.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ setup.sh | 8 ++++++++ start.sh | 1 + 5 files changed, 105 insertions(+) create mode 100644 bot.py create mode 100644 cogs/basic.py create mode 100644 cogs/lydia.py create mode 100644 setup.sh create mode 100644 start.sh diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..5880151 --- /dev/null +++ b/bot.py @@ -0,0 +1,32 @@ +"""https://discord.com/oauth2/authorize?client_id=605758711071506432&permissions=67119104&scope=bot""" + +import discord +from discord.ext import commands +import os + +client = commands.Bot(command_prefix = "$") + + +@client.command() +@commands.is_owner() +async def load(ctx, extension): + client.load_extension(f'cogs.{extension}') + +@client.command() +@commands.is_owner() +async def unload(ctx, extension): + client.unload_extension(f'cogs.{extension}') + +@client.command() +@commands.is_owner() +async def reload(ctx, extension): + client.unload_extension(f'cogs.{extension}') + client.load_extension(f'cogs.{extension}') + + +for filename in os.listdir("./cogs"): + if filename.endswith('.py'): + client.load_extension(f'cogs.{filename[:-3]}') + + +client.run('NjA1NzU4NzExMDcxNTA2NDMy.XUBKww.sI5-DZzdCCXdgW44uEACqGzaVpc') diff --git a/cogs/basic.py b/cogs/basic.py new file mode 100644 index 0000000..bc29e91 --- /dev/null +++ b/cogs/basic.py @@ -0,0 +1,14 @@ +import discord +from discord.ext import commands + +class Basic(commands.Cog): + + def __init__(self,client): + self.client = client + + @commands.command() + async def ping(self, ctx): + await ctx.send("pong!") + +def setup(client): + client.add_cog(Basic(client)) diff --git a/cogs/lydia.py b/cogs/lydia.py new file mode 100644 index 0000000..b5a5e07 --- /dev/null +++ b/cogs/lydia.py @@ -0,0 +1,50 @@ +import discord +from discord.ext import commands +import sqlite3 +from coffeehouse.lydia import LydiaAI +import time + +class Lydia(commands.Cog): + + def __init__(self,client): + self.lydia = LydiaAI("69e6f26aba27d05e14c6a48e38008bc0794e24c1d25591db00575e9ce93c577ca934724981a540c319c7da3209ca4c0f910098c14ad6b5e27902c346c9f5cf7f") + self.client = client + self.conn = sqlite3.connect('./lydia_sessions.sqlite3') + self.cur = self.conn.cursor() + + @commands.command(aliases=["lydia"]) + async def ai(self, ctx, *, quest): + self.cur.execute(f'SELECT * FROM sessions WHERE user_id="{ctx.author.id}"') + query = self.cur.fetchone() + + if query == None: + session = self.lydia.create_session() + self.cur.execute(f"INSERT INTO sessions VALUES ('{ctx.author.id}','{session.id}','{session.expires}')") + self.conn.commit() + + await ctx.send(session.think_thought(quest)) + else: + #give it a 50 second margin to avoid possible errors + if query[2]-50 < int(time.time()): + self.cur.execute(f'DELETE FROM sessions WHERE session_id = "{query[1]}"') + session = self.lydia.create_session() + self.cur.execute(f"INSERT INTO sessions VALUES ('{ctx.author.id}','{session.id}','{session.expires}')") + self.conn.commit() + + + reply = session.think_thought(quest) + await ctx.send(reply) + #or alternatively + #await ctx.send(session.think_thought(quest)) + else: + reply = self.lydia.think_thought(query[1], quest) + await ctx.send(reply) + #or alternatively + #await ctx.send(session.think_thought(quest)) + + #remember to comment this out if you use the alternatives above + print(f"[<]{ctx.message.author}: {quest}\n[>]{reply}") + + +def setup(client): + client.add_cog(Lydia(client)) diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..e248779 --- /dev/null +++ b/setup.sh @@ -0,0 +1,8 @@ +apt-get install python3 +apt-get install python3-distutils +apt-get install wget +wget https://bootstrap.pypa.io/get-pip.py +python3 get-pip.py +python3 -m pip install coffeehouse +python3 -m pip install discord.py +python3 -m pip install sqlite3 diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..43c1287 --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +python3 bot.py