This commit is contained in:
Ben Harris 2017-04-25 19:42:55 -04:00
parent 12f49a777d
commit 2a0a1a72aa
4 changed files with 246 additions and 0 deletions

24
paper/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
*.aux
*.glo
*.idx
*.log
*.toc
*.ist
*.acn
*.acr
*.alg
*.bbl
*.blg
*.dvi
*.glg
*.gls
*.ilg
*.ind
*.lof
*.lot
*.maf
*.mtc
*.mtc1
*.out
*.synctex.gz

BIN
paper/sr_proj_paper.pdf Normal file

Binary file not shown.

198
paper/sr_proj_paper.tex Normal file
View File

@ -0,0 +1,198 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Thin Sectioned Essay
% LaTeX Template
% Version 1.0 (3/8/13)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original Author:
% Nicolas Diaz (nsdiaz@uc.cl) with extensive modifications by:
% Vel (vel@latextemplates.com)
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass[12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size (remove a4paper for US letter paper)
\usepackage[protrusion=true,expansion=true]{microtype} % Better typography
\usepackage{graphicx} % Required for including pictures
\usepackage{wrapfig} % Allows in-line images
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=PHP,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
\usepackage{mathpazo} % Use the Palatino font
\usepackage[T1]{fontenc} % Required for accented characters
\linespread{1.25} % Change line spacing here, Palatino benefits from a slight increase by default
\makeatletter
\renewcommand\@biblabel[1]{\textbf{#1.}} % Change the square brackets for each bibliography item from '[1]' to '1.'
\renewcommand{\@listI}{\itemsep=0pt} % Reduce the space between items in the itemize and enumerate environments and the bibliography
\renewcommand{\maketitle}{ % Customize the title - do not edit title and author name here, see the TITLE block below
\begin{flushright} % Right align
{\LARGE\@title} % Increase the font size of the title
\vspace{50pt} % Some vertical space between the title and author name
{\large\@author} % Author name
\\\@date % Date
\vspace{40pt} % Some vertical space between the author block and abstract
\end{flushright}
}
%----------------------------------------------------------------------------------------
% TITLE
%----------------------------------------------------------------------------------------
\title{\textbf{Senior Project Paper}\\ % Title
Discord Chat Bot: Asynchronous PHP} % Subtitle
\author{\textsc{Benjamin Harris} % Author
\\{\textit{Northern Michigan University}}} % Institution
\date{April 25, 2017} % Date
%----------------------------------------------------------------------------------------
\begin{document}
\maketitle % Print the title section
%----------------------------------------------------------------------------------------
% ABSTRACT AND KEYWORDS
%----------------------------------------------------------------------------------------
% \renewcommand{\abstractname}{Summary} % Uncomment to change the name of the
% %abstract to something else
% \begin{abstract}
% Morbi tempor congue porta. Proin semper, leo vitae faucibus dictum, metus mauris lacinia lorem, ac congue leo felis eu turpis. Sed nec nunc pellentesque, gravida eros at, porttitor ipsum. Praesent consequat urna a lacus lobortis ultrices eget ac metus. In tempus hendrerit rhoncus. Mauris dignissim turpis id sollicitudin lacinia. Praesent libero tellus, fringilla nec ullamcorper at, ultrices id nulla. Phasellus placerat a tellus a malesuada.
% \end{abstract}
% \hspace*{3,6mm}\textit{Keywords:} lorem , ipsum , dolor , sit amet , lectus % Keywords
% \vspace{30pt} % Some vertical space between the abstract and first section
%----------------------------------------------------------------------------------------
% ESSAY BODY
%----------------------------------------------------------------------------------------
\section*{Introduction}
I chose to build a chat bot for my senior project. It started as a side project that I happened upon while chatting with friends in a Discord chat. I started spending a lot of time on this project as I added more and more features to the bot.
My original plan for my senior project was to make a social beer rating site as an optimized mobile website (leveraging the latest browser tech to make it feel native). However, the more time I spent working on the bot, the more I dreaded starting on this site. I ended up deciding that I should commit to the project that I was more passionate about. After submitting a new project proposal, I continued work on my project.
My bot now has over 2000 lines of code and is able to fulfill many generic purposes that one would look for in a large chat group. The bot is now being used in over a dozen public Discord servers.
%------------------------------------------------
\section*{What I Learned}
The biggest thing that I've learned is that being excited about the project you're doing makes everything a whole lot easier and more fun.
In terms of tangible skills, I have learned a great deal about writing asynchronous code, as well as modern, style-compliant PHP, following the PHP Standards Recommendations by the PHP Framework Interop Group (FIG).
%\begin{wrapfigure}{l}{0.4\textwidth} % Inline image example
%\begin{center}
%\includegraphics[width=0.38\textwidth]{fish.png}
%\end{center}
%\caption{Fish}
%\end{wrapfigure}
%------------------------------------------------
\section*{What I would do differently}
The main thing that I would have done differently would have been to choose the framework to use based on something other than familiarity with the language. While I made the project quite hard for myself, I feel that some of the things that I built for my bot would have been much easier to complete in a different language or with a framework that has a larger support community (see the DiscordPHP section under Technologies Used).
\section*{Technologies Used}
\begin{itemize}
\item Discord API
\item DiscordPHP API Wrapper
\item ReactPHP Promises
\item Guzzle PHP HTTP client
\item PHP 7.1
\item Ubuntu Server 16.10
\item Composer Dependency Manager (PSR-4 class autoloading)
\end{itemize}
I chose to use PHP for my bot when I originally looked at creating a bot for Discord. There are API wrappers for most major programming languages. I felt the most comfortable in PHP because I've had the most experience with it.
DiscordPHP is the wrapper in PHP for the Discord HTTP/WebSocket API. It boasts nearly full coverage of the API, even including voice support for streaming audio to a voice channel within Discord. The downside to using the DiscordPHP framework is the poor documentation and relatively weak developer community. There is a group on Discord itself dedicated to DiscordPHP, but it is rarely active and I already find myself knowing more than most of the other users there. Additionally, the framework authors are seldom seen and have not resolved outstanding pull requests or made any new contributions on GitHub in over two months.
Since PHP is inherently synchronous, we need to account for the asynchronous nature of responding to commands and messages in a chat. Promises are a way to deal with operations that take differing amounts of time to complete. Promises represent the eventual value returned from the completion of an operation, and can be unfulfilled, fulfilled, or failed. The DiscordPHP framework uses the ReactPHP implementation of Promises to deal with asynchronous events.
PHP 7 offered a major improvement over PHP 5 in terms of speed - up to twice the speed in most use cases. There are also many deprecations of old extensions and modules as well as a few new syntactic sugar operators (my favorite is the null coalescence operator). A bug in the OpenSSL extension for PHP prevented \verb|fwrite()|s to non-blocking SSL sockets from completing. It was not found for so long due to the infrequency of non-blocking streams in PHP. Versions later than 7.1.4 include the patch for this bug.
I run the bot from my personal Ubuntu server. I run my own instance of GitLab and host close to a dozen websites with Apache. I learned how to problem solve the many different errors and bugs encountered when working with a headless server. Another obstacle I overcame with the hosting was the migration from my old server to this current machine. I built a new PC in the beginning of the year and decided to use my old desktop as the server instead of the laptop that was acting as the server at the time. I would gain CPU cores and double the RAM. It was an interesting challenge migrating all of my installed applications, sites, and configurations over to a new machine.
Composer is the de-facto standard for PHP package management. As any package manager should, Composer is able to install packages from \verb|packagist.org| to the current project, writing exact version information to the \verb|composer.lock| file. Other requirements can be specified in the \verb|composer.json| file. I have leveraged the PSR-4 class autoloading feature provided by Composer to specify namespaces for my PHP classes.
\section*{Software organization}
The source code for my bot is organized into classes under the \verb|src/| directory. Classes that contain code that will not be called as commands in Discord are in the root directory of \verb|src/|. Classes that contain commands are in \verb|src/Commands/|.
The \verb|Command| class describes a command that the bot will perform for certain inputs. It holds the name of the command as a string, which is compared to the message when it begins with the prefix \verb|;|. If a match is found, the callable of the command is called with the specified parameters. With the current structure, classes in \verb|src/Commands/| must be registered in the main \verb|BenBot| class to get the BenBot instance as a class variable. The \verb|register()| method in each of the Command classes then registers commands as needed to the BenBot instance.
The first iteration of my bot was a single file over 1000 lines long. This was unmanageable and difficult to work on. The only way to find a certain command or function was with the find function of my editor. The current structure is much easier to work with and I am able to add Command classes quickly and efficiently.
\section*{The Hardest Part}
\section*{Complex Data Structures and/or Algorithms}
\section*{Expected Difficulty}
\section*{Grade}
%----------------------------------------------------------------------------------------
% BIBLIOGRAPHY
%----------------------------------------------------------------------------------------
%\bibliographystyle{unsrt}
%\bibliography{sample}
%----------------------------------------------------------------------------------------
\end{document}

24
project_proposal/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
*.aux
*.glo
*.idx
*.log
*.toc
*.ist
*.acn
*.acr
*.alg
*.bbl
*.blg
*.dvi
*.glg
*.gls
*.ilg
*.ind
*.lof
*.lot
*.maf
*.mtc
*.mtc1
*.out
*.synctex.gz