@kx501/koishi-plugin-red-packet
Version:
175 lines (169 loc) • 7.65 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
inject: () => inject,
log: () => log,
name: () => name,
usage: () => usage
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var name = "red-packet";
var log = new import_koishi.Logger("red-packet");
var inject = ["monetary", "database"];
var usage = `
---
**免责声明**
感谢您使用我们的插件!请您仔细阅读以下条款,以确保您了解并接受我们的政策:
1. **游戏性质**:本插件提供的红包游戏仅供娱乐之用,不涉及任何真实的货币交易或赌博行为。
2. **法律责任**:使用者必须遵守当地法律法规,若因违反相关规定而产生的任何法律后果,均由使用者自行承担。
3. **免责声明更新**:我们保留随时修改本声明的权利,请及时更新插件以获取最新版本的免责声明。**若因未及时更新插件而导致的责任和损失,本方概不负责**。
4. **解释权归属**:本声明的最终解释权归插件开发者所有。
通过使用本插件,即视为**同意上述条款**。请确保您已经仔细阅读并理解以上内容。
---
`;
var Config = import_koishi.Schema.object({});
function apply(ctx) {
ctx.model.extend("red_packet_kx", {
id: "integer",
sender: "char",
amount: "integer",
remainingAmount: "integer",
totalCount: "integer",
grabbedCount: "integer",
grabbedBy: "json",
createdAt: "date",
channelId: "char"
// 新增字段,记录红包所在的频道ID
}, { autoInc: true });
ctx.command("packet <money:integer> <count:integer>", "发送积分手气红包", {
checkArgCount: true
}).alias("发红包").action(async ({ session }, money, count) => {
let userPoints = 0;
const userAid = (await ctx.database.get("binding", { pid: [session.userId] }, ["aid"]))[0]?.aid;
userPoints = (await ctx.database.get("monetary", { uid: userAid }, ["value"]))[0]?.value;
if (userPoints === void 0)
ctx.monetary.gain(userAid, 0);
if (userPoints < money)
return `当前余额不足,你有 ${userPoints} 积分`;
await ctx.monetary.cost(userAid, money);
await ctx.database.create("red_packet_kx", {
sender: session.username,
amount: money,
remainingAmount: money,
totalCount: count,
grabbedCount: 0,
grabbedBy: [],
createdAt: /* @__PURE__ */ new Date(),
channelId: session.channelId
// 记录红包所在的频道ID
});
return `红包发送成功!金额为 ${money} 积分,共 ${count} 个。`;
});
ctx.command("packet").subcommand("snatch", "抢手气红包").alias("抢红包").action(async ({ session }) => {
const userAid = (await ctx.database.get("binding", { pid: [session.userId] }, ["aid"]))[0]?.aid;
const redEnvelope = await ctx.database.get("red_packet_kx", { remainingAmount: { $gt: 0 }, channelId: session.channelId }, ["id", "sender", "remainingAmount", "totalCount", "grabbedCount", "grabbedBy"]);
if (redEnvelope.length === 0)
return "当前没有可抢的红包。";
let randomEnvelope = null;
for (const envelope of redEnvelope) {
if (!envelope.grabbedBy.includes(userAid) && envelope.grabbedCount < envelope.totalCount) {
randomEnvelope = envelope;
break;
}
}
if (!randomEnvelope)
return "当前没有可抢的红包。";
let grabAmount = 0;
if (randomEnvelope.grabbedCount + 1 === randomEnvelope.totalCount)
grabAmount = randomEnvelope.remainingAmount;
else
grabAmount = import_koishi.Random.int(1, randomEnvelope.remainingAmount);
await ctx.database.set("red_packet_kx", randomEnvelope.id, {
remainingAmount: randomEnvelope.remainingAmount - grabAmount,
grabbedCount: randomEnvelope.grabbedCount + 1,
grabbedBy: [...randomEnvelope.grabbedBy, userAid]
});
if (randomEnvelope.remainingAmount - grabAmount <= 0 || randomEnvelope.grabbedCount + 1 >= randomEnvelope.totalCount) {
await ctx.database.remove("red_packet_kx", { id: randomEnvelope.id });
}
await ctx.monetary.gain(userAid, grabAmount);
return `恭喜你从【${randomEnvelope.sender}】发送的红包中抢到 ${grabAmount} 积分!`;
});
ctx.command("packet").subcommand("list", "查询当前群聊可抢红包列表").alias("红包列表").action(async ({ session }) => {
const redEnvelopes = await ctx.database.get("red_packet_kx", { remainingAmount: { $gt: 0 }, channelId: session.channelId }, ["id", "sender", "totalCount", "grabbedCount", "remainingAmount", "createdAt"]);
if (redEnvelopes.length === 0)
return "当前没有可抢的红包。";
let response = "当前可抢的红包列表:\n";
const now = /* @__PURE__ */ new Date();
for (const envelope of redEnvelopes) {
const createdAt = new Date(envelope.createdAt);
const diffInDays = (now.getTime() - createdAt.getTime()) / (1e3 * 60 * 60 * 24);
if (diffInDays > 7) {
const senderAid = (await ctx.database.get("binding", { pid: [envelope.sender] }, ["aid"]))[0]?.aid;
if (senderAid) {
await ctx.monetary.gain(senderAid, envelope.remainingAmount);
}
await ctx.database.remove("red_packet_kx", { id: envelope.id });
} else
response += `【发送者】${envelope.sender},
【剩余个数】${envelope.totalCount - envelope.grabbedCount}
`;
}
return response;
});
ctx.command("balance", "查询当前积分").alias("查询积分").option("userid", "-u [char] 查询@指定用户积分").action(async ({ session, options }) => {
let id = options?.userid;
const atElements = import_koishi.h.select(session?.elements, "at");
if (!id)
id = session.userId;
else if (atElements?.length) {
if (atElements.length === 1 && atElements[0]?.attrs?.id !== session.selfId)
id = atElements[0].attrs.id;
else if (atElements.length > 1)
id = atElements[1].attrs.id;
}
log.debug(`查询积分的用户ID: ${id}`);
const userAid = (await ctx.database.get("binding", { pid: [id] }, ["aid"]))[0]?.aid;
let userPoints = (await ctx.database.get("monetary", { uid: [userAid] }, ["value"]))[0]?.value;
log.debug(`查询积分的用户AID: ${userAid}`);
if (userAid !== void 0) {
if (userPoints === void 0) {
userPoints = 0;
await ctx.monetary.gain(userAid, 0);
}
return `当前积分为 ${userPoints}。`;
} else
return "当前用户不存在";
});
}
__name(apply, "apply");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
inject,
log,
name,
usage
});