Skip to content

Commit

Permalink
Merge pull request cyclic-software#2 from seekayel/main
Browse files Browse the repository at this point in the history
Updated some more
  • Loading branch information
korostelevm authored Sep 9, 2022
2 parents 94f1ecb + 7861731 commit e09c95d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ After you authorize, the bot will appear in your server.



### **5.** Get your server ID
To get the server id, you first have to enable developer mode for in your discord user settings > advanced:
### **5.** Get your Server ID
To get the server id (Discord also calls this GUILD ID), you first have to enable developer mode for in your discord user settings > advanced:
<p align="center">
<img src="./doc/0.png" width="500px"/>
</p>
Expand All @@ -86,6 +86,11 @@ Developer mode allows you to right click all kinds of things in discord to get t

## **6.** Set up environment variables
At this point, make sure you've collected all the variables you will use in your environment
- CLIENT_ID (same as Application ID)
- PUBLIC_KEY
- TOKEN
- GUILD_ID (same as Server ID)

<p align="center">
<img src="./doc/8.png" width="600px"/>
</p>
Expand All @@ -94,16 +99,16 @@ At this point, make sure you've collected all the variables you will use in your
**This step is VERY IMPORTANT**, it lets discord know the url at which the bot is running. Discord will send interaction event payloads to this endpoint.

<p align="center">
<img src="./doc/9.png" width="200px"/>
<img src="./doc/9.png" width="600px"/>
</p>

When you first enter the endpoint into the ui, discord will make a cryptographically signed request to your endpoint and expect you to verify the signature. This will establish trust between discord and your hosted bot.

1. Make sure your bot is deployed to Cyclic
2. Before entering the endpoint **make sure** the environment variables have been properly configured and that `process.env.PUBLIC_KEY` is set.
3. Enter the endpoint url into discord and Save Changes
3. Enter the your Cyclic App url as the endpoint url into discord and Save Changes (eg `https://[your bot's url].cyclic.app/interactions`)

- The interactions endpoint is using the `verifyKeyMiddleware` function to verify the keys automatically -
- The `/interactions` endpoint is using the `verifyKeyMiddleware` function to verify the keys automatically

```js
app.post('/interactions', verifyKeyMiddleware(PUBLIC_KEY), async (req, res) => {
Expand All @@ -112,12 +117,12 @@ app.post('/interactions', verifyKeyMiddleware(PUBLIC_KEY), async (req, res) => {
# Register slash commands
1. Make sure all of the environment variables have been correctly set up
2. Make a `GET` request to
- `https://[your bot's url].cyclic.app/register_commands`
This route makes a put request to register a `/yo` command on your discord sever
2. Go to `https://[your bot's url].cyclic.app/register_commands`
This route makes a put request to register two commands on your server: `/yo` and `/dm`
```
app.get('/register_commands', async (req,res) =>{
```

Refer to the [discord docs](https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-guild-application-commands) to extend this further

After registering commands, you should see them available in your discord server
Expand All @@ -128,6 +133,11 @@ After registering commands, you should see them available in your discord server
</p>

# Try the bot!

In any channel type `/yo` or `/dm`.

NOTE: this starter does not configure

## `/yo`
<p align="center">
<img src="./doc/12.png" width="500px"/>
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ app.post('/interactions', verifyKeyMiddleware(PUBLIC_KEY), async (req, res) => {
},
});
}

if(interaction.data.name == 'dm'){
// https://discord.com/developers/docs/resources/user#create-dm
let c = (await discord_api.post(`/users/@me/channels`,{
recipient_id: interaction.member.user.id
recipient_id: interaction.member.user.id
})).data
try{
// https://discord.com/developers/docs/resources/channel#create-message
// https://discord.com/developers/docs/resources/channel#create-message
let res = await discord_api.post(`/channels/${c.id}/messages`,{
content:'Yo!',
content:'Yo! I got your slash command. I am not able to respond to DMs just slash commands.',
})
console.log(res.data)
}catch(e){
console.log(e)
}

return res.send({
// https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
// https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data:{
content:'👍'
Expand Down Expand Up @@ -110,4 +110,4 @@ app.get('/', async (req,res) =>{
app.listen(8999, () => {

})

0 comments on commit e09c95d

Please sign in to comment.