Level 6 - Student Getting Started

1 - Clone the repository and make a branch

There are multiple ways that you can choose to clone your project and create a branch. Below, you will find instructions for using the terminal, Eclipse IDE, and IntelliJ IDE


Terminal

Github Desktop provides nice tools for managing branches, but you can also try using the terminal to clone your repository. If you use GitHub desktop, make sure to create a new branch for you work. If you are interested in trying the terminal, you can use the following steps. Note that these instructions are written for Mac OS X, and certain commands such as "cd" will be different if you are using a windows computer:

  1. open the terminal

  2. navigate to your desktop
  3. cd desktop

  4. clone the repository
  5. git clone https://github.com/LEAGUE-Level6/DiscordBot_v2.git


    After this step, you should see your repository folder appear on the desktop
    Now we need to create a branch for you to work on


  6. Navigate inside of Discord bot folder
  7. cd DiscordBot_v2


  8. See every branch that the repository has
  9. git branch -a


  10. quit this branch view
  11. q


  12. create your own branch
  13. git checkout -b feature1/<yourName>


  14. check your active branch
  15. git branch


  16. Import into eclipse by going to file>import and selecting "Gradle", then "Existing Gradle Projects"

Eclipse

Eclipse does not support importing Gradle projects from git. You will need to clone the project using the termainal method (above) or GitHub Desktop. You can also use eGit to clone it, then delete the project from Eclipse and reimport it. After cloning the project, you will need to import it into Eclipse by going to the import menu and selecting Gradle>Existing Gradle Projects

  1. In the menu bar, go to file>import

  2. Select "git", then "Projects from Git"

  3. Select "Clone URI"

  4. Insert the repository's URL into the "URI" text field (https://github.com/LEAGUE-Level6/DiscordBot_v2.git)

  5. You will probably be prompted to enter your GitHub credentials to prove you have access to this repository

  6. Select the "development" branch

  7. Proceed through the remaining menu screens until it allows your to finish.

  8. Take note of where Eclipse cloned the project on your computer. You can right-click on the project and select "properties"

  9. Delete the project from eclipse, but do not remove it from your disk

  10. In the menu bar, go to file>import

  11. Select "Gradle", then "Existing Gradle Projects"

  12. Proceed through the remaining menu screens until it allows your to finish.

  13. Now we need to create a branch for you to work on


  14. Right-click on the project in the package explorer

  15. Go to team>switch to>new branch

  16. Enter a name for your branch (e.g. feature1/<yourName>)

  17. You should now see your branch's name displayed next to the project name in your package explorer

IntelliJ

  1. In the initial menu, press the "Get from VCS" button

  2. Enter the project's clone URL (https://github.com/LEAGUE-Level6/DiscordBot_v2.git)

  3. Now we need to create a branch for you to work on


  4. Click on the "development" branch name at the bottom-right of the IDE window

  5. Select "new branch"

  6. Enter a name for your branch (e.g. feature1/<yourName>)

2 - Create bot token on discord developers page

Navigate to the discord developer's portal. You will need to "create a new application". Then click on the "bot" tab on the left side of the screen and retrieve your bot's token

3 - Add environment variables

It is bad practice to put sensitive information directly into variables. In the case of this project, we have a discord token that provides access to our channel to anyone that possess it. We need to keep that secure and out of version control (i.e. Github).

Launcher.java in the DiscordBot application is set up to read your Discord token and channel name from environment variables. We need to create those environment variables and give them appropriate values. In Eclipse:

  • Run>Run Configurations
  • Click on the white rectangle with the plus sign to create a new launch configuration
  • Enter a name for the configuration (e.g. DiscordBot)
  • Select the Main class (i.e. Launcher.java)
  • Click on the "Environment" tab
  • Click "new", and add two new entries:
    1. name: DISCORD_TOKEN value:<your_discord_token>
    2. name: CHANNEL_NAME value:<your_channel_name>


In IntelliJ:
  • Run>Edit Configurations
  • Click on the plus sign to create a new launch configuration
  • Enter a name for the configuration (e.g. DiscordBot)
  • Select the Main class (i.e. Launcher.java)
  • Click on the page icon at the end of the Environment Variables text field
  • Click "+", and add two new entries:
    1. name: DISCORD_TOKEN value:<your_discord_token>
    2. name: CHANNEL_NAME value:<your_channel_name>

4 - Run the bot

Now run your program using your launch configuration

You should now see a link printed out in the console. You will need to send this in your discord channel so that your teacher can follow it and authorize your bot

Rerun your bot. The bot will send a "bot connected" message in your discord channel!

5 - Explore the code

There are two classes that you should be familiar with before you create your first feature:



DiscordBot_v2/src/main/java/org/jointheleague/discord_bot/DiscordBot.java

When you create a new feature, you will need to add it in this class. You will notice multiple lines that look like this:

addFeature(new RandomNumber(channelName));

This adds your feature to the bot. You will need to add a line like the one above, replacing "new RandomNumber" with "new <your_feature_class_name>()"



DiscordBot_v2/src/main/java/org/jointheleague/
features/examples/first_features/RandomNumber.java

This is an example of a simple feature. This class contains the code for a feature that listens for the command "!random" to be entered into the chat, then sends a random number as a response. The command that this feature listens for is saved in the "COMMAND" variable

private static final String COMMAND = "!random";

In the handle method, this class listens for the command to be issued

String messageContent = event.getMessageContent();
if (messageContent.startsWith(COMMAND)) {

It then creates an instance of Random, and uses it to send a message containing a random number

Random r = new Random();
event.getChannel().sendMessage("Your random number is " + r.nextInt(1001));

6 - How to do other things

For more examples of things you can do with the Discord API, you can take a look at other completed features in the same package. Our bot uses a wrapper for the Discord API called Javacord. The complete documentation for this wrapper can be found here: https://javacord.org/wiki/getting-started/welcome/

7 - Adding a feature

There is starter code available for your new feature class located at: features/templates/FeatureTemplate.java. You can place that inside of the appropriate package located in the "/features/student" package

8 - Adding unit tests for your feature

There is starter code available for your unit tests as well, this is located at: src/test/java/org/jointheleague/features/templates/FeatureTemplateTest.java. Notice that this is in the "test" source set, the root of which is at the same level as the "main" source set where you wrote your code. The structure of the packages within the "test" source set should be identical to the package structure from the "main" source set. You should place your new test in the package within the "test" source set that corresponds to your feature code's location within the "main" source set

9 - Saving your work

Terminal



  1. Reopen your terminal window.

  2. make sure you made changes to the right repository
  3. git status

    You should see the names of all the files your changed displayed in red. If you see a message saying this "is not a git repository", you probably need to navigate back to the correct directory. Go look at how we did this using "cd" before.


  4. stage your changes
  5. git add .

    the "." (period) at the end means "all"


  6. commit your changes
  7. git commit -m "Your commit message goes here"


  8. push your changes
  9. git push

    If it tells your to set the git remote, copy the line it gives you and run it. After successfully pushing your code, "git status" should tell you that your branch is up to date.

Eclipse


  1. Right-click on your project in the package explorer


  2. Select "team", and then "commit"


  3. Use the area that appears to complete your commit

IntelliJ


  1. Use command+k, or open the "Git" menu at the bottom of the screen and click on the checkmark for "commit"


  2. Use the area that appears to complete your commit


10 - Pull request

Once you finish your feature, you will need to submit a pull request to merge it back into development. You can do this from the pull requests tab of the repository on GitHub. Before submitting a pull request, review the "Additional Requirements" section of the Project Overview page. Failure to follow the guidelines there should result in your pull request being rejected.