A Discord bot that allows you to search Google Images without leaving your channel! To search, enter /search
in a Discord channel followed by the term you want to search for. The bot will return the first image matching your search term.
SSW 345 | Team 2 | ||
---|---|---|---|
Serena Lee serenadia |
Mya Phu mxfu |
Tyler Seliber Tyler-Seliber |
Mehrab Syed msyed5 |
It is strongly recommended you make two applications/bots. One will be the actual bot used in production environments, while the other will be for development purposes. This allows you to safely develop and test your bot without impacting the bot that will be active in multiple other servers. In this case, please perform steps 1-4 below twice - once for the production bot and another for the development bot.
If you only plan on hosting a development bot and don't intend on distributing it globally, you can only create one bot/application by following the steps below, and following the rest of the instructions for using the dev
environment.
- Create a Dicord application and bot by following the official Discord.js documentation. Be sure to make a note of the bot's
TOKEN
. - Open the
OAuth2
tab and, under SCOPES, enablebot
andapplication.commands
. - Invite the bot to your server by following the official documentation. Use the same scopes selected in the previous step.
- On the Discord Developer Portal, navigate to your application and from the "General Information" tab, copy your
APPLICATION ID
. This will be theclientId
later. - Enable Developer Mode on Discord by:
- Open up the Discord app
- Click on the settings cog in the bottom left corner
- Go to Advanced
- Enable
Developer Mode
- Exit settings
- Right-click on your server from the left and click "Copy ID." This will be the
guildId
later.
As Google no longer allows direct APIs for Search, you will need to create your own Programmable Search Engine and create an API to make use of it.
- Login to the Programmable Search Engine Control Panel and create a new search engine.
- Give your search engine a name, and select
Search the entire
. Be sure to turn onImage search
and (optionally, but recommended)SafeSearch
. - Once created, make a note of the Search engine ID. This will be
cx
later.
The next step is to generate an API key for your search engine.
- Visit this page and click blue button that says Get a Key.
- In the menu that appears, click + Create a new project and enter a project name (ex. Discord Image Search Bot). Then click Next.
- Your API key will be displayed. Make a note of this, as it will be
auth
later.
- Clone the repository
- Create a new file called
.env
in the root of the repository and enter the following fields you took note of earlier. You can also retrieve them from the Discord Developer Portal, Programmable Search Engine Control Panel, and Google Cloud API Manager.Note: these keys should be from your development bot/application and server. Your production keys should be added as repository secrets by following the steps in Deploying your Commands below.CLIENT_ID=<clientId> GUILD_ID=<guildId> TOKEN=<token> GG_API_KEY=<auth> GG_CX=<cx>
- If it is not already there, create a new file called
.gitignore
and include:node_modules/** .env
- Run
npm install
Commands can be created and edited in the commands
folder using the appropriate command file. See ping.js as an example.
The commands
folder is meant to include only commands meant for production. To test and develop new commands, create them in the commands-dev
folder instead. Once they are ready to be pushed to production, they should be moved to the commands
folder. Any command files that remain in commands-dev
will not be pushed to production.
Any time new commands are created, they need to be registered. To do so, run npm run commands
. This will deploy the commands to your development server only. See Deploying your Commands below for more information.
To run the bot, run the command npm start
. This will run the bot in a production state, meaning it will not deploy your commands for you. To deploy your global commands for production, see Deploying your Commands below for more information.
To start the development bot, use npm run dev
. This will start the bot locally on your machine and also automatically deploy the commands to your development server.
To stop the bot, press CTRL-C
When you modify the name or description of your commands, or add/remove commands, the changes may not be reflected immediately after starting the bot. To get around this, you can use a GitHub Action to automatically deploy your commands when merging to main
, or manually running a script to deploy the commands.
The deploy-commands.yml file contains the code necessary for automatically deploying your bot's commands everytime a commit is pushed to main
or a PR is merged. To configure this, you will need to set up Repository Secrets. To do so, navigate to your repository's Settings and go to Secrets > Actions. Click "New Repository Secret" to create a new secret. You will need to create the same secrets that you entered in your .env
file: CLIENT_ID
, GUILD_ID
, and TOKEN
. Make sure that you are using the keys for your production bot/application.
To disable this functionality, you can delete the deploy-commands.yml file and commit the change to main
. Otherwise, this can remain and you can still deploy commands manually (see below) without causing any conflicts.
If you are developing locally and would like to test commands without committing them, you can do so by running npm run commands
command from the root of your directory. A confirmation message will appear in the console. If you use npm run dev
the development commands are automatically deployed and you do not need to run this command.
If you choose to not deploy your production commands automatically, you can manually do so by running node deploy-commands.js prod
. This will globally deploy your bots production commands from the commands
folder across Discord. It may take up to an hour for the updates to be reflected due to Discord caching global commands.