Skip to content

Instantly share code, notes, and snippets.

@Arthurdw
Created September 2, 2021 15:18
Show Gist options
  • Save Arthurdw/e110ebbdafca388722f25ddb79c1dfb8 to your computer and use it in GitHub Desktop.
Save Arthurdw/e110ebbdafca388722f25ddb79c1dfb8 to your computer and use it in GitHub Desktop.
Current command implementation for Pincer
from typing import List, Dict, Any
from pincer.client import Client, middleware
from pincer.core.dispatch import GatewayDispatch
from pincer.core.http import HTTPClient
say_cmd = {
"name": "say",
"type": 1,
"description": "Send a message",
"options": [
{
"name": "message",
"description": "The message that must be sent",
"type": 3,
"required": True,
"value": "STRING"
}
]
}
class Bot(Client):
@Client.event
async def on_ready(self):
print(f"Started client on {self.bot}")
async with self.http as client:
res: List[Dict[str, Any]] = await client.get(
f"applications/{self.bot.id}/commands")
contains_say_cmd = any(cmd["name"] == "say" for cmd in res)
if not contains_say_cmd:
await self.http.post(f"applications/{self.bot.id}/commands",
say_cmd)
@middleware("interaction_create")
async def interaction_create_middleware(self, payload: GatewayDispatch):
return "on_interaction_invoke", [
payload.data["token"],
payload.data["id"],
payload.data["data"]["options"][0]["value"],
payload.data["member"]["user"]["id"]
]
@Client.event
async def on_interaction_invoke(self, _token: str, _id: str, message: str,
user_id: int):
async with HTTPClient(token) as client:
await client.post(f"interactions/{_id}/{_token}/callback", {
"type": 4,
"data": {"content": f"<@!{user_id}> said:\n {message}"}
})
if __name__ == "__main__":
Bot("XXXTOKENXXX").run()
@Arthurdw
Copy link
Author

Arthurdw commented Sep 9, 2021

Please make your commands with the command decorator from now on :)

@Arthurdw
Copy link
Author

This is how you can currently implement this:

from pincer import Client, command
from pincer.objects import MessageContext

class Bot(Client):
    @Client.event
    async def on_ready(self):
        print(f"Started client on {self.bot}")

    @command(description="Send a message")
    async def say(self, ctx: MessageContext, message: str):
        return f"{ctx.author.user.mention} said:\n{message}"

if __name__ == "__main__":
    Bot("XXXTOKENXXX").run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment