-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
54 lines (36 loc) · 1.68 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from __future__ import annotations
from logging import INFO, basicConfig
from typing import TYPE_CHECKING
from nextcord import Client, Color, Embed
from nextcord import Interaction as _Interaction
from cordcutter import Cordcutter, expect
basicConfig(level=INFO)
if TYPE_CHECKING:
Interaction = _Interaction[Client]
client = Client()
# By default a command breaker will pop after three errors and reset after one minute.
cordcutter = Cordcutter(client)
@client.slash_command(name="test")
async def test_command(interaction: Interaction) -> None: # noqa: ARG001
raise RuntimeError("This command always fails!")
@client.slash_command(name="other_test")
async def other_test_command(interaction: Interaction) -> None: # noqa: ARG001
# This command never trips its breaker
# Use the `expect` context manager to ignore errors that you expect to happen
# This is useful if other parts of your codebase are already handling the error
with expect(RuntimeError):
raise RuntimeError("This command always fails!")
@cordcutter.on_tripped_call
async def on_tripped(interaction: Interaction) -> None:
# In a classic circuit breaker, this is where you would show the user a message,
# that lets them know that the command is temporarily disabled.
# You should avoid accessing a database or any other external resources here,
# as that might be the reason why the breaker tripped in the first place.
await interaction.response.send_message(
embed=Embed(
title="⚡ Breaker tripped!",
description="This command is temporarily disabled due to encountering too many errors.",
color=Color.red(),
),
)
client.run("")