2022-07-29 01:42:35 +08:00
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
|
|
export async function sleepAsync(sleepms) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve();
|
|
|
|
|
}, sleepms)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-12 16:51:51 +08:00
|
|
|
|
export async function randomSleepAsync(end) {
|
2022-10-17 20:55:39 +08:00
|
|
|
|
let sleep = 4 * 1000 + _.random((end || 5) * 1000);
|
2022-07-29 01:42:35 +08:00
|
|
|
|
await sleepAsync(sleep);
|
|
|
|
|
}
|
2022-10-14 10:02:45 +08:00
|
|
|
|
export function randomString(length, os = false) {
|
2022-09-05 11:49:06 +08:00
|
|
|
|
let randomStr = '';
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2022-10-14 10:02:45 +08:00
|
|
|
|
randomStr += _.sample(os ? '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' :
|
|
|
|
|
'abcdefghijklmnopqrstuvwxyz0123456789');
|
2022-09-05 11:49:06 +08:00
|
|
|
|
}
|
|
|
|
|
return randomStr;
|
2022-07-29 01:42:35 +08:00
|
|
|
|
}
|
2022-10-13 10:47:13 +08:00
|
|
|
|
|
2022-10-14 10:02:45 +08:00
|
|
|
|
export async function redisGet(userId, type = 'bbs') {
|
|
|
|
|
await redis.get(`xiaoyao:${type}:${userId}`);
|
2022-10-13 10:47:13 +08:00
|
|
|
|
}
|
2022-10-14 10:02:45 +08:00
|
|
|
|
export async function redisSet(userId="all", type = 'bbs', data, time=0) {
|
2022-10-13 10:47:13 +08:00
|
|
|
|
var time = moment(Date.now()).add('days', 1).format('YYYY-MM-DD 00:00:00')
|
2022-10-14 10:02:45 +08:00
|
|
|
|
var new_date = (new Date(time).getTime() - new Date().getTime()) / 1000 //获取隔天凌晨的时间差
|
|
|
|
|
if (time!==0) {
|
|
|
|
|
new_date = time
|
|
|
|
|
}
|
|
|
|
|
await redis.set(`xiaoyao:${type}:${userId}`, JSON.stringify(data), {
|
|
|
|
|
EX: parseInt(new_date)
|
|
|
|
|
});
|
2022-10-13 10:47:13 +08:00
|
|
|
|
}
|
2022-10-17 20:55:39 +08:00
|
|
|
|
|
2022-09-05 11:49:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* 发送私聊消息,仅给好友发送
|
|
|
|
|
* @param user_id qq号
|
|
|
|
|
* @param msg 消息
|
|
|
|
|
*/
|
|
|
|
|
export async function relpyPrivate(userId, msg) {
|
|
|
|
|
userId = Number(userId)
|
|
|
|
|
let friend = Bot.fl.get(userId)
|
|
|
|
|
if (friend) {
|
|
|
|
|
Bot.logger.mark(`发送好友消息[${friend.nickname}](${userId})`)
|
|
|
|
|
return await Bot.pickUser(userId).sendMsg(msg).catch((err) => {
|
|
|
|
|
Bot.logger.mark(err)
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-08-01 23:13:34 +08:00
|
|
|
|
}
|
2022-09-05 11:49:06 +08:00
|
|
|
|
export async function replyMake(e, _msg, lenght) {
|
2022-09-03 09:57:59 +08:00
|
|
|
|
let nickname = Bot.nickname;
|
|
|
|
|
if (e.isGroup) {
|
|
|
|
|
let info = await Bot.getGroupMemberInfo(e.group_id, Bot.uin)
|
|
|
|
|
nickname = info.card || info.nickname
|
|
|
|
|
}
|
2022-09-05 11:49:06 +08:00
|
|
|
|
let msgList = [];
|
|
|
|
|
for (let [index, item] of Object.entries(_msg)) {
|
|
|
|
|
if (index < lenght) {
|
2022-09-03 09:57:59 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
msgList.push({
|
|
|
|
|
message: item,
|
|
|
|
|
nickname: nickname,
|
|
|
|
|
user_id: Bot.uin
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-10-14 10:02:45 +08:00
|
|
|
|
if (e._reply) {
|
2022-09-09 10:03:17 +08:00
|
|
|
|
e._reply(await Bot.makeForwardMsg(msgList));
|
2022-10-14 10:02:45 +08:00
|
|
|
|
} else {
|
2022-09-09 10:03:17 +08:00
|
|
|
|
e.reply(await Bot.makeForwardMsg(msgList));
|
|
|
|
|
}
|
2022-09-03 09:57:59 +08:00
|
|
|
|
}
|
2022-10-06 14:10:50 +08:00
|
|
|
|
|
2022-10-14 10:02:45 +08:00
|
|
|
|
export function getServer(uid) {
|
2022-10-06 14:10:50 +08:00
|
|
|
|
switch (String(uid)[0]) {
|
|
|
|
|
case '1':
|
|
|
|
|
case '2':
|
|
|
|
|
return 'cn_gf01' // 官服
|
|
|
|
|
case '5':
|
|
|
|
|
return 'cn_qd01' // B服
|
|
|
|
|
case '6':
|
|
|
|
|
return 'os_usa' // 美服
|
|
|
|
|
case '7':
|
|
|
|
|
return 'os_euro' // 欧服
|
|
|
|
|
case '8':
|
|
|
|
|
return 'os_asia' // 亚服
|
|
|
|
|
case '9':
|
|
|
|
|
return 'os_cht' // 港澳台服
|
|
|
|
|
}
|
|
|
|
|
return 'cn_gf01'
|
|
|
|
|
}
|
2022-09-05 11:49:06 +08:00
|
|
|
|
export async function getCookieMap(cookie) {
|
|
|
|
|
let cookieArray = cookie.replace(/\s*/g, "").split(";");
|
|
|
|
|
let cookieMap = new Map();
|
|
|
|
|
for (let item of cookieArray) {
|
|
|
|
|
let entry = item.split("=");
|
|
|
|
|
if (!entry[0]) continue;
|
|
|
|
|
cookieMap.set(entry[0], entry[1]);
|
|
|
|
|
}
|
2022-10-14 10:02:45 +08:00
|
|
|
|
return cookieMap || {};
|
2022-09-05 11:49:06 +08:00
|
|
|
|
}
|
2022-07-29 01:42:35 +08:00
|
|
|
|
export default {
|
2022-10-14 10:02:45 +08:00
|
|
|
|
sleepAsync,
|
|
|
|
|
getServer,
|
2022-09-05 11:49:06 +08:00
|
|
|
|
randomSleepAsync,
|
|
|
|
|
replyMake,
|
2022-10-14 10:02:45 +08:00
|
|
|
|
randomString,
|
|
|
|
|
redisGet,
|
|
|
|
|
redisSet,
|
2022-09-05 11:49:06 +08:00
|
|
|
|
relpyPrivate,
|
|
|
|
|
getCookieMap
|
2022-07-29 01:42:35 +08:00
|
|
|
|
}
|