2024-01-27 03:54:37 +00:00
|
|
|
import { exec } from 'node:child_process';
|
|
|
|
|
2022-07-11 09:34:15 +00:00
|
|
|
import { overrideConsole } from 'nodejs-better-console';
|
|
|
|
import Eris from 'eris';
|
2024-01-27 03:54:37 +00:00
|
|
|
import { Command } from 'commander';
|
2024-01-27 19:18:53 +00:00
|
|
|
import KeyEvent from 'keyevent.json' assert { type: 'json' };
|
2024-01-27 03:54:37 +00:00
|
|
|
import { parse } from 'shell-quote';
|
2022-07-11 09:34:15 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
botToken,
|
2024-01-27 03:54:37 +00:00
|
|
|
botWhitelist,
|
|
|
|
adbIp,
|
|
|
|
adbPort
|
2022-07-11 09:34:15 +00:00
|
|
|
} from './config.js';
|
|
|
|
|
|
|
|
overrideConsole();
|
|
|
|
|
|
|
|
const
|
|
|
|
eris = new Eris(
|
|
|
|
botToken,
|
|
|
|
{ intents: ['directMessages'] }
|
2024-01-27 03:54:37 +00:00
|
|
|
),
|
|
|
|
adb = command => new Promise((resolve, reject) => exec(
|
|
|
|
`adb -s ${adbIp}:${adbPort} shell ${command}`,
|
|
|
|
(
|
|
|
|
error,
|
|
|
|
stdout,
|
|
|
|
stderr
|
|
|
|
) => {
|
|
|
|
if(error || stderr)
|
|
|
|
reject(error || stderr);
|
|
|
|
else
|
|
|
|
resolve(stdout);
|
|
|
|
}
|
|
|
|
)),
|
|
|
|
handleCommand = async command => {
|
|
|
|
const program = new Command('');
|
|
|
|
let response;
|
|
|
|
program.exitOverride();
|
|
|
|
program.configureOutput({
|
|
|
|
writeOut: output => response = { output, isRaw: true },
|
|
|
|
writeErr: output => response = { output, isRaw: true }
|
|
|
|
});
|
|
|
|
program
|
|
|
|
.command('shell')
|
|
|
|
.description('Execute arbitrary shell command')
|
|
|
|
.argument('<command>', 'Arbitrary shell command')
|
|
|
|
.action(async command => {
|
|
|
|
response = {
|
|
|
|
output: await adb(command),
|
|
|
|
isRaw: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
program
|
|
|
|
.command('p')
|
|
|
|
.description('Play/pause media')
|
2024-01-27 19:18:53 +00:00
|
|
|
.action(() => adb(`input keyevent ${KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE}`));
|
2024-01-27 03:54:37 +00:00
|
|
|
program
|
|
|
|
.command('play')
|
|
|
|
.description('Play media')
|
2024-01-27 19:18:53 +00:00
|
|
|
.action(() => adb(`input keyevent ${KeyEvent.KEYCODE_MEDIA_PLAY}`));
|
2024-01-27 03:54:37 +00:00
|
|
|
program
|
|
|
|
.command('pause')
|
|
|
|
.description('Pause media')
|
2024-01-27 19:18:53 +00:00
|
|
|
.action(() => adb(`input keyevent ${KeyEvent.KEYCODE_MEDIA_PAUSE}`));
|
2024-01-27 03:54:37 +00:00
|
|
|
program
|
|
|
|
.command('spotify')
|
|
|
|
.description('Launch Spotify')
|
|
|
|
.action(() => adb('monkey -p com.spotify.music 1'));
|
|
|
|
program
|
|
|
|
.command('mpv')
|
|
|
|
.description('Launch MPV')
|
|
|
|
.argument('<url>', 'Media URL')
|
|
|
|
.action(url => adb(`am start -a android.intent.action.VIEW -d ${url} -t video/any is.xyz.mpv`));
|
|
|
|
try {
|
|
|
|
await program.parseAsync(
|
|
|
|
parse(command),
|
|
|
|
{ from: 'user' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch(error){
|
|
|
|
if(!error.code.startsWith?.('commander.'))
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
if(response?.output)
|
|
|
|
response.output = response.output.replace(/\r/g, '').trim();
|
|
|
|
return response;
|
|
|
|
};
|
2022-07-12 15:01:25 +00:00
|
|
|
|
|
|
|
eris.on(
|
|
|
|
'error',
|
|
|
|
error => console.error(error)
|
|
|
|
);
|
|
|
|
|
2022-07-11 09:34:15 +00:00
|
|
|
eris.on(
|
|
|
|
'messageCreate',
|
2022-07-13 17:36:44 +00:00
|
|
|
async message => {
|
2024-01-27 03:54:37 +00:00
|
|
|
const
|
|
|
|
{
|
|
|
|
guildID,
|
|
|
|
author: { id: authorID },
|
|
|
|
channel: { id: channelID },
|
|
|
|
id: messageID,
|
|
|
|
content
|
|
|
|
} = message,
|
|
|
|
reply = async (content, isCodeBlock) => {
|
|
|
|
const lines = content.split('\n');
|
|
|
|
let chunk = isCodeBlock ? '```\n' : '';
|
|
|
|
for(let lineIndex = 0; lineIndex < lines.length; lineIndex++){
|
|
|
|
const
|
|
|
|
line = lines[lineIndex] + '\n',
|
|
|
|
canAddLine = chunk.length + line.length + (isCodeBlock ? 3 : 0) <= 2000;
|
|
|
|
if(canAddLine)
|
|
|
|
chunk += line;
|
|
|
|
if(!canAddLine || lineIndex === lines.length - 1){
|
|
|
|
if(isCodeBlock)
|
|
|
|
chunk += '```';
|
|
|
|
await eris.createMessage(
|
|
|
|
channelID,
|
|
|
|
{
|
|
|
|
content: chunk,
|
|
|
|
messageReference: { messageID }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
chunk = `\`\`\`\n${line}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if(message.author.bot) return;
|
2022-07-11 09:34:15 +00:00
|
|
|
if(
|
|
|
|
guildID
|
|
|
|
||
|
|
|
|
!botWhitelist.includes(authorID)
|
2024-01-27 03:54:37 +00:00
|
|
|
) return message.addReaction('❌');
|
|
|
|
await message.addReaction('⏳');
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
output,
|
|
|
|
isRaw
|
|
|
|
} = await handleCommand(content) || {};
|
|
|
|
if(output)
|
|
|
|
await reply(output, isRaw);
|
|
|
|
await message.addReaction('✅');
|
|
|
|
}
|
|
|
|
catch(error){
|
|
|
|
console.error(error);
|
|
|
|
await message.addReaction('❌');
|
2022-07-11 09:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
await Promise.all([
|
|
|
|
eris.connect(),
|
|
|
|
new Promise(resolve => eris.once('ready', resolve))
|
|
|
|
]);
|
|
|
|
console.log('Ready');
|
|
|
|
})().catch(error => console.error(error));
|