Skip to content

Instantly share code, notes, and snippets.

@kayaked
Last active July 15, 2018 01:06
Show Gist options
  • Save kayaked/86229adab8c9be3bee78fb9a616f8d26 to your computer and use it in GitHub Desktop.
Save kayaked/86229adab8c9be3bee78fb9a616f8d26 to your computer and use it in GitHub Desktop.
TerminalBot - a bot that interprets basic terminal commands on your machine
import discord
import asyncio
from discord.ext import commands
from discord.ext.commands import Bot
import platform
from os import listdir, makedirs, utime, rename, system, remove
from os.path import isfile, join, isdir, exists, dirname, realpath
import traceback
import random
from random import randint
import inspect
from shutil import rmtree
""" Usage: Replace current_sessions value with your discord user ID. More information in the '$ help' command. In the last line, replace 'token' with your token (in quotes still of course)"""
bot = commands.Bot(command_prefix='$ ')
bot.remove_command("help")
current_sessions=["232133184404455424"]
userfolder="."
currprefix="$"
@bot.event
async def on_ready():
print('----------------------------------------')
print("TerminalBot")
print('----------------------------------------')
@bot.command(pass_context=True)
async def ls(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
folder = listdir(userfolder)
current_dir_items=""
for files in folder:
current_dir_items=current_dir_items + files + " "
if current_dir_items=="":
current_dir_items=" "
await bot.say("```{}```".format(current_dir_items))
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def help(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
await bot.say("```error : please change ID to use this bot (line 16 bash.py) get ID by typing '\@yourname' in chat or by enabling developer mode and right clicking your name.```")
return
await bot.say("```GNU bash, version 4.4.19(2)-release (x86_64-pc-msys-python) \nThese shell commands are defined internally. Type '$help' to see this list. \nType 'help name' to find out more about the function 'name'. \nUse 'info bash' to find out more about the shell in general. \nUse 'man -k' or 'info' to find out more about commands not on this list. \n\nA star (*) next to a name means that the command is disabled.```")
await bot.say("```help\tDisplays this prompt```")
await bot.say("```cd [dir]\tOpens specified directory```")
await bot.say("```ls\tLists items in current directory```")
await bot.say("```mkdir [dir]\tCreates directory in specified location```")
await bot.say("```cat [file]\tPrints File```")
await bot.say("```touch [file]\tCreates file in current directory```")
await bot.say("```mv [file] [dest]\tMove a file to specified destination```")
await bot.say("```su [user]\tTakes root and mobile users```")
await bot.say("```py [file]\tRuns file as Python```")
await bot.say("```echo [text] [>> destfile]\tReturns the string or writes to specified file```")
await bot.say("```rm [item]\tRemoves specified file/dir```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def cd(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if args==None:
userfolder="."
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
truedir=dirname(realpath(__file__)).replace("\\", "/")
truedir=truedir.replace("C:", "")
if args=="..":
userfolder=userfolder.rsplit('/', 1)[0]
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if args.startswith("/"):
if isdir("{}".format(args)):
userfolder=args
if userfolder==truedir:
userfolder="."
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
else:
await bot.say("```File not found!```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if isdir("{}{}".format(userfolder, args)):
userfolder=userfolder+ "{}".format(args)
if userfolder==truedir:
userfolder="."
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if isdir("{}/{}".format(userfolder, args)):
userfolder=userfolder+ "/{}".format(args)
if userfolder==truedir:
userfolder="."
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
else:
await bot.say("```File not found!```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def mkdir(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if args==None:
await bot.say("```mkdir : missing operand\nUsage: 'mkdir [dir]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
try:
if not exists("{}/{}".format(userfolder, args)):
makedirs("{}/{}".format(userfolder, args))
except OSError:
print ('Creation of directory {} failed.'.format(args))
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def cat(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if args==None:
await bot.say("```cat : missing operand\nUsage: 'cat [file]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
try:
if isfile("{}/{}".format(userfolder, args)):
file = open("{}/{}".format(userfolder, args))
lines=["\n"]
for line in file:
lines.append(line)
await bot.say("```{}```".format("".join(lines)))
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
else:
await bot.say("```cat : File not found```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
except IOError:
await bot.say("error")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
@bot.command(pass_context=True)
async def touch(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if args==None:
await bot.say("```touch : missing operand\nUsage: 'touch [file]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
try:
if not isfile("{}/{}".format(userfolder, args)):
with open("{}/{}".format(userfolder, args), 'a'):
utime("{}/{}".format(userfolder, args), None)
except IOError:
await bot.say("```touch : command failure.```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def su(ctx, *, args=None):
if ctx.message.author.id not in current_sessions:
return
global currprefix
if args=="root":
bot.command_prefix='# '
currprefix="#"
elif args=="mobile":
bot.command_prefix='$ '
currprefix="$"
else:
await bot.say("```su : user not found```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def mv(ctx, file=None, *, dest=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if dest==None:
await bot.say("```mv : missing operand\nUsage: 'mv [file] [dest]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if isfile("{}/{}".format(userfolder, file)) or isdir("{}/{}".format(userfolder, file)):
if isdir("{}/{}".format(userfolder, dest)):
rename("{}/{}".format(userfolder, file), "{}/{}/{}".format(userfolder, dest, file.rsplit("/", 1)[len(file.rsplit("/", 1))-1]))
else:
await bot.say("```mv : destination not found```")
else:
await bot.say("```mv : file not found```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True, aliases=["python", "python2", "python3"])
async def py(ctx, *, fname=None):
if ctx.message.author.id not in current_sessions:
return
global userfolder
if fname==None:
await bot.say("```py : missing operand\nUsage: 'py [python file]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
try:
system("py {}/{}".format(userfolder, fname))
except Exception:
await bot.say("```py : script error```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def echo(ctx, *, shit=None):
global userfolder
if ctx.message.author.id not in current_sessions:
return
if shit==None:
await bot.say("```echo : missing operand\nUsage: 'echo [text] [>> file]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if ">>" in shit:
parts=shit.split(">>")
parts[1]=parts[1].lstrip(" ")
parts[0]=parts[0].rstrip(" ")
if isfile("{}/{}".format(userfolder, parts[1])):
with open("{}/{}".format(userfolder, parts[1]), "a") as fffd:
fffd.write(parts[0])
elif isfile("C:{}{}".format(userfolder, parts[1])):
with open("C:{}{}".format(userfolder, parts[1]), "a") as fffd:
fffd.write("\n{}".format(parts[0]))
else:
await bot.say("```echo : file not found```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
await bot.say("```{}```".format(shit))
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
@bot.command(pass_context=True)
async def rm(ctx, *, file=None):
global userfolder
if ctx.message.author.id not in current_sessions:
return
if file==None:
await bot.say("```rm : missing operand\nUsage: 'rm [file/dir]'```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
return
if isfile("{}/{}".format(userfolder, file)):
remove("{}/{}".format(userfolder, file))
elif isdir("{}/{}".format(userfolder, file)):
rmtree("{}/{}".format(userfolder, file))
else:
await bot.say("```rm : file not found```")
await bot.say("```{}@{}:~{} {}```".format(ctx.message.author, ctx.message.server, userfolder, currprefix))
bot.run('token')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment