Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* MADE BY FLUTTERSHY.PL */
- //feel free to improve this code. Remember to share your results!
- /* PNGJS is a lazy, temporary solution. The intended way is transforming original BMP structure of bigchunk.bmp .*/
- //required modules, get pngjs and discord.js using npm
- const Discord = require('discord.js');
- const HTTP = require('http');
- const PNG = require('pngjs').PNG;
- //RGB values of colours used by pixelcanvas
- const red = [255,228,136,34,255,229,229,160,229,148,2,0,0,0,207,130];
- const green = [255,228,136,34,167,0,149,106,217,224,190,211,131,0,110,0];
- const blue = [255,228,136,34,209,0,0,66,0,68,1,221,199,234,228,128];
- //user configuration, maximum dimentions that won't get scaled down by Discord are 400 width 300 height, maximum for the bot is 897 width 897 height
- const token = ''; //token from discord developer API
- const w = 400;
- const h = 120;
- //actual bot code
- const client = new Discord.Client();
- client.on('ready', () => {
- console.log("Successfully connected to Discord!");
- });
- function sendSnapshot(x,y,channel){
- console.log("Somepony requested @"+x+","+y)
- var topLeftX = parseInt(x-(w/2));
- var offsetLeft = ((topLeftX+448)%64)+((topLeftX<448)*64)
- var bigChunkX = parseInt((topLeftX+448)/64)+(topLeftX>=448);
- var topLeftY = parseInt(y-(h/2));
- var offsetTop = ((topLeftY+448)%64)+((topLeftY<448)*64)
- var bigChunkY = parseInt((topLeftY+448)/64)+(topLeftY>=448);
- HTTP.get('http://pixelcanvas.io/api/bigchunk/'+bigChunkX+'.'+bigChunkY+'.bmp',function(response){
- var data = [];
- response.on('data',function(chunk){
- data.push(chunk);
- });
- response.on('end',function(){
- var buffer = Buffer.concat(data);
- var pngimg = new PNG({
- width: w,
- height: h,
- filterType: -1
- });
- for (var y = 0; y < pngimg.height; y++)
- for (var x = 0; x < pngimg.width; x++) {
- var i = (pngimg.width * y + x) << 2;
- var byteOffset = ((parseInt((offsetTop+y)/64)*15+parseInt((offsetLeft+x)/64))*4096+64*((offsetTop+y)%64)+((offsetLeft+x)%64))/2;
- var colourId = byteOffset%1!=0?buffer[parseInt(byteOffset)]&15:buffer[parseInt(byteOffset)]>>4;
- pngimg.data[i] = red[colourId];
- pngimg.data[i+1] = green[colourId];
- pngimg.data[i+2] = blue[colourId];
- pngimg.data[i+3] = 0xff;
- }
- var att = new Discord.Attachment(pngimg.pack(),"snapshot.png");
- channel.send("",att);
- });
- });
- }
- client.on('message', message => {
- var index = message.content.indexOf("pixelcanvas.io/@");
- while(index>-1){
- var failed = false;
- index += 0x10;
- var paramX="";
- var paramY="";
- if(message.content[index]=="-")
- paramX+=message.content[index++];
- if(isNaN(message.content[index]))
- failed = true;
- else {
- while(!isNaN(message.content[index]))
- paramX += message.content[index++];
- paramX = (paramX);
- if(message.content[index++]!=",")
- failed = true;
- else {
- if(message.content[index]=="-")
- paramY += message.content[index++];
- if(isNaN(message.content[index]))
- failed = true;
- else {
- while(!isNaN(message.content[index]))
- paramY += message.content[index++];
- }
- }
- }
- if(!failed)
- sendSnapshot(parseInt(paramX),parseInt(paramY),message.channel);
- message.content = message.content.substring(index);
- index = message.content.indexOf("pixelcanvas.io/@");
- }
- });
- client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement