final? fix of psql command, some spam avoidance updates

parent e9704ba0
......@@ -5,7 +5,8 @@ blacklist.json
config.json
tags.json
whitelist.json
snake.log
*.log
*.txt
info/
cogs/__pycache__/
cogs/__init__.py
......
......@@ -70,7 +70,6 @@ class Debug:
try:
cmd = functools.partial(self.bot.db.engine.execute, sql_command)
results = await self.bot.loop.run_in_executor(None, cmd)
print("passed")
except sqlalchemy.exc.ProgrammingError:
await self.bot.send_message(ctx.message.channel, "Unable to process statement. Double check your query:\n```sql\n{}\n```".format(sql_command))
......@@ -85,11 +84,15 @@ class Debug:
result = "Query returned 0 rows"
else:
row_names = results.keys()
result_list = results.fetchall()
clr = lambda arr: ", ".join(repr(item) for item in arr)
result = ("```py\n--> {} rows <--\n{}\n```".format(len(result_list), "\n".join(clr(arg) for arg in result_list)))[:1950]
result = ("```py\n{1}\n--> {0} rows <--\n{2}\n```".format(
len(result_list),
", ".join(row_names),
"\n".join(clr(arg) for arg in result_list))[:1900]
)
print(result)
await self.bot.send_message(ctx.message.channel, result)
@commands.command(name='run', pass_context=True, brief="exec")
......
......@@ -76,20 +76,18 @@ class Message(Base):
__tablename__ = "chat_logs"
id = Column(BigInteger, primary_key=True)
pk = Column(Integer, primary_key=True)
id = Column(BigInteger)
timestamp = Column(String)
author_id = Column(BigInteger, ForeignKey("users.id"))
author = relationship("User", back_populates="messages")
channel_id = Column(BigInteger)
server_id = Column(BigInteger)
edit_timestamp = Column(String)
content = Column(String(2000))
edit_content = Column(String(2000))
edited = Column(Boolean)
deleted = Column(Boolean)
action = Column(String(15))
def __repr__(self):
return "<Message(id={0.id}, timestamp='{0.timestamp}', author_id={0.author_id}, channel_id={0.channel_id}, server_id={0.server_id}, edit_timestamp='{0.edit_timestamp}', edited={0.edited}, deleted={0.deleted})>".format(self)
return "<Message(id={0.id}, timestamp='{0.timestamp}', author_id={0.author_id}, channel_id={0.channel_id}, server_id={0.server_id}, action='{0.action}')>".format(self)
class CommandStat(Base):
......
......@@ -25,14 +25,16 @@ SOFTWARE.
"""
# Imports
import discord, asyncio, os, logging, sys, traceback, aiohttp, json, sqlalchemy, websockets
import discord, asyncio, os, logging, sys, traceback, aiohttp, json, websockets, functools
from contextlib import contextmanager
from random import choice as rand_choice
from bs4 import BeautifulSoup as b_soup
from discord.ext import commands
from datetime import datetime
import sqlalchemy
from cogs.utils import config, time, checks
from cogs.utils import time, checks
from cogs.utils import sql
from cogs.utils.colors import paint, back, attr
......@@ -109,7 +111,7 @@ class SnakeBot(commands.Bot):
def __init__(self, *args, **kwargs):
self.config = self._read_config("config.json")
self._DEBUG = any("debug" in arg for arg in sys.argv)
self.credentials = config.Config("credentials.json")
self.credentials = self._read_config("credentials.json")
self.loop = asyncio.get_event_loop()
self.shard_ws = None
......@@ -123,7 +125,7 @@ class SnakeBot(commands.Bot):
self.log = logging.getLogger()
self.log.setLevel(logging.INFO)
self.log.setLevel(logging.DEBUG)
self.log.addHandler(
logging.FileHandler(filename="snake_shard{}.log".format(self.shard_id), encoding="utf-8", mode='w')
)
......@@ -140,6 +142,23 @@ class SnakeBot(commands.Bot):
finally:
session.close()
def log_message(self, message, action):
author = message.author
channel = message.channel
server = channel.server
#print("SEARCHING FOR AUTHOR {0!r} {0!s} {0}".format(author))
with self.session_scope() as session:
sql_author = session.query(sql.User).filter_by(id=int(author.id)).first()
#print("SQL AUTHOR {0!r} {0!s} {0}".format(sql_author))
if sql_author is None:
sql_author = sql.User(id=int(author.id), name=author.name, nick=author.nick, bot=author.bot, discrim=author.discriminator)
session.add(sql_author)
new_message = sql.Message(id=int(message.id), timestamp=message.timestamp.strftime(self.config.get("msg_strftime")), author_id=int(author.id), author=sql_author, channel_id=int(channel.id), server_id=int(server.id), content=message.content, action=action)
session.add(new_message)
def get_prefix(self, bot, message):
return "snake "
......@@ -156,8 +175,8 @@ class SnakeBot(commands.Bot):
def _log_shard(self, event_name, op, unknown=False):
if unknown:
print("Unknown op {} on shard #{}".format(op, self.shard_id))
else:
print("{} on shard #{}".format(self._color(event_name, op), self.shard_id))
#else:
#print("{} on shard #{}".format(self._color(event_name, op), self.shard_id))
async def _run_shard_event(self, event, *args, **kwargs):
try:
......@@ -379,21 +398,24 @@ class SnakeBot(commands.Bot):
self.log.info("{1}: {0.author.name}: {0.clean_content}".format(message, destination))
async def on_message(self, message):
channel = message.channel
author = message.author
if (not channel.is_private) and isinstance(author, discord.Member):
with self.session_scope() as session:
sql_author = session.query(sql.User).filter_by(id=int(author.id)).first()
if sql_author is None:
sql_author = sql.User(id=int(author.id), name=author.name, nick=author.nick, bot=author.bot, discrim=author.discriminator)
await self.loop.run_in_executor(None, functools.partial(self.log_message, message, "create"))
await self.process_commands(message)
new_message = sql.Message(id=int(message.id), timestamp=message.timestamp.strftime(self.config.get("msg_strftime")), author_id=int(author.id), author=sql_author, channel_id=int(channel.id), server_id=int(message.server.id), content=message.content, edited=False, deleted=False)
async def on_message_delete(self, message):
channel = message.channel
author = message.author
if channel.is_private is False and isinstance(author, discord.Member):
await self.loop.run_in_executor(None, functools.partial(self.log_message, message, "delete"))
session.add(sql_author)
session.add(new_message)
# async def on_message_edit(self, msg_1, message):
# channel = message.channel
# author = message.author
# if channel.is_private is False and isinstance(author, discord.Member):
# await self.loop.run_in_executor(None, functools.partial(self.log_message, message, "edit"))
await self.process_commands(message)
# more class functions here
......
......@@ -38,6 +38,8 @@ class ShardBroker:
args.append("debug")
shard_process = psutil.Popen(args, stdout=sys.stdout)
self.shard_processes[shard_id] = shard_process
print("Started shard {}/{} pid {} [{}]".format(shard_id, self.shard_limit - 1, shard_process.pid, shard_process.cmdline()))
print("Broker created!")
......@@ -51,8 +53,8 @@ class ShardBroker:
def _log_shard(self, event_name, ws, op, unknown=False):
if unknown:
print("Unknown op {} from shard #{}".format(op, ws.shard_id))
else:
print("{} from shard #{}".format(self._color(event_name, op), ws.shard_id))
#else:
#print("{} from shard #{}".format(self._color(event_name, op), ws.shard_id))
async def _run_event(self, event, ws, *args, **kwargs):
try:
......@@ -79,6 +81,27 @@ class ShardBroker:
print("Ignoring exception in {} [shard #{}]".format(event_method, ws.shard_id), file=sys.stderr)
traceback.print_exc()
def restart_shard(self, ws):
shard_id = ws.shard_id
try:
asyncio.ensure_future(ws.close(code=1004, reason="closing"))
if self.shard_processes[ws.shard_id].is_running():
self.shard_processes[ws.shard_id].kill() # Kill the shard process
del self.shards[ws.shard_id]
except:
pass
args = ["python" if self.platform == "win32" else "python3.5", "new_snake.py", str(shard_id)]
if self._DEBUG:
args.append("debug")
shard_process = psutil.Popen(args, stdout=sys.stdout)
self.shard_processes[shard_id] = shard_process
print("Restarted shard {}/{} pid {} [{}]".format(shard_id, self.shard_limit - 1, shard_process.pid, shard_process.cmdline()))
def close_shard(self, ws):
try:
asyncio.ensure_future(ws.close(code=1004, reason="closing"))
......@@ -112,8 +135,8 @@ class ShardBroker:
try:
await ws.send(data)
except websockets.exceptions.ConnectionClosed:
print("Shard #{} closed, removing.".format(ws.shard_id))
self.close_shard(ws)
print("Shard #{} closed, restarting.".format(ws.shard_id))
self.restart_shard(ws)
def _decode(self, raw_data):
data = None
......@@ -136,10 +159,21 @@ class ShardBroker:
payload = {"op": self.PING}
try:
await self.send(payload, ws)
status = self.shard_processes[shard_id].is_running()
if not status:
raise TypeError("it ded jimes")
ws.temp_ping = datetime.now()
except websockets.exceptions.ConnectionClosed:
print("Shard #{} closed, removing.".format(ws.shard_id))
self.close_shard(ws)
print("Shard #{} closed, restarting.".format(ws.shard_id))
self.restart_shard(ws)
except psutil.NoSuchProcess:
print("Shard #{} closed, restarting.".format(ws.shard_id))
self.restart_shard(ws)
except TypeError:
print("Shard #{} closed, restarting.".format(ws.shard_id))
self.restart_shard(ws)
await asyncio.sleep(10)
async def poll_event(self, ws):
......@@ -147,8 +181,8 @@ class ShardBroker:
data = await ws.recv()
await self.process_message(ws, data)
except websockets.exceptions.ConnectionClosed:
print("Shard #{} closed, removing.".format(ws.shard_id))
self.close_shard(ws)
print("Shard #{} closed, restarting.".format(ws.shard_id))
self.restart_shard(ws)
async def process_message(self, ws, message):
self.dispatch("raw_data", ws, message)
......@@ -190,7 +224,7 @@ class ShardBroker:
if hasattr(ws, "temp_ping"):
ws.temp_pong = datetime.strptime(data.get("reply"), "%H-%M-%S:%f")
ws.ping = self._get_ping(ws.temp_pong, datetime.now())
print("Shard #{} responded from ping with response time of {}ms".format(ws.shard_id, ws.ping))
#print("Shard #{} responded from ping with response time of {}ms".format(ws.shard_id, ws.ping))
del ws.temp_pong
del ws.temp_ping
......@@ -281,7 +315,7 @@ class ShardBroker:
await self.poll_event(ws)
except websockets.exceptions.ConnectionClosed as e:
print("Shard #{} closed [{}]".format(ws.shard_id, e.reason))
self.close_shard(ws)
self.restart_shard(ws)
async def on_data(self, ws, *args, **kwargs):
print(args, kwargs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment