Learn how to write a Discord bot that can get the weather forecast.
<application_id>
in the URL with the ID you copied beforehand):
permissions=8
so that it can do everything on the
server. If you are adding to another server, select only the permissions it
needs.
We now have a bot on our server:
cargo install cargo-shuttle
. Afterwards, run the following in an empty
directory:
src/main.rs
:
Copy Server ID
(you will need developer mode enabled to do this):
async fn message
hook as we won’t be using it in
this example, and then configure the gateway intents to not use any, as we won’t be needing them.
ready
hook we will call set_commands
to register a command with Discord.
Here we register a hello
command
with a description and no parameters (Discord refers to these as options).
If you are working on a larger command application, poise (which builds on Serenity) might be better suited.With our command registered, we will now add a hook for when these commands are called using
interaction_create
.
Secrets.toml
file with a key value pair:
cargo add reqwest -F json
and deserialize the results
to structures using serde, with cargo add serde
. We will then have a function
that chains the two requests together and deserializes the forecast to a
readable result.
You can skip some of the boilerplate by using direct access on untyped values. But we will opt for the better strongly typed structured approach.Here we type some of the structures returned by the API and add
#[derive(Deserialize)]
so they can be decoded from JSON. All the keys are in
PascalCase so we use the #[serde(rename_all = "PascalCase")]
helper attribute
to stay aligned with Rust standards. Some are completely different from the Rust
field name so we use #[serde(alias = ...)]
on the field to set its matching
JSON representation.
The above skips a lot of the fields returned by the API, only opting for the ones we will use in this demo. If you wanted to type all the fields you could try the new type from JSON feature in rust-analyzer to avoid having to write as much.Our location request call also fails if the search we put in returns no places. We will create an intermediate type that represents this case and implements
std::error::Error
:
async
function that, given a
place and a client, will return the forecast along with the location:
reqwest
client and a place,
we can wire that into the bots logic.
get_forecast
requires a reqwest
Client and the weather API key. We will
add some fields to our bot for holding this data and initialize this in the
shuttle_runtime::main
function. Using the secrets feature we can get our
weather API key(we will also move the guild ID to the secrets file):
ready
hook, we can add an additional command alongside the existing hello
command:
Secrets.toml
file:
get_forecast
function: