basic bot with less garbage

This commit is contained in:
Expand-sys 2021-04-28 18:30:00 +10:00
parent 9437c59015
commit dc9a9c46a3
5 changed files with 0 additions and 432 deletions

View file

@ -1,61 +0,0 @@
const tf = require('@tensorflow/tfjs')
const tfn = require('@tensorflow/tfjs-node')
const mobilenet = require('@tensorflow-models/mobilenet');
const canvasAPP = require('canvas');
const cocoSsd = require('@tensorflow-models/coco-ssd');
async function drawBoxes(img){
var image = await canvasAPP.loadImage(img)
const canvas = await canvasAPP.createCanvas(image.width, image.height)
const ctx = await canvas.getContext('2d')
await ctx.drawImage(image, 0, 0)
const model = await cocoSsd.load();
var imgPixel = await tf.browser.fromPixels(canvas)
const predictions = await model.detect(imgPixel, 20, 0.1)
const font = "16px sans-serif";
ctx.font = font;
ctx.textBaseline = "top";
predictions.forEach(prediction => {
const x = prediction.bbox[0];
const y = prediction.bbox[1];
const width = prediction.bbox[2];
const height = prediction.bbox[3];
// Bounding box
ctx.strokeStyle = "#00FFFF";
ctx.lineWidth = 2;
ctx.strokeRect(x, y, width, height);
// Label background
ctx.fillStyle = "#00FFFF";
const textWidth = ctx.measureText(prediction.class).width;
const textHeight = parseInt(font, 10); // base 10
ctx.fillRect(x, y, textWidth + 4, textHeight + 4);
});
predictions.forEach(prediction => {
const x = prediction.bbox[0];
const y = prediction.bbox[1];
ctx.fillStyle = "#000000";
ctx.fillText(prediction.class, x, y);
});
const buffer = canvas.toBuffer('image/png')
return buffer
};
async function catdetector(imagePath){
let result
var image = await canvasAPP.loadImage(imagePath)
const canvas = await canvasAPP.createCanvas(image.width, image.height)
const ctx = await canvas.getContext('2d')
await ctx.drawImage(image, 0, 0)
//const decodedImage = await tfn.node.decodeImage(image, 3);
const model = await mobilenet.load()
var imgPixel = await tf.browser.fromPixels(canvas)
result = await model.classify(imgPixel)
return result
}
module.exports = {
catdetector,
drawBoxes
}

View file

@ -1,91 +0,0 @@
const alphabetBasic = {
'a': '4',
'b': '8',
'e': '3',
'f': 'f',
'g': '6', // or 9
'i': '1', // or |
'o': '0',
's': '5',
't': '7' // or +
}
const alphabetAdvanced = {
'c': '(', // or k or |< or /<
'd': '<|',
'h': '|-|',
'k': '|<', // or /<
'l': '|', // or 1
'm': '|\\/|',
'n': '|\\|',
'p': '|2',
'u': '|_|',
'v': '/', // or \/
'w': '//', // or \/\/
'x': '><',
'y': '\'/'
}
const alphabetReversed = [
[/(\|\\\/\|)/g, 'm'],
[/(\|\\\|)/g, 'n'],
[/(\()/g, 'c'],
[/(<\|)/g, 'd'],
[/\|-\|/g, 'h'],
[/(\|<)/g, 'k'],
[/(\|2)/g, 'p'],
[/(\|_\|)/g, 'u'],
[/(\/\/)/g, 'w'],
[/(><)/g, 'x'],
[/(\|)/g, 'l'],
[/(\'\/)/g, 'y'],
[/(\/)/g, 'v'],
[/(1)/g, 'i'],
[/(0)/g, 'o'],
[/(3)/g, 'e'],
[/(4)/g, 'a'],
[/(5)/g, 's'],
[/(6)/g, 'g'],
[/(7)/g, 't'],
[/(8)/g, 'b'],
[/(ph)/g, 'f'],
]
// Convert input into l33t
const convertInput = (text, useAdvanced = 'n') => {
for (let i = 0; i < text.length; i++) {
let alphabet
let letter = text[i].toLowerCase()
if (useAdvanced.toLowerCase() === 'y') {
// Use advanced l33t speak alphabet
alphabet = alphabetBasic[letter] ? alphabetBasic[letter] : alphabetAdvanced[letter]
} else {
// Use basic l33t speak alphabet
alphabet = alphabetBasic[letter]
}
if (alphabet) {
text = text.replace(text[i], alphabet)
}
}
// Show the result in console
console.log(text)
return text
}
const convertInputReverse = (text) => {
text = text.toLowerCase()
alphabetReversed.map((x) => {
text = text.replace(x[0], x[1])
})
console.log(text)
return text
}
module.exports = { convertInput, convertInputReverse }

View file

@ -1,65 +0,0 @@
const canvasAPP = require('canvas');
const applyText = (canvas, text) => {
const ctx = canvas.getContext('2d');
// Declare a base size of the font
let fontSize = 70;
do {
// Assign the font to the context and decrement it so it can be measured again
ctx.font = `${fontSize -= 10}px sans-serif`;
// Compare pixel width of the text to the canvas minus the approximate avatar size
} while (ctx.measureText(text).width > canvas.width - 300);
// Return the result to use in the actual canvas
return ctx.font;
};
async function makeCard(type, img, texttop, textbottom){
var background = await canvasAPP.loadImage('./action.png')
if(type == "s" || type == "S"){
background = await canvasAPP.loadImage('./status.png')
}
if(textbottom[0]==' '){
textbottom = textbottom.slice(1, textbottom.length)
}
const canvas = await canvasAPP.createCanvas(background.width, background.height)
const ctx = await canvas.getContext('2d')
await ctx.drawImage(background, 0, 0)
const font1 = "40px sans-serif";
const font2 = "8px sans-serif";
const pic = await canvasAPP.loadImage(img)
ctx.drawImage(pic,200,220,360,360)
ctx.font = applyText(canvas, texttop);
ctx.textBaseline = "top";
ctx.fillStyle = '#ffffff';
ctx.fillText(texttop, canvas.width/3.25, 60)
ctx.font = '16pt sans-serif'
let x = canvas.width/4;
let maxWidth = 400;
let y = 620
let line = '';
let lineHeight = 25;
for(var n = 0; n < textbottom.length; n++) {
var testLine = line + textbottom[n] + ' ';
var metrics = ctx.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
ctx.fillText(line, x, y);
line = textbottom[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
ctx.fillText(line, x, y);
}
const buffer = canvas.toBuffer('image/png')
return buffer
};
module.exports = {
makeCard
}

View file

@ -1,168 +0,0 @@
const baseURL = "https://api.steampowered.com/"
const token = '6CF857296B7A47108E148C40164822B2'
var http = require("http");
const superagent = require("superagent")
async function createRequest(interfaceName, method, version) {
let url = `${baseURL}/${interfaceName}/${method}/v${version}/?key=${token}&format=json`;
return url;
}
async function ownedGames(steamID) {
return new Promise((resolve, reject) => {
let req = http.get(`http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=${token}&steamid=${steamID}&format=json`, function(res){
var body = ""
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
let json
try{
json = JSON.parse(body)
} catch(error){
console.log(error)
}
resolve(json);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
async function getSteamID(vanityUrl) {
return new Promise((resolve, reject) => {
let req = http.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${token}&vanityurl=${vanityUrl}&format=json`, function(res){
var body = ""
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
let json
try{
json = JSON.parse(body)
} catch(error){
console.log(error)
}
resolve(json);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
async function recentGames(steamID) {
return new Promise((resolve, reject) => {
let req = http.get(`http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=${token}&steamid=${steamID}&format=json`, function(res){
var body = ""
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
let json
try{
json = JSON.parse(body)
} catch(error){
console.log(error)
}
resolve(json);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
async function user(steamID) {
return new Promise((resolve, reject) => {
let req = http.get(`http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${token}&steamids=${steamID}`, function(res){
var body = ""
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
let json
try{
json = JSON.parse(body)
} catch(error){
console.log(error)
}
resolve(json);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
async function gameNews(appID) {
return new Promise((resolve, reject) => {
let req = http.get(`http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=${appID}&count=1&maxlength=300&format=json`, function(res){
var body = ""
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
let json
try{
json = JSON.parse(body)
} catch(error){
console.log(error)
}
resolve(json);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
async function game(appID) {
return new Promise((resolve, reject) => {
let req = http.request(`https://store.steampowered.com/api/appdetails?appids=${appID}`, function(res){
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
})
}
module.exports = {
getSteamID,
game,
gameNews,
user,
createRequest,
ownedGames,
recentGames,
}

View file

@ -1,47 +0,0 @@
var http = require("https");
async function define(term) {
return new Promise((resolve, reject) => {
const path = '/define?term='+term;
const options = {
"method": "GET",
"hostname": "mashape-community-urban-dictionary.p.rapidapi.com",
"port": null,
"path": path,
"headers": {
"x-rapidapi-key": "1276b32e63msh739dcbdeaf39a12p175d69jsn31c93e5fcdbc",
"x-rapidapi-host": "mashape-community-urban-dictionary.p.rapidapi.com",
"useQueryString": true
}
};
const req = http.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
module.exports = {
define,
};