Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Commit

Permalink
Add log of chat & MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
sor committed Mar 9, 2018
1 parent a8dd336 commit a8a854c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
48 changes: 26 additions & 22 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import random
import requests
import configparser
import time
from api import *
from drive import *

Expand Down Expand Up @@ -236,21 +237,9 @@ def bot_chat(bot, update, text, id):

def private(bot, update, chat_data, user_data):
id = update.effective_user.id
status = mod_status(id)
if status == 'trick':
trick(bot, update, chat_data)
update_mod_status(id, 'idle')
return
elif status == '!announce':
announce(bot, update, chat_data, 1)
update_mod_status(id, 'idle')
return
elif status == 'announce':
announce(bot, update, chat_data, 0)
update_mod_status(id, 'idle')
return

text = update.effective_message.text
blog = '(%s, private) %d: %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), id, text)
bot_log.write(blog)
if text == '# trick':
update_mod_status(id, 'trick')
return
Expand All @@ -260,26 +249,39 @@ def private(bot, update, chat_data, user_data):
elif text == '# announce':
update_mod_status(id, 'announce')
return
elif text == '# idle':
update_mod_status(id, 'idle')
return

status = mod_status(id)
if status == 'trick':
trick(bot, update, chat_data)
# update_mod_status(id, 'idle')
return
elif status == '!announce':
announce(bot, update, chat_data, 1)
# update_mod_status(id, 'idle')
return
elif status == 'announce':
announce(bot, update, chat_data, 0)
# update_mod_status(id, 'idle')
return

def group(bot, update, chat_data, user_data):
id = update.effective_user.id
cid = update.message.chat_id
text = update.effective_message.text
if id == cid:
if ( (text[:3] == '```') and (text[-3:] == '```' ) ) and ismod(id):
trick(bot, update, chat_data, user_data)
return
if text[:3] != '幼兔娘':
if ismod(id):
mod(bot, update, chat_data, user_data)
return

if (('女' in text) and ('装' in text)) and ((('索' in text) and ('尔' in text)) or (('群' in text) and ('主' in text))):
bot.delete_message(update.message.chat_id, update.effective_message.message_id);
return
uid = id2uid(id)
if text[:3] != '幼兔娘':
return
blog = '(%s, group) %d: %s' % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), id, text)
print(blog)
bot_log.write(blog + '\n')
bot_log.flush()
if uid == -1:
update.message.reply_text('主人请先告诉幼兔娘UID~')
return
Expand Down Expand Up @@ -315,6 +317,8 @@ def group(bot, update, chat_data, user_data):
signal.signal(signal.SIGINT, dbexit)
signal.signal(signal.SIGTERM, dbexit)

bot_log = open('bot.log', 'a')

conf = configparser.ConfigParser()
conf.read('secret.ini')

Expand Down
17 changes: 13 additions & 4 deletions bot/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
import pymysql
import configparser
import random
import time

conf = configparser.ConfigParser()
conf.read('secret.ini')
dbconf = dict(conf.items('DB'))
db = pymysql.connect(dbconf['host'], dbconf['username'], dbconf['password'], dbconf['database'], charset = 'utf8')
cursor = db.cursor()

sql_log = open('sql.log', 'a')

def execute(sql):
cursor.execute(sql)
slog = '(%s) %s' % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()), sql)
print(slog)
sql_log.write(slog + '\n')
sql_log.flush()
return

def dbexit(sig, frame):
db.close()
print('bye~')
Expand Down Expand Up @@ -72,13 +83,11 @@ def sm(text, mod):

def mod_status(id):
sql = 'select mod_status from `user` where id = %s' % (id)
print(sql)
cursor.execute(sql)
execute(sql)
status = cursor.fetchone()[0]
return status

def update_mod_status(id, status):
sql = 'update `user` set mod_status = \'%s\' where id = %d' % (status, id)
print(sql)
cursor.execute(sql)
execute(sql)
return

0 comments on commit a8a854c

Please sign in to comment.