botalan

discord bot
git clone git://git.benoneill.xyz/botalan
Log | Files | Refs | README | LICENSE

commit 0d99ac8420cc8e7c333b45bcdb15c61c0000a3d1
parent 604a1a23c7be27500a23eb94135d5fa2bdf806a8
Author: Ben O'Neill <ben@benoneill.xyz>
Date:   Tue,  7 Sep 2021 14:17:09 -0400

restructure commands, add bug notice

Diffstat:
MREADME.md | 4++++
Mbotalan.py | 22+++++++++++++++++-----
Mcmd.py | 22++++++++++++++++------
3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md @@ -21,6 +21,10 @@ features may be added later. Obtain a bot token, then run `start.sh` and enter the token. To stop, run `stop.sh`. +## Bugs + +Shell injections are possible currently. This must be addressed. + ## License Copyright (C) 2021 Ben O'Neill <ben@benoneill.xyz>. License: MIT. diff --git a/botalan.py b/botalan.py @@ -11,12 +11,25 @@ def exec_cmd(msg): msg = msg.strip() if msg == '!tweet': response = cmd.tweet() - if msg == '!fortune': + + if msg.startswith('!fortune'): + if len(msg.split()) > 1: + arg = msg.split()[1] + print(arg) + print(len(arg)) + response = cmd.fortune(arg) response = cmd.fortune() + if msg.startswith('!cowsay ') and len(msg) > len('!cowsay '): index = msg.index(' ') + 1 - cowmsg = msg[index:] - response = cmd.cowsay(cowmsg) + arg = None + if msg[index] == '-': + arg = msg[index + 1:].split()[0] + cowmsg = msg[index + len(arg) + 1:] + else: + cowmsg = msg[index:] + response = cmd.cowsay(arg, cowmsg) + if msg == ('!cowfortune'): response = cmd.cowfortune() @@ -24,7 +37,7 @@ def exec_cmd(msg): @client.event async def on_ready(): - print('bot alan has entered the discord realm') + print(f'{client.user} has connected to Discord!') @client.event async def on_message(message): @@ -33,7 +46,6 @@ async def on_message(message): response = exec_cmd(message.content) await message.channel.send(response) -# bot code can run without connecting to Discord for testing if TOKEN == None: print("Running in test mode") while True: diff --git a/cmd.py b/cmd.py @@ -3,8 +3,12 @@ from subprocess import Popen, PIPE from tweet import tweets -def cowsay(msg): - process = Popen(["cowsay", msg], stdout=PIPE) +def cowsay(arg, msg): + if arg == None: + process = Popen(["cowsay", msg], stdout=PIPE) + else: + process = Popen(["cowsay", "-f", arg, msg], stdout=PIPE) + (output, err) = process.communicate() exit_code = process.wait() return '```\n' + output.decode('utf-8') + '```\n' @@ -12,15 +16,21 @@ def cowsay(msg): def tweet(): return random.choice(tweets) -def fortune(): - process = Popen(["fortune"], stdout=PIPE) +def fortune(arg=None): + if arg == None: + process = Popen(["fortune"], stdout=PIPE) + else: + process = Popen(["fortune", arg], stdout=PIPE) (output, err) = process.communicate() exit_code = process.wait() return output.decode('utf-8') +def cowfortune(arg=None): + if arg == None: + p1 = Popen(["fortune"], stdout=PIPE) + else: + p1 = Popen(["fortune", arg], stdout=PIPE) -def cowfortune(): - p1 = Popen(["fortune"], stdout=PIPE) p2 = Popen(["cowsay"], stdin=p1.stdout, stdout=PIPE) p1.stdout.close() (output, err) = p2.communicate()