Skip to content

Commit

Permalink
feat: callOneBot
Browse files Browse the repository at this point in the history
支持从脚本直接调用onebot接口
支持直接与脚本交互json
remove authorize
remove DiceEvent::isCalled
  • Loading branch information
mystringEmpty committed Nov 29, 2023
1 parent 181aec8 commit 50a4cb4
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
# A list of files, directories, and wildcard patterns to cache and restore
path: ${{github.workspace}}\vcpkg\vcpkg.exe
# An explicit key for restoring and saving the cache
key: ${{ runner.os }}-${{ matrix.triplet }}-20231006
restore-keys: ${{ runner.os }}-${{ matrix.triplet }}-20231006
key: ${{ runner.os }}-${{ matrix.triplet }}-20231129
restore-keys: ${{ runner.os }}-${{ matrix.triplet }}-20231129

- name: Setup Vcpkg
shell: cmd
Expand Down
50 changes: 3 additions & 47 deletions Dice/DiceEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,53 +711,13 @@ int DiceEvent::MasterSet()

int DiceEvent::BasicOrder()
{
if (strMsg[0] != '.')return 0;
if (strMsg[0] != '/')return 0;
intMsgCnt++;
while (isspace(static_cast<unsigned char>(strMsg[intMsgCnt])))
intMsgCnt++;
//指令匹配
if (console["DebugMode"])console.log("listen:" + strMsg, 0, printSTNow());
if (strLowerMessage.substr(intMsgCnt, 9) == "authorize")
{
intMsgCnt += 9;
readSkipSpace();
if (isPrivate() || strMsg[intMsgCnt] == '+')
{
long long llTarget = readID();
if (llTarget)
{
pGrp = &chat(llTarget);
}
else
{
replyMsg("strGroupIDEmpty");
return 1;
}
}
if (pGrp->isset("许可使用") && !pGrp->isset("未审核") && !pGrp->isset("协议无效"))return 0;
string strInfo = readRest();
if (fmt->call_hook_event(merge({
{"hook","GroupAuthorize"},
{"aimGroup",to_string(pGrp->ID)},
{"AttachInfo",strInfo},
{"aim_gid",pGrp->ID},
})))return 1;
if (trusted > 0)
{
pGrp->set("许可使用").reset("未审核").reset("协议无效");
note("已授权" + printGroup(pGrp->ID) + "许可使用", 1);
AddMsgToQueue(getMsg("strGroupAuthorized", *this), { 0,pGrp->ID });
}
else
{
if (!console["CheckGroupLicense"] && !console["Private"] && !isCalled)return 0;
if (strInfo.empty())console.log(printUser(fromChat.uid) + "申请" + printGroup(pGrp->ID) + "许可使用", 0b10, printSTNow());
else console.log(printUser(fromChat.uid) + "申请" + printGroup(pGrp->ID) + "许可使用;附言:" + strInfo, 0b100, printSTNow());
replyMsg("strGroupLicenseApply");
}
return 1;
}
else if (strLowerMessage.substr(intMsgCnt, 6) == "master")
if (strLowerMessage.substr(intMsgCnt, 6) == "master")
{
intMsgCnt += 6;
if (!console)
Expand All @@ -778,9 +738,6 @@ int DiceEvent::BasicOrder()
else if (trusted > 4 || isFromMaster()) {
return MasterSet();
}
else {
if (isCalled)replyMsg("strNotMaster");
}
return 1;
}
else if (!isPrivate() && pGrp->isset("协议无效")){
Expand Down Expand Up @@ -1333,7 +1290,7 @@ int DiceEvent::BasicOrder()
}

int DiceEvent::InnerOrder() {
if (strMsg[0] != '.')return 0;
if (strMsg[0] != '/')return 0;
if (WordCensor()) {
return 1;
}
Expand Down Expand Up @@ -3843,7 +3800,6 @@ bool DiceEvent::WordCensor() {

void DiceEvent::virtualCall() {
isVirtual = true;
isCalled = true;
DiceFilter();
}
std::optional<string> DiceEvent::getGameRule() {
Expand Down
2 changes: 1 addition & 1 deletion Dice/DiceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DiceEvent : public AttrObject {
bool WordCensor();
void virtualCall();
short trusted = 0;
bool isCalled = true;
//bool isCalled = true;

private:
std::optional<string> getGameRule();
Expand Down
125 changes: 122 additions & 3 deletions Dice/DiceJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ string js_toNativeString(JSContext* ctx, JSValue val) {
JS_FreeCString(ctx, s);
return ret;
}
string js_NativeToGBK(JSContext* ctx, JSValue val) {
string js_NativeToUTF8(JSContext* ctx, JSValue val) {
auto s{ JS_ToCString(ctx, val) };
#ifdef _WIN32
string ret{ s };
string ret{ GBKtoUTF8(s) };
#else
string ret{ UTF8toGBK(s) };
string ret{ s };
#endif
JS_FreeCString(ctx, s);
return ret;
Expand Down Expand Up @@ -176,6 +176,64 @@ AttrVar js_toAttr(JSContext* ctx, JSValue val) {
}
return {};
}
fifo_json js_toJson(JSContext* ctx, JSValue val) {
switch (auto tag{ JS_VALUE_GET_TAG(val) }) {
case JS_TAG_BOOL:
return JS_VALUE_GET_INT(val) != 0;
break;
case JS_TAG_INT:
return JS_VALUE_GET_INT(val);
break;
case JS_TAG_BIG_INT:
return js_toLongLong(ctx, val);
break;
case JS_TAG_BIG_FLOAT:
case JS_TAG_FLOAT64:
if (auto d = js_toDouble(ctx, val); d == (long long)d)return (long long)d;
else return d;
break;
case JS_TAG_STRING:
return js_toUTF8(ctx, val);
break;
case JS_TAG_OBJECT:
if (void* p{ nullptr }; JS_GetClassID(val, &p) == js_dice_context_id) {
return ((AttrObject*)p)->to_json();
}
//else if (void* p{ nullptr }; JS_GetClassID(val, &p) == js_dice_Set_id) {
// return *(AttrSet*)p;
//}
else if (JS_IsArray(ctx, val)) {
fifo_json ary = fifo_json::array();
auto p = JS_VALUE_GET_OBJ(val);
auto len = (uint32_t)JS_VALUE_GET_INT(JS_GetPropertyStr(ctx, val, "length"));
for (uint32_t i = 0; i < len; i++)
ary.emplace_back(js_toJson(ctx, JS_GetPropertyUint32(ctx, val, i)));
return ary;
}
else {
fifo_json obj = fifo_json::object();
JSPropertyEnum* tab{ nullptr };
uint32_t len = 0;
if (!JS_GetOwnPropertyNames(ctx, &tab, &len, val, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK)) {
for (uint32_t i = 0; i < len; ++i) {
obj.emplace(js_AtomtoUTF8(ctx, tab[i].atom), js_toJson(ctx, JS_GetProperty(ctx, val, tab[i].atom)));
}
js_free_prop_enum(ctx, tab, len);
}
return obj;
}
break;
case JS_TAG_UNINITIALIZED:
case JS_TAG_NULL:
case JS_TAG_EXCEPTION:
case JS_TAG_UNDEFINED:
return nullptr;
default:
console.log("js_value_tag:" + to_string(JS_VALUE_GET_TAG(val)), 0);
return nullptr;
}
return nullptr;
}
JSValue js_newAttr(JSContext* ctx, const AttrVar& var) {
switch (var.type) {
case AttrVar::Type::Boolean:
Expand Down Expand Up @@ -230,6 +288,47 @@ JSValue js_newAttr(JSContext* ctx, const AttrVar& var) {
}
return JS_UNDEFINED;
}
JSValue js_newJson(JSContext* ctx, const fifo_json& j) {
switch (j.type()) {
case fifo_json::value_t::boolean:
return JS_NewBool(ctx, j);
break;
case fifo_json::value_t::number_integer:
return JS_NewInt64(ctx, j);
break;
case fifo_json::value_t::number_unsigned:
return JS_NewInt64(ctx, j.get<uint64_t>());
break;
case fifo_json::value_t::number_float:
return JS_NewFloat64(ctx, j);
break;
case fifo_json::value_t::string:
return JS_NewString(ctx, j.get<string>().c_str());
break;
case fifo_json::value_t::array: {
auto ary = JS_NewArray(ctx);
uint32_t idx{ 0 };
for (auto& val : j) {
JS_SetPropertyUint32(ctx, ary, idx++, js_newJson(ctx, val));
}
return ary;
break;
}
case fifo_json::value_t::object: {
auto dict = JS_NewObject(ctx);
for (auto& item : j.items()) {
JS_SetPropertyStr(ctx, dict, item.key().c_str(), js_newJson(ctx, item.value()));
}
return dict;
break;
}
case fifo_json::value_t::null:
default:
return JS_UNDEFINED;
break;
}
return JS_UNDEFINED;
}
string js_context::getException() {
auto e{ JS_GetException(ctx) };
auto err{ js_toUTF8(ctx, e) };
Expand Down Expand Up @@ -687,6 +786,26 @@ QJSDEF(getPlayerCard) {
else JS_ThrowTypeError(ctx, "#1 invalid user id!");
return JS_EXCEPTION;
}
QJSDEF(callOneBot) {
fifo_json param = js_toJson(ctx, argv[0]);
if (!param.is_null() && !param.empty()) {
try {
if (param.count("echo") && !param["echo"].is_null()) {
if (auto ret{ api::getExtra(param) }; !ret.is_null()) {
return ret;
}
}
else {
api::pushExtra(param);
return JS_TRUE;
}
}
catch (std::exception& e) {
console.log("callOneBot抛出异常!" + string(e.what()), 0b10);
}
}
return JS_EXCEPTION;
}
//Context
void js_dice_context_finalizer(JSRuntime* rt, JSValue val) {
JS2OBJ(val);
Expand Down
Loading

0 comments on commit 50a4cb4

Please sign in to comment.