Skip to content

Commit ca942b6

Browse files
committed
docs(gateway): add ping pong slash command example
1 parent 88dca70 commit ca942b6

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Ping pong - slash command edition
2+
## Environment variables
3+
The following environment variables is required
4+
5+
1. TOKEN
6+
This can be found in your developer dashboard.
7+
2. APPLICATION_ID
8+
This is usually the ID of your bot.
9+
10+
## Creating the commands
11+
Please run the [register_commands script](register_commands.py)
12+
13+
After that just run the example
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2022-present nextcore developers
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a
6+
# copy of this software and associated documentation files (the "Software"),
7+
# to deal in the Software without restriction, including without limitation
8+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
# and/or sell copies of the Software, and to permit persons to whom the
10+
# Software is furnished to do so, subject to the following conditions:
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
# DEALINGS IN THE SOFTWARE.
21+
22+
"""
23+
A simple "ping pong" example of using nextcore, but in slash commands.
24+
25+
This will respond with "pong" every time someone sends "ping" in the chat.
26+
"""
27+
28+
import asyncio
29+
from os import environ
30+
from typing import cast
31+
32+
from discord_typings import InteractionCreateData
33+
34+
from nextcore.gateway import ShardManager
35+
from nextcore.http import BotAuthentication, HTTPClient, Route
36+
37+
# Constants
38+
AUTHENTICATION = BotAuthentication(environ["TOKEN"])
39+
40+
# Intents are a way to select what intents Discord should send to you.
41+
# For a list of intents see https://discord.dev/topics/gateway#gateway-intents
42+
INTENTS = 0 # Guild messages and message content intents.
43+
44+
45+
# Create a HTTPClient and a ShardManager.
46+
# A ShardManager is just a neat wrapper around Shard objects.
47+
http_client = HTTPClient()
48+
shard_manager = ShardManager(AUTHENTICATION, INTENTS, http_client)
49+
50+
51+
@shard_manager.event_dispatcher.listen("INTERACTION_CREATE")
52+
async def on_interaction_create(interaction: InteractionCreateData):
53+
# Only accept application comamnds. See https://discord.dev/interactions/receiving-and-responding#interaction-object-interaction-type for a list of types
54+
if interaction["type"] != 2:
55+
return
56+
# Only respond to the ping command
57+
if interaction["data"]["name"] != "ping":
58+
return
59+
60+
response = {
61+
"type": 4,
62+
"data": {
63+
"content": "Pong!"
64+
}
65+
}
66+
67+
route = Route("POST", "/interactions/{interaction_id}/{interaction_token}/callback", interaction_id=interaction["id"], interaction_token = interaction["token"])
68+
# Authentication is interaction id and interaction token, not bot authenication
69+
await http_client.request(route, rate_limit_key=None, json=response)
70+
71+
72+
async def main():
73+
await http_client.setup()
74+
75+
# This should return once all shards have started to connect.
76+
# This does not mean they are connected.
77+
await shard_manager.connect()
78+
79+
# Raise a error and exit whenever a critical error occurs
80+
(error,) = await shard_manager.dispatcher.wait_for(lambda: True, "critical")
81+
82+
raise cast(Exception, error)
83+
84+
85+
asyncio.run(main())
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2022-present nextcore developers
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a
6+
# copy of this software and associated documentation files (the "Software"),
7+
# to deal in the Software without restriction, including without limitation
8+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
# and/or sell copies of the Software, and to permit persons to whom the
10+
# Software is furnished to do so, subject to the following conditions:
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
# DEALINGS IN THE SOFTWARE.
21+
22+
"""
23+
The register script for this example.
24+
25+
This will register the "ping" slash command we need for this example.
26+
27+
NOTE: This only needs to be run once per bot, and should be ran before the example.
28+
WARNING: This will remove all other commands
29+
"""
30+
31+
from __future__ import annotations # We want to use newer type hinting. If you are using python 3.9+ feel free to remove this.
32+
33+
import asyncio
34+
from os import environ
35+
from discord_typings import ApplicationCommandPayload
36+
from nextcore.http import BotAuthentication, Route, HTTPClient
37+
38+
# Constants
39+
AUTHENTICATION = BotAuthentication(environ["TOKEN"])
40+
APPLICATION_ID = environ["APPLICATION_ID"] # This should also usually be the same as your bots id.
41+
COMMANDS: list[ApplicationCommandPayload] = [ # TODO: Currently we have no TypedDict for this.
42+
{
43+
"name": "ping",
44+
"description": "Responds with pong!"
45+
}
46+
]
47+
48+
http_client = HTTPClient()
49+
50+
async def main() -> None:
51+
await http_client.setup()
52+
53+
route = Route("PUT", "/applications/{application_id}/commands", application_id=APPLICATION_ID)
54+
await http_client.request(route, rate_limit_key=AUTHENTICATION.rate_limit_key, headers=AUTHENTICATION.headers, json=COMMANDS)
55+
56+
print("Commands registered")
57+
58+
await http_client.close()
59+
60+
asyncio.run(main())

0 commit comments

Comments
 (0)