猫史档案馆


【教程】如何让管喵在聊天区执行代码时可以用await

用户:145a145a查看:1 回复:5 评论:1 创建时间:2022-07-31T17:32:20


const adminList=["这里是管喵列表,填名字"];world.onChat(async({entity:{player:{name:name}},message:message})=>{if(message.startsWith("$")&&adminList.includes(name)){let c=message.slice(1);world.say(`~> ${c}`);try{c=await eval(`!async function(){${c}}`)}catch(a){c=a}finally{world.say(`<~ ${c}`)}}});


回复

上一页1 页 / 共 1下一页
治愈绾兮治愈绾兮

额,直接在onChat函数加上anscy不是更好……

点赞0


评论


啾咪啾咪!啾咪啾咪!

const adminList=["这里是管喵列表,填名字"];
world.onChat(async({entity:{player:{name:name}},message:message})=>{
    if(message.startsWith("$")&&adminList.includes(name)){
        let c=message.slice(1);
        world.say(`~> ${c}`);
        try{
            c=await eval(`!async function(){${c}}`)
        }
        catch(a){
            c=a
        }
        finally{
            world.say(`<~ ${c}`)
        }
    }
});

点赞0


评论


蓝矮爱乐蓝矮爱乐

你这有bug……

点赞1


评论


145a145a

这个代码有bug,请不要使用

点赞0


评论


七式草莓七式草莓

请注意执行的代码不能为多条语句(即包含分号),否则会出bug

const adminList = ["这里是管喵列表,填名字"];
world.onChat(async ({ entity, message }) => {
    if (message.startsWith("$") && adminList.includes(entity.player.name)) {
        var result = message.slice(1)
        var text = ''
        var error = false
        if (result.includes('await')) {
            try {
                text = await eval(`(async() => {return ${result}})()`)
            } catch (err) {
                if (err.name == 'SyntaxError') {
                    try {
                        text = await eval(`(async() => {${result}})()`)
                    } catch (err) {
                        text = err, error = true
                    }
                } else {
                    text = err, error = true
                }
            }
        } else {
            try {
                text = eval(`${result}`)
            } catch (err) {
                text = err, error = true
            }
        }
        try {
            text = text.valueOf()
        } catch (err) { }
        if (typeof text == 'object' && !error) {
            try {
                text = JSON.stringify(text)
            } catch (err) { }
        }
        world.say('<~ ' + text)
    }
});

点赞0


评论