Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmod-midori/4967479 to your computer and use it in GitHub Desktop.
Save kmod-midori/4967479 to your computer and use it in GitHub Desktop.
Moe Dice's command runner. Known issue:If I send too many segments to Google,the API will return 500.
var Segment=require('./classes/segment');
function buildContent(content,bot_id,user_id,user_name){
var ret=new Segment();
ret.addMention(user_name,user_id);
for(var i in content){
if(content[i].type=='USER_MENTION'&&content[i].user.userId==bot_id){
console.log('Command detected.');
var command=content[parseInt(i)+1].text.trim();
var rest=content;
rest.splice(0,parseInt(i)+2);
matchAndExec(command,rest,ret);
}
}
}
function matchAndExec(command,rest,seg){
var commands=[
{
reg:/^(\d+)\s*[dDx×\*]\s*(\d+)/,
callback:function(m){
var count=m[0],
min=1,
max=m[1];
if(max>256)max=256;
if(max<1)max=1;
if(count>64)count=64;
if(count<1)count=1;
var dice_result=roll(max,min,count);
if(roll(300,1,1)[0]==233){
seg.addText(' 骰子投出了 *0* 点!和喇叭酱的节操一样多!');
return;
}
if(count==1){
seg.addText(' 骰子骨碌骨碌转!投出了 *'+dice_result[0]+'* 点!诶嘿~(ゝω・)☆');
return;
}else{
seg.addText(' '+count+'只骰子骨碌骨碌转!分别投出了 *'+dice_result.join('、')+'* 点!诶嘿~(ゝω・)☆');
return;
}
}
},
{
reg:/^roll\s*(\d+)?\s*(\d+)?/i,
callback:function(m){
console.log(m);
if(typeof m[0]=='undefined'&&typeof m[1]=='undefined'){
var dice_result=roll(100,1,1);
}else if(typeof m[0]!='undefined'&&typeof m[1]=='undefined'){
var max=parseInt(m[0]);
if(max>256)max=256;
if(max<1)max=1;
var dice_result=roll(max,1,1);
}else{
var min=parseInt(m[0]);
if(min>256)min=256;
if(min<1)min=1;
var max=parseInt(m[1]);
if(min>256)min=256;
if(min<1)min=1;
if(min>max){
var tem=max;
max=min;
min=tem;
}
if(min==max)min=max-2;
var dice_result=roll(max,min,1);
}
seg.addText(' 骰子骨碌骨碌转!投出了 *'+dice_result[0]+'* 点!诶嘿~(ゝω・)☆');
return;
}
},
{
reg:/^list\s*(\d+)?/i,
callback:function(m){
console.log(rest);
for(var i in rest){
if(rest[i].type=='LINE_BREAK'){
console.log(i);
rest.splice(0,parseInt(i)+1);
break;
}
}
var options=[];
var temp_option=[];
for(var i in rest){
if(rest[i].type=='LINE_BREAK'){
options.push(temp_option);
temp_option=[];
continue;
}
if(rest[i].text.toLowerCase()=='endlist'&&temp_option.length==0){
break;
}
temp_option.push(rest[i]);
}
if(options.length==0){
seg.addText(' 没有选项让老娘选什么!');
return;
}
if(options.length==1){
seg.addText('只有一个选项让老娘选什么!');
return;
}
if(typeof m !='undefined'){
var count=parseInt(m);
}else{var count=1;}
if(options.length<count){
seg.addText(' 全选上不就行了么,还用老娘出马?');
return;
}
var dice_result=roll(options.length-1,0,count);
if(count==1){
seg.addText(' 骰子骨碌骨碌转!选中了 *');
for(var i in options[dice_result[0]]){
seg.segments.push(options[dice_result[0]][i]);
}
seg.addText('* !诶嘿~(ゝω・)☆');
return;
}else{
seg.addText(' '+count+'只骰子骨碌骨碌转!分别选中了 *');
for(var i in dice_result){
for(var j in options[dice_result[i]]){
seg.segments.push(options[dice_result[i]][j]);
}
seg.addText('、');
}
seg.addText('* !诶嘿~(ゝω・)☆');
}
}
},
{
reg:/^help/i,
callback:function(){
}
},
{
reg:/^bug\s*(.*)/i,
callback:function(m){
}
}
];
for(var i in commands){
var m=changeResult(commands[i].reg.exec(command));
if(m!==null){
commands[i].callback(m);
console.log(JSON.stringify(seg.toSegments()));
}
}
}
function roll(max,min,count){
var i=0;
var ret=[];
while(i<count){
ret.push(parseInt(Math.random()*(max-min+1)+min));
i++;
}
return ret;
}
function changeResult(result) {
if(result===null)return result;
// Check if the result contains named parameters
var has_named = false;
for (key in result) {
if (!key.match(/^([0-9]+|index|input)$/)) {
has_named = true;
break;
}
}
if (!has_named) {
// Only one result, do not need to create an array
if (result.length === 2) {
return result[1];
}
// Create a clean result array without the first element
var changed = [];
for (var i = 1; i < result.length; ++i) {
changed.push(result[i]);
}
return changed;
}
// Create a clean object without array elements, 'index' and 'input'
var changed = {};
for (key in result) {
if (!key.match(/^([0-9]+|index|input)$/)) {
if (key[0] == '_') {
changed[key.substr(1)] = +result[key];
} else {
changed[key] = result[key];
}
}
}
return changed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment