Finish token generation, add flags to bot
Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
parent
91a95e96f5
commit
d922bb67ce
10 changed files with 698 additions and 182 deletions
|
@ -3,13 +3,21 @@ type: docker
|
|||
name: default
|
||||
|
||||
steps:
|
||||
|
||||
- name: lint
|
||||
image: node
|
||||
commands:
|
||||
- yarn
|
||||
- yarn lint
|
||||
|
||||
- name: build
|
||||
image: node
|
||||
commands:
|
||||
- yarn
|
||||
- yarn build
|
||||
|
||||
- name: test
|
||||
image: node
|
||||
commands:
|
||||
- yarn
|
||||
- yarn test
|
||||
|
|
|
@ -1,29 +1,36 @@
|
|||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 13,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"indent": [
|
||||
"error",
|
||||
4
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es2021": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
],
|
||||
"linebreak-style": [
|
||||
"error",
|
||||
"unix"
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 13
|
||||
},
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
],
|
||||
"quotes": [
|
||||
"error",
|
||||
"double"
|
||||
],
|
||||
"semi": [
|
||||
"error",
|
||||
"always"
|
||||
]
|
||||
}
|
||||
"rules": {
|
||||
"indent": [
|
||||
"error",
|
||||
4
|
||||
],
|
||||
"linebreak-style": [
|
||||
"error",
|
||||
"unix"
|
||||
],
|
||||
"quotes": [
|
||||
"error",
|
||||
"double"
|
||||
],
|
||||
"semi": [
|
||||
"error",
|
||||
"always"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -118,3 +118,5 @@ dist
|
|||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
config.json
|
||||
images/*
|
15
bot.js
15
bot.js
|
@ -1,15 +0,0 @@
|
|||
import 'command-line-args';
|
||||
import 'command-line-usage';
|
||||
|
||||
|
||||
const optionDefinitions = [
|
||||
{ name: "help", type: Boolean, alias: 'help' },
|
||||
{ name: 'src', type: String, multiple: true, defaultOption: true },
|
||||
{ name: 'timeout', alias: 't', type: Number }
|
||||
]
|
||||
const args = commandLineArgs(optionDefinitions)
|
||||
|
||||
if (args.help) {
|
||||
|
||||
return 0;
|
||||
}
|
8
config.sample.jsonc
Normal file
8
config.sample.jsonc
Normal file
|
@ -0,0 +1,8 @@
|
|||
// This is just a mock of what the config should look like
|
||||
// Run yarn build && yarn gen-token to generate the configuration.
|
||||
{
|
||||
"instance": "INSTANCE_URL", // example https://test.com
|
||||
"type": "INSTANCE_TYPE", // examples: "mastodon", "misskey", "pleroma"
|
||||
"accessToken": "ACCESS_TOKEN",
|
||||
"refreshToken": "REFRESH_TOKEN"
|
||||
}
|
19
package.json
19
package.json
|
@ -1,21 +1,32 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"@types/command-line-args": "^5.2.0",
|
||||
"@types/command-line-usage": "^5.0.2",
|
||||
"@types/node": "^17.0.8",
|
||||
"@types/readline-sync": "^1.4.4",
|
||||
"command-line-args": "^5.2.0",
|
||||
"command-line-usage": "^6.1.1",
|
||||
"megalodon": "^4.0.0"
|
||||
"megalodon": "^4.0.0",
|
||||
"readline-sync": "^1.4.10",
|
||||
"typescript": "^4.5.4"
|
||||
},
|
||||
"name": "fediverse-imagebot",
|
||||
"version": "0.1.0",
|
||||
"description": "Imagebot for the fediverse (Pleroma, Mastodon, Misskey)",
|
||||
"main": "bot.js",
|
||||
"main": "dist/bot.js",
|
||||
"scripts": {
|
||||
"lint": "eslint",
|
||||
"test": "echo \" No tests yet!\" && exit 0"
|
||||
"build": "npm run clean && tsc",
|
||||
"clean": "rm -rf dist",
|
||||
"lint": "eslint --ext .ts src",
|
||||
"test": "echo \"No tests yet!\" && exit 0",
|
||||
"gen-token": "node ./dist/gen-token.js"
|
||||
},
|
||||
"repository": "https://git.freecumextremist.com/NotSam/fediverse-imagebot.git",
|
||||
"author": "Sam Therapy <sam@samtherapy.net>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||
"@typescript-eslint/parser": "^5.9.0",
|
||||
"eslint": "^8.6.0"
|
||||
}
|
||||
}
|
||||
|
|
52
src/bot.ts
Normal file
52
src/bot.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import commandLineArgs from "command-line-args";
|
||||
import commandLineUsage from "command-line-usage";
|
||||
import * as fs from "fs";
|
||||
import { exit } from "process";
|
||||
|
||||
|
||||
|
||||
const optionDefinitions = [
|
||||
{
|
||||
name: "help",
|
||||
type: Boolean,
|
||||
alias: "h",
|
||||
description: "Print this usage guide."
|
||||
},
|
||||
{
|
||||
name: "verbose",
|
||||
type: Boolean,
|
||||
alias: "v",
|
||||
description: "Print debugging output."
|
||||
},
|
||||
{
|
||||
name: "directory",
|
||||
type: String,
|
||||
alias: "d",
|
||||
description: "The directory of images for the bot to post.",
|
||||
defaultValue: "./images",
|
||||
typeLabel: "<files>"
|
||||
},
|
||||
];
|
||||
|
||||
const args = commandLineArgs(optionDefinitions);
|
||||
|
||||
if (args.help) {
|
||||
const usage = commandLineUsage([
|
||||
{
|
||||
header: "Fediverse Image Bot",
|
||||
content: "A bot that posts images from a directory to the Fediverse."
|
||||
},
|
||||
{
|
||||
header: "Options",
|
||||
optionList: optionDefinitions
|
||||
},
|
||||
{
|
||||
content: "Project home: {underline https://git.freecumextremist.com/NotSam/fediverse-imagebot}"
|
||||
}
|
||||
]);
|
||||
console.log(usage);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
const data = JSON.parse(fs.readFileSync("./config.json", "utf8"));
|
||||
console.log(data);
|
107
src/gen-token.ts
Normal file
107
src/gen-token.ts
Normal file
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/env node
|
||||
// Takes a user inputted fediverse instance and generates a token for the bot, and adds it to the configuration file
|
||||
import commandLineArgs from "command-line-args";
|
||||
import commandLineUsage from "command-line-usage";
|
||||
import generator, { detector, OAuth } from "megalodon";
|
||||
import { exit } from "process";
|
||||
import { question } from "readline-sync";
|
||||
import * as fs from "fs";
|
||||
|
||||
const optionDefinitions = [
|
||||
{
|
||||
name: "help",
|
||||
type: Boolean,
|
||||
alias: "h",
|
||||
description: "Print this usage guide."
|
||||
},
|
||||
{
|
||||
name: "verbose",
|
||||
type: Boolean,
|
||||
alias: "v",
|
||||
description: "Print debugging output."
|
||||
}
|
||||
];
|
||||
|
||||
const args = commandLineArgs(optionDefinitions);
|
||||
|
||||
if (args.help) {
|
||||
const usage = commandLineUsage([
|
||||
{
|
||||
header: "Token Grabber",
|
||||
content: "Grabs tokens for the bot to use."
|
||||
},
|
||||
{
|
||||
header: "Options",
|
||||
optionList: optionDefinitions
|
||||
},
|
||||
{
|
||||
content: "Project home: {underline https://git.freecumextremist.com/NotSam/fediverse-imagebot}"
|
||||
}
|
||||
]);
|
||||
console.log(usage);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (args.verbose) {
|
||||
console.log("Running in verbose mode.");
|
||||
console.log();
|
||||
}
|
||||
|
||||
|
||||
const instance: string = question("Instance URL: ");
|
||||
callDetector(instance).then(type => {
|
||||
let clientId!: string;
|
||||
let clientSecret!: string;
|
||||
const client = generator(type, instance);
|
||||
client.registerApp("Node Imagebot", { scopes: [ "write" ] })
|
||||
.then((appData) => {
|
||||
clientId = appData.clientId;
|
||||
clientSecret = appData.clientSecret;
|
||||
console.log("Please open this URL in your browser for the authorization code.");
|
||||
console.log(appData.url);
|
||||
const code = question("Authorization Code: ");
|
||||
client.fetchAccessToken(clientId, clientSecret, code)
|
||||
.then((tokenData: OAuth.TokenData) => {
|
||||
if (args.verbose) {
|
||||
console.log(`Access Token: ${tokenData.accessToken}`);
|
||||
console.log(`Refresh Token: ${tokenData.refreshToken}`);
|
||||
}
|
||||
const config = {
|
||||
instance: instance,
|
||||
type: type,
|
||||
accessToken: tokenData.accessToken,
|
||||
refreshToken: tokenData.refreshToken
|
||||
};
|
||||
fs.writeFileSync("./config.json", JSON.stringify(config, null, 2));
|
||||
console.log("Saved config to ./config.json");
|
||||
exit(0);
|
||||
})
|
||||
.catch((err: Error) => { // catch for fetchAccessToken
|
||||
console.error("Access Token fetch failed.");
|
||||
if (args.verbose)
|
||||
console.error(err);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
})
|
||||
.catch((err: Error) => { // catch for registerApp
|
||||
console.error("App registration failure!");
|
||||
if (args.verbose)
|
||||
console.error(err);
|
||||
exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
async function callDetector(instance: string) {
|
||||
const type = await detector(instance).catch( (err) => {
|
||||
console.error("This does not seem to be a valid instance!");
|
||||
console.error("Supported instance types: Mastodon, Misskey, Pleroma");
|
||||
if (args.verbose)
|
||||
console.error(err);
|
||||
exit(1);
|
||||
});
|
||||
if (args.verbose)
|
||||
console.log(`Detected ${instance} as a ${type} instance.`);
|
||||
return type;
|
||||
}
|
60
tsconfig.json
Normal file
60
tsconfig.json
Normal file
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
/* Basic Options */
|
||||
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
|
||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||
"lib": ["es6"], /* Specify library files to be included in the compilation. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||
// "sourceMap": true, /*es Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||
// "composite": true, /* Enable project compilation */
|
||||
"removeComments": true, /* Do not emit comments to output. */
|
||||
// "noEmit": true, /* Do not emit outputs. */
|
||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||
"strictNullChecks": true, /* Enable strict null checks. */
|
||||
"strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||
|
||||
/* Additional Checks */
|
||||
"noUnusedLocals": true, /* Report errors on unused locals. */
|
||||
"noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
|
||||
/* Module Resolution Options */
|
||||
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||
// "types": [], /* Type declaration files to be included in compilation. */
|
||||
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||
|
||||
/* Source Map Options */
|
||||
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||
|
||||
/* Experimental Options */
|
||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||
},
|
||||
"include": ["./src"]
|
||||
}
|
Reference in a new issue