|
| 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()) |
0 commit comments