1
# SPDX-License-Identifier: MIT
7
from disnake import Event
8
from disnake.ext import commands
11
# n.b. the specific choice of events used in this file is irrelevant
14
return disnake.Client()
20
command_prefix=commands.when_mentioned,
21
command_sync_flags=commands.CommandSyncFlags.none(),
25
@pytest.fixture(params=["client", "bot"])
26
def client_or_bot(request):
27
return request.getfixturevalue(request.param)
33
def test_event(client_or_bot: disnake.Client) -> None:
34
assert not hasattr(client_or_bot, "on_message_edit")
37
async def on_message_edit(self, *args: Any) -> None:
40
assert client_or_bot.on_message_edit is on_message_edit # type: ignore
46
@pytest.mark.parametrize("event", ["thread_create", Event.thread_create])
47
def test_wait_for(client_or_bot: disnake.Client, event) -> None:
48
coro = client_or_bot.wait_for(event)
49
assert len(client_or_bot._listeners["thread_create"]) == 1
50
coro.close() # close coroutine to avoid warning
53
# Client.add_listener / Client.remove_listener
56
@pytest.mark.parametrize("event", ["on_guild_remove", Event.guild_remove])
57
def test_addremove_listener(client_or_bot: disnake.Client, event) -> None:
58
async def callback(self, *args: Any) -> None:
61
client_or_bot.add_listener(callback, event)
62
assert len(client_or_bot.extra_events["on_guild_remove"]) == 1
63
client_or_bot.remove_listener(callback, event)
64
assert len(client_or_bot.extra_events["on_guild_remove"]) == 0
67
def test_addremove_listener__implicit(client_or_bot: disnake.Client) -> None:
68
async def on_guild_remove(self, *args: Any) -> None:
71
client_or_bot.add_listener(on_guild_remove)
72
assert len(client_or_bot.extra_events["on_guild_remove"]) == 1
73
client_or_bot.remove_listener(on_guild_remove)
74
assert len(client_or_bot.extra_events["on_guild_remove"]) == 0
80
@pytest.mark.parametrize("event", ["on_guild_role_create", Event.guild_role_create])
81
def test_listen(client_or_bot: disnake.Client, event) -> None:
82
@client_or_bot.listen(event)
83
async def callback(self, *args: Any) -> None:
86
assert len(client_or_bot.extra_events["on_guild_role_create"]) == 1
89
def test_listen__implicit(client_or_bot: disnake.Client) -> None:
90
@client_or_bot.listen()
91
async def on_guild_role_create(self, *args: Any) -> None:
94
assert len(client_or_bot.extra_events["on_guild_role_create"]) == 1
97
# @commands.Cog.listener
98
@pytest.mark.parametrize("event", ["on_automod_rule_update", Event.automod_rule_update])
99
def test_listener(bot: commands.Bot, event) -> None:
100
class Cog(commands.Cog):
101
@commands.Cog.listener(event)
102
async def callback(self, *args: Any) -> None:
106
assert len(bot.extra_events["on_automod_rule_update"]) == 1
109
def test_listener__implicit(bot: commands.Bot) -> None:
110
class Cog(commands.Cog):
111
@commands.Cog.listener()
112
async def on_automod_rule_update(self, *args: Any) -> None:
116
assert len(bot.extra_events["on_automod_rule_update"]) == 1