1 Star 0 Fork 13

masquerade/wechat-robot

forked from 开源社/wechat-robot 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
db-utils.js 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
David Zhang 提交于 2020-01-17 21:52 +08:00 . add code from server
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'localhost',
user: 'kys',
password: 'kys',
database: 'kaiyuanshe'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log("Connected to Kaiyuanshe DB");
});
global.db = db;
exports.get_user = function (nick_name, func) {
if (nick_name.slice(0, 1) == "@") {
nick_name = nick_name.slice(1);
}
let query = "select * from users where `nick_name`='" + nick_name + "'";
console.log(query);
db.query(query, (err, result) => {
if (!err) {
func(result[0]);
} else {
func(null);
}
});
}
exports.update_user_status = function (wechat_id, user_status, func) {
let query = "update `users` set `status`='" + user_status + "' where `wechat_id`='" + wechat_id + "'";
db.query(query, (err, result) => {
if (func) { func(); }
});
}
exports.update_user_position = function (wechat_id, position, func) {
let query = "update `users` set `position`='" + position + "' where `wechat_id`='" + wechat_id + "'";
db.query(query, (err, result) => {
if (func) { func(); }
});
}
exports.save_wechat_friend = async function (user) {
var wechat_id = user.id;
var nick_name = await user.name();
console.log(wechat_id + "," + nick_name);
db.query("SET NAMES utf8mb4", (err, result) => {
db.query("select * from wechat_friends where wechat_id='"+wechat_id+"'", (err1, result) => {
if(result.length==0){
db.query("insert into `wechat_friends` (wechat_id,nick_name) values ('" + wechat_id + "','" + nick_name + "')", (err2, result2) => {
if (err2) {
console.log(err2);
}
});
}
});
});
}
const MessageType = ["Unknown", "Attachment", "Audio", "Contact", "Emoticon", "Image", "Text", "Video", "Url"];
exports.save_msg = async function (msg) {
var fields = "`type`,";
var values = "'" + MessageType[msg.type()-1] + "',";
var room = await msg.room();
if (room) {
var room_id = room.id;
var room_topic = await room.topic();
fields = fields + "`room_id`,`room_topic`,";
room_topic = room_topic.replace(/\'/g, "\\\'");
values = values + "'" + room_id + "','" + room_topic + "',";
}
var from = await msg.from();
if (from) {
var from_user_id = from.id;
var from_user_name = await from.name();
fields = fields + "`from_user_id`,`from_user_name`,";
values = values + "'" + from_user_id + "','" + from_user_name + "',";
}
var mention_list = await msg.mention();
if (mention_list) {
var mention_id_list = "";
var mention_name_list = "";
mention_list.forEach(async function (item, index) {
mention_id_list = mention_id_list + item.id + ",";
mention_name_list = mention_name_list + await item.name() + ",";
});
fields = fields + "`mention_id_list`,`mention_name_list`,";
values = values + "'" + mention_id_list + "','" + mention_name_list + "',";
}
text = await msg.text();
text = text.replace(/\'/g,"\\\'");
fields = fields + "`text`,`create_at`";
values = values + "'" + text + "',CURRENT_TIMESTAMP";
sql = "INSERT INTO `messages` (" + fields + ") VALUES (" + values + ")";
db.query("SET NAMES utf8mb4", (err, result) => {
db.query(sql);
});
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/masqueradeone/wechat-robot.git
git@gitee.com:masqueradeone/wechat-robot.git
masqueradeone
wechat-robot
wechat-robot
master

搜索帮助