Skip to content

Commit

Permalink
Added file uploading, codeblock printing/parsing, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
daisyUniverse committed Aug 26, 2019
1 parent 309932f commit e52d97c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 6 deletions.
28 changes: 26 additions & 2 deletions discho
Expand Up @@ -7,6 +7,28 @@ if [[ $1 = "-c" ]]; then
echo $(</dev/stdin) > /tmp/discho
node $HOME/.discho/discho.js

elif [[ $1 = "-u" ]]; then

[ ! -t 0 ] && echo $(</dev/stdin) > /tmp/discho || > /tmp/discho

if [[ $3 = "-c" ]]; then
jq '."channel" = ''"'"$4"'"' $HOME/.discho/config.json > /tmp/temp.json && cat /tmp/temp.json > $HOME/.discho/config.json
fi
echo $2 > /tmp/dischofn
cp $2 $HOME/.discho/out/
node $HOME/.discho/dischofs.js && rm $HOME/.discho/out/*

elif [[ $1 = "-cb" ]]; then

[ ! -t 0 ] && echo $(</dev/stdin) > /tmp/discho || > /tmp/discho

if [[ $3 = "-c" ]]; then
jq '."channel" = ''"'"$4"'"' $HOME/.discho/config.json > /tmp/temp.json && cat /tmp/temp.json > $HOME/.discho/config.json
fi
echo $2 > /tmp/dischofn
cp $2 $HOME/.discho/out/
node $HOME/.discho/dischofs.js cb && rm $HOME/.discho/out/*

elif [[ $1 = "-t" ]]; then

jq '."channel" = ''"'"$2"'"' $HOME/.discho/config.json > /tmp/temp.json && cat /tmp/temp.json > $HOME/.discho/config.json
Expand All @@ -17,9 +39,11 @@ elif [[ $1 = "-h" ]]; then
echo "Discho by Elisha Shaddock"
echo "discho -c <channel-name> to send to a specific channel. defaults to 'general', and stays on the last one you sent to"
echo "discho -t <TOKEN> sets bot token for the bot"
echo "discho -u <FILEPATH> will upload this file as an attachement. Append -c <channel-name> to specify a channel, otherwise it will send to the last used"
echo "discho -cb <FILEPATH> will upload this file as an attatchement, and attempt to post its content as a code block using its file extension as the syntax"
echo "discho -h shows this"
echo "discho install: installs this script to /usr/bin/ for systemwide use"

elif [ $? != 0 ]; then

echo $(</dev/stdin) > /tmp/discho
Expand All @@ -30,4 +54,4 @@ elif [ $1 = "install" ]; then
sudo cp $HOME/.discho/discho /usr/bin/
echo "Installed to /usr/bin/"

fi
fi
47 changes: 47 additions & 0 deletions dischofs.js
@@ -0,0 +1,47 @@
const Discord = require('discord.js');
const bot = new Discord.Client();
const config = require("./config.json");
const args = process.argv;
const fs = require('fs');

bot.on("ready", () =>{
const chan = bot.channels.find(channel => channel.name === config.channel);

fs.readFile('/tmp/discho', 'utf-8', (err, data) => {
if (err) throw err;
if (data) {
chan.send(data);
}
});

fs.readFile('/tmp/dischofn', 'utf-8', (err, data) => {
if (err) throw err;
fl = __dirname+"/out/" + data.split("/").splice(-1);
fn = "" + data.split("/").splice(-1);
ft = ""+fn.split(".").splice(-1);

if(args[2] == "cb"){
if(ft.trim() == "js" || ft.trim() == "py" || ft.trim() == "css" || ft.trim() == "fix" || ft.trim() == "md" || ft.trim() == "cs" || ft.trim() == "diff" || ft.trim() == "tex" || ft.trim() == "sh" || ft.trim() == "h" || ft.trim() == "cpp" || ft.trim() == "json"){
fs.readFile(fl.trim(),'utf8',(er, cfile) => {
if (er) throw er;
console.log("Detected code file type ["+ft.trim()+"] attempting to print code block")
chan.send("`"+"``"+ft.trim()+"\n"+cfile+"`"+"``")
})
}
}

chan.send({files: [{attachment: fl.trim(), name: fn.trim()}]})
.then(console.log)
.catch(console.error);;
});
})

bot.on('message', function(message){
if (message.attachments.size > 0) {
process.exit();
}else{
console.log("waiting to detect attatchment to exit...")
}
});

bot.login(config.token);
Binary file added img/1566829260321.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/1566829320839.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/1566829427880.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/1566829473079.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 29 additions & 4 deletions readme.md
Expand Up @@ -5,20 +5,33 @@



**What it does:** Allows you to pipe input from bash and have it output to a discord channel


**What it does:** Allows you to pipe input from bash and have it output to a discord channel, and upload local files / scripts

## Usage:

```bash
echo "FOO" | discho
```

![1566829260321](img/1566829260321.png)

```bash
uname -r | discho -c channelname
scrot ~/scrot.png && echo "My Desktop!" | discho -u ~/scrot.png
```

![1566829320839](img/1566829320839.png)

```bash
echo "my config" | discho -cb ~/.keebie/default.json
```

![1566829427880](img/1566829427880.png)

```bash
uname -r | discho -c general
```

![1566829473079](img/1566829473079.png)


## **Installation**
Expand All @@ -40,3 +53,15 @@ discho -t whateveryourbottokenis

to set your bot token

from `discho -h`...

```bash
Discho by Elisha Shaddock
discho -c <channel-name> to send to a specific channel. defaults to 'general', and stays on the last one you sent to
discho -t <TOKEN> sets bot token for the bot
discho -u <FILEPATH> will upload this file as an attachement. Append -c <channel-name> to specify a channel, otherwise it will send to the last used
discho -cb <FILEPATH> will upload this file as an attatchement, and attempt to post its content as a code block using its file extension as the syntax
discho -h shows this
discho install: installs this script to /usr/bin/ for systemwide use
```

0 comments on commit e52d97c

Please sign in to comment.