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:
- open the terminal
- navigate to your desktop
- clone the repository
- Navigate inside of Discord bot folder
- See every branch that the repository has
- quit this branch view
- create your own branch
- check your active branch
- Import into eclipse by going to file>import and selecting "Gradle", then "Existing Gradle Projects"
cd desktop
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
cd DiscordBot_v2
git branch -a
q
git checkout -b feature1/<yourName>
git branch
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
- In the menu bar, go to file>import
- Select "git", then "Projects from Git"
- Select "Clone URI"
- Insert the repository's URL into the "URI" text field (https://github.com/LEAGUE-Level6/DiscordBot_v2.git)
- You will probably be prompted to enter your GitHub credentials to prove you have access to this repository
- Select the "development" branch
- Proceed through the remaining menu screens until it allows your to finish.
- Take note of where Eclipse cloned the project on your computer. You can right-click on the project and select "properties"
- Delete the project from eclipse, but do not remove it from your disk
- In the menu bar, go to file>import
- Select "Gradle", then "Existing Gradle Projects"
- Proceed through the remaining menu screens until it allows your to finish.
- Right-click on the project in the package explorer
- Go to team>switch to>new branch
- Enter a name for your branch (e.g. feature1/<yourName>)
- You should now see your branch's name displayed next to the project name in your package explorer
Now we need to create a branch for you to work on
IntelliJ
- In the initial menu, press the "Get from VCS" button
- Enter the project's clone URL (https://github.com/LEAGUE-Level6/DiscordBot_v2.git)
- Click on the "development" branch name at the bottom-right of the IDE window
- Select "new branch"
- Enter a name for your branch (e.g. feature1/<yourName>)
Now we need to create a branch for you to work on
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:
- name: DISCORD_TOKEN value:<your_discord_token>
- 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:
- name: DISCORD_TOKEN value:<your_discord_token>
- 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
- Reopen your terminal window.
- make sure you made changes to the right repository
- stage your changes
- commit your changes
- push your changes
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.
git add .
the "." (period) at the end means "all"
git commit -m "Your commit message goes here"
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
- Right-click on your project in the package explorer
- Select "team", and then "commit"
- Use the area that appears to complete your commit
IntelliJ
- Use command+k, or open the "Git" menu at the bottom of the screen and click on the checkmark for "commit"
- 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.