跳到正文

个人记录

pbb bot 写作基本教学

lightup37约 9 分钟
原文
文章信息与版本

文章信息

发布
2020年11月13日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

api

请使用下面这个模板. 在这个模板中, 笔者给出了四个接口 : pbbSend(str), pbbLoad(page), pbbIdDel(id), pbbStrDel(str). 这四个接口的作用是发送犇犇, 查询某页的犇犇, 按 id 删除某条犇犇, 按内容删除某条犇犇(需要完全匹配).

(bot.html)

HTML
<html>
	<head>
		<meta charset="utf-8">
		<title> Bot by tyqtyq </title>
	</head>
	<body>
		<script src="https://cdn.luogu.com.cn/js/jquery-2.1.1.min.js" charset="utf-8"></script>
		<script>
			// pbb api
			var TOKEN = "";
			const request = ({ url, data, method }) => new Promise((resolve, reject) => {
				$.ajax({
					type: method,
					data: data,
					url: url,
					dataType: 'json',
					xhrFields: { withCredentials: false },
					success: resolve,
					error: reject
				});
			});

			function pbbSend(str) {
		  	request({
		  		url: "https://pbb.akioi.ml/api/feed/n0YwNhpd/post",
		    	data: { content: str, token: TOKEN },
		    	method: "POST"
		  	}).then(resp => {
		    	if (resp.status !== 200) {
		    		alert(resp.data);
			  	}
				});
			}

			function pbbLoad(pbbPage) {
				var resp = request({
					url: "https://pbb.akioi.ml/api/feed/n0YwNhpd/",
					data: {
						page: pbbPage,
						token: TOKEN
					},
					method: 'GET'
				});
				return resp;
			}
			function getMd(resp) {
				var ret=new Array();
				for(var i=0; i<resp.data.length; ++i) {
					ret.push(resp.data[i].content_markdown);
				}
				return ret;
			}
			function pbbIdDel(id) {
				request({
					url: 'https://pbb.akioi.ml/api/feed/delete',
					data: {
						id: id,
						token: TOKEN
					},
					method: 'POST'
				});
			}
			function pbbStrDel(str) {
				// 删除最近发送的 str 犇犇
				var brk=0;
				for(var i=1; i<=10; ++i) {
					pbbLoad(i).then( resp => {
							if(typeof resp == 'undefined') { brk=1; }
							for(var j=0; j<resp.data.length; ++j) {
								if(resp.data[j].content_markdown == str) {
									idDel(resp.data[j].id);
									brk=1;
								}
								if(brk) break;
							}
						}
					);
					if(brk) break;
				}
			}
			// thanks tiger0132 for providing help during the writing of this program
			// bot
			// send
			// pbbSend("test");
			// query
			// pbbLoad(1).then( resp => { alert(getMd(resp)[0]); }, () => alert("failed"));
		</script>
		
	</body>
</html>

限制

  • 10s 内最多进行 20 次 api 调用, 超出会有 500ms 延迟.

  • 建议在 5s 以上进行查询.

实例

setu-bot

HTML
<script>
    var finid = new Array();
    function seler(dat, num) {
        for(var i=0; i<dat.length; ++i) if(dat[i] == num) return 1;
        return 0;
    }
    function chkresp(resp) {
        var count=0;
        for(var i=0; i<resp.data.length; ++i) {
            if(resp.data[i].content_markdown.indexOf("@setu-bot : setu")>=0 && !seler(finid, resp.data[i].id)) {
                ++count;
                console.log("Solve " + resp.data[i].id.toString());
                finid.push(resp.data[i].id);
            }
        }
        console.log("Count = " + count.toString());
        console.log(finid);
        console.log(resp);
        return count;
    }
    var picList = [
        "https://cdn.luogu.com.cn/upload/image_hosting/mpwjpynt.png",
        "https://cdn.luogu.com.cn/upload/image_hosting/8nwv47mx.png",
        "https://cdn.luogu.com.cn/upload/image_hosting/7w6xthoz.png",
        "https://cdn.luogu.com.cn/upload/image_hosting/yd6sqe87.png",
        "https://cdn.luogu.com.cn/upload/image_hosting/8x5uq0dr.png"
    ];
    function getsetu() {
        return "![](" + picList[ Math.floor((Math.random()*(picList.length))) ] + ")";
    }
    function sendsetu(times) {
        if(times==0) return 0;
        else {
            pbbSend(getsetu());
            setTimeout(function(){sendsetu(times-1)}, 1000);
        }
    }
    var pbbListenServiceStarted;
    function pbbStart() {
        pbbListenServiceStarted = 1;
    }
    function pbbClose() {
        pbbListenServiceStarted = 0;
    }
    function pbbListen() {
        console.log("Start listening"); pbbListenServiceStarted = 0;
        setInterval(function(){ if(pbbListenServiceStarted == 1) { pbbLoad(1).then(resp => { sendsetu(chkresp(resp)) });} }, 20000);
    }
    pbbListen();
</script>

<button onclick="pbbStart()">Start</button>
<button onclick="pbbClose()">Close</button>

如果您想阅读这份代码

箭头函数

形如 (arg1, arg2, …) => { … } 的函数

例如下面这两个函数是大致等价的.

HTML
x => { return x*x; }
function(x){ return x*x; }

.then 语句

这个语句将让您可以再执行一个操作后执行另一个操作. 这里推荐使用 api 中的 request. 语法如下面的代码.

代码
request({
    url: "your-url"
    data: { token: "your-token" }
    method: "your-method"
}).then(resp => { /*Do something to resp*/ })

console.log 语句

这个语句将让您能够在控制台输出变量或结构体.

代码
var tigers = ["0133", "0132", "2005"];
console.log(tigers);
// Get: Array(3) [ "0133", "0132", "2005" ]
console.log(tigers[0]);
// Get: 0133
原文
存档评论2 条抓取于