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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from discord import app_commands
import discord
from discord.ext import commands
def test_group_with_commands():
my_group = app_commands.Group(name='mygroup', description='My group')
@my_group.command()
async def my_command(interaction: discord.Interaction) -> None:
...
assert my_command.binding is None
assert my_command.parent is my_group
assert my_group.commands[0] is my_command
def test_group_subclass_with_commands():
class MyGroup(app_commands.Group, name='mygroup'):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
my_group = MyGroup()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not my_group
assert my_group.my_command is not MyGroup.my_command
assert my_group.my_command.parent is my_group
def test_group_subclass_with_group():
class MyGroup(app_commands.Group, name='mygroup'):
sub_group = app_commands.Group(name='mysubgroup', description='My sub-group')
@sub_group.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
my_group = MyGroup()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not my_group
assert MyGroup.sub_group.parent is None
assert MyGroup.my_command.parent is MyGroup.sub_group
assert my_group.sub_group is not MyGroup.sub_group
assert my_group.my_command is not MyGroup.my_command
assert my_group.sub_group.parent is my_group
assert my_group.my_command.parent is my_group.sub_group
assert my_group.my_command.binding is my_group
def test_group_subclass_with_group_subclass():
class MySubGroup(app_commands.Group, name='mysubgroup'):
@app_commands.command()
async def my_sub_group_command(self, interaction: discord.Interaction) -> None:
...
class MyGroup(app_commands.Group, name='mygroup'):
sub_group = MySubGroup()
@app_commands.command()
async def my_group_command(self, interaction: discord.Interaction) -> None:
...
my_group = MyGroup()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not my_group
assert MySubGroup.__discord_app_commands_group_children__[0].parent is not my_group.sub_group
assert my_group.sub_group is not MyGroup.sub_group
assert my_group.my_group_command is not MyGroup.my_group_command
assert my_group.sub_group.my_sub_group_command is not MySubGroup.my_sub_group_command
assert my_group.sub_group.parent is my_group
assert my_group.my_group_command.parent is my_group
assert my_group.my_group_command.binding is my_group
assert my_group.sub_group.my_sub_group_command.parent is my_group.sub_group
assert not hasattr(my_group, 'my_sub_group_command')
assert my_group.sub_group.my_sub_group_command.binding is my_group.sub_group
def test_cog_with_commands():
class MyCog(commands.Cog):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert cog.my_command.parent is None
assert cog.my_command.binding is cog
def test_cog_with_group_with_commands():
class MyCog(commands.Cog):
my_group = app_commands.Group(name='mygroup', description='My group')
@my_group.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert cog.my_group is not MyCog.my_group
assert cog.my_command is not MyCog.my_command
assert cog.my_group.parent is None
assert cog.my_command.parent is cog.my_group
assert cog.my_command.binding is cog
def test_cog_with_nested_group_with_commands():
class MyCog(commands.Cog):
first = app_commands.Group(name='test', description='Test 1')
second = app_commands.Group(name='test2', parent=first, description='Test 2')
@first.command(name='cmd')
async def test_cmd(self, interaction: discord.Interaction) -> None:
...
@second.command(name='cmd2')
async def test2_cmd(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert len(MyCog.__cog_app_commands__) == 1
assert cog.first.parent is None
assert cog.first is not MyCog.first
assert cog.second is not MyCog.second
assert cog.second.parent is cog.first
assert cog.test_cmd.parent is cog.first
assert cog.test2_cmd.parent is cog.second
assert cog.test_cmd.binding is cog
assert cog.test2_cmd.binding is cog
def test_cog_with_group_subclass_with_commands():
class MyGroup(app_commands.Group, name='mygroup'):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
class MyCog(commands.Cog):
my_group = MyGroup()
@my_group.command()
async def my_cog_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not cog.my_group
assert cog.my_group is not MyCog.my_group
assert cog.my_group.my_command is not MyGroup.my_command
assert cog.my_cog_command is not MyCog.my_cog_command
assert not hasattr(cog.my_group, 'my_cog_command')
assert cog.my_group.parent is None
assert cog.my_group.my_command.parent is cog.my_group
assert cog.my_group.my_command.binding is cog.my_group
assert cog.my_cog_command.parent is cog.my_group
assert cog.my_cog_command.binding is cog
def test_cog_with_group_subclass_with_group():
class MyGroup(app_commands.Group, name='mygroup'):
sub_group = app_commands.Group(name='mysubgroup', description='My sub-group')
@sub_group.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
class MyCog(commands.Cog):
my_group = MyGroup()
@my_group.command()
async def my_cog_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not cog.my_group
assert cog.my_group is not MyCog.my_group
assert cog.my_group.sub_group is not MyGroup.sub_group
assert cog.my_group.my_command is not MyGroup.my_command
assert cog.my_cog_command is not MyCog.my_cog_command
assert not hasattr(cog.my_group, 'my_cog_command')
assert not hasattr(cog, 'sub_group')
assert not hasattr(cog, 'my_command')
assert cog.my_group.parent is None
assert cog.my_group.sub_group.parent is cog.my_group
assert cog.my_group.my_command.parent is cog.my_group.sub_group
assert cog.my_group.my_command.binding is cog.my_group
assert cog.my_cog_command.parent is cog.my_group
assert cog.my_cog_command.binding is cog
def test_cog_with_group_subclass_with_group_subclass():
class MySubGroup(app_commands.Group, name='mysubgroup'):
@app_commands.command()
async def my_sub_group_command(self, interaction: discord.Interaction) -> None:
...
class MyGroup(app_commands.Group, name='mygroup'):
sub_group = MySubGroup()
@app_commands.command()
async def my_group_command(self, interaction: discord.Interaction) -> None:
...
class MyCog(commands.Cog):
my_group = MyGroup()
@my_group.command()
async def my_cog_command(self, interaction: discord.Interaction) -> None:
...
@my_group.sub_group.command()
async def my_sub_group_cog_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyGroup.__discord_app_commands_group_children__[0].parent is not cog.my_group
assert MySubGroup.__discord_app_commands_group_children__[0].parent is not cog.my_group.sub_group
assert cog.my_group is not MyCog.my_group
assert cog.my_group.my_group_command is not MyCog.my_group.my_group_command
assert cog.my_group.sub_group is not MyGroup.sub_group
assert cog.my_cog_command is not MyCog.my_cog_command
assert not hasattr(cog.my_group, 'my_cog_command')
assert not hasattr(cog, 'sub_group')
assert not hasattr(cog, 'my_group_command')
assert not hasattr(cog, 'my_sub_group_command')
assert not hasattr(cog.my_group, 'my_sub_group_command')
assert cog.my_group.sub_group.my_sub_group_command is not MyGroup.sub_group.my_sub_group_command
assert cog.my_group.sub_group.my_sub_group_command is not MySubGroup.my_sub_group_command
assert cog.my_group.sub_group.parent is cog.my_group
assert cog.my_group.my_group_command.parent is cog.my_group
assert cog.my_group.my_group_command.binding is cog.my_group
assert cog.my_group.sub_group.my_sub_group_command.parent is cog.my_group.sub_group
assert cog.my_group.sub_group.my_sub_group_command.binding is cog.my_group.sub_group
assert cog.my_cog_command.parent is cog.my_group
assert cog.my_cog_command.binding is cog
assert cog.my_sub_group_cog_command.parent is cog.my_group.sub_group
assert cog.my_sub_group_cog_command.binding is cog
def test_cog_group_with_commands():
class MyCog(commands.GroupCog):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyCog.__cog_app_commands__[0].parent is not cog
assert MyCog.__cog_app_commands__[0].parent is not cog.__cog_app_commands_group__
assert cog.my_command is not MyCog.my_command
assert cog.__cog_app_commands_group__ is not None
assert cog.__cog_app_commands_group__.parent is None
assert cog.my_command.parent is cog.__cog_app_commands_group__
def test_cog_group_with_group():
class MyCog(commands.GroupCog):
sub_group = app_commands.Group(name='mysubgroup', description='My sub-group')
@sub_group.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyCog.__cog_app_commands__[0].parent is not cog
assert MyCog.__cog_app_commands__[0].parent is not cog.__cog_app_commands_group__
assert cog.sub_group is not MyCog.sub_group
assert cog.my_command is not MyCog.my_command
assert cog.__cog_app_commands_group__ is not None
assert cog.__cog_app_commands_group__.parent is None
assert cog.sub_group.parent is cog.__cog_app_commands_group__
assert cog.my_command.parent is cog.sub_group
def test_cog_group_with_subclass_group():
class MyGroup(app_commands.Group, name='mygroup'):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
class MyCog(commands.GroupCog):
sub_group = MyGroup()
@sub_group.command()
async def my_cog_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyCog.__cog_app_commands__[0].parent is not cog
assert MyCog.__cog_app_commands__[0].parent is not cog.__cog_app_commands_group__
assert MyGroup.__discord_app_commands_group_children__[0].parent is not cog.sub_group
assert cog.sub_group is not MyCog.sub_group
assert cog.sub_group.my_command is not MyGroup.my_command
assert cog.my_cog_command is not MyCog.my_cog_command
assert not hasattr(cog.sub_group, 'my_cog_command')
assert cog.__cog_app_commands_group__ is not None
assert cog.__cog_app_commands_group__.parent is None
assert cog.sub_group.parent is cog.__cog_app_commands_group__
assert cog.sub_group.my_command.parent is cog.sub_group
assert cog.my_cog_command.parent is cog.sub_group
assert cog.my_cog_command.binding is cog
def test_cog_group_with_subclassed_subclass_group():
class MyGroup(app_commands.Group):
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
class MySubclassedGroup(MyGroup, name='mygroup'):
...
class MyCog(commands.GroupCog):
sub_group = MySubclassedGroup()
@sub_group.command()
async def my_cog_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert MyCog.__cog_app_commands__[0].parent is not cog
assert MyCog.__cog_app_commands__[0].parent is not cog.__cog_app_commands_group__
assert MyGroup.__discord_app_commands_group_children__[0].parent is not cog.sub_group
assert MySubclassedGroup.__discord_app_commands_group_children__[0].parent is not cog.sub_group
assert cog.sub_group is not MyCog.sub_group
assert cog.sub_group.my_command is not MyGroup.my_command
assert cog.sub_group.my_command is not MySubclassedGroup.my_command
assert cog.my_cog_command is not MyCog.my_cog_command
assert not hasattr(cog.sub_group, 'my_cog_command')
assert cog.__cog_app_commands_group__ is not None
assert cog.__cog_app_commands_group__.parent is None
assert cog.sub_group.parent is cog.__cog_app_commands_group__
assert cog.sub_group.my_command.parent is cog.sub_group
assert cog.my_cog_command.parent is cog.sub_group
assert cog.my_cog_command.binding is cog
def test_cog_group_with_custom_state_issue9383():
class InnerGroup(app_commands.Group):
def __init__(self):
super().__init__()
self.state: int = 20
@app_commands.command()
async def my_command(self, interaction: discord.Interaction) -> None:
...
class MyCog(commands.GroupCog):
inner = InnerGroup()
@app_commands.command()
async def my_regular_command(self, interaction: discord.Interaction) -> None:
...
@inner.command()
async def my_inner_command(self, interaction: discord.Interaction) -> None:
...
cog = MyCog()
assert cog.inner.state == 20
assert cog.my_regular_command is not MyCog.my_regular_command
# Basically the same tests as above... (superset?)
assert MyCog.__cog_app_commands__[0].parent is not cog
assert MyCog.__cog_app_commands__[0].parent is not cog.__cog_app_commands_group__
assert InnerGroup.__discord_app_commands_group_children__[0].parent is not cog.inner
assert InnerGroup.__discord_app_commands_group_children__[0].parent is not cog.inner
assert cog.inner is not MyCog.inner
assert cog.inner.my_command is not InnerGroup.my_command
assert cog.inner.my_command is not InnerGroup.my_command
assert cog.my_inner_command is not MyCog.my_inner_command
assert not hasattr(cog.inner, 'my_inner_command')
assert cog.__cog_app_commands_group__ is not None
assert cog.__cog_app_commands_group__.parent is None
assert cog.inner.parent is cog.__cog_app_commands_group__
assert cog.inner.my_command.parent is cog.inner
assert cog.my_inner_command.parent is cog.inner
assert cog.my_inner_command.binding is cog
def test_cog_hybrid_group_manual_command():
class MyCog(commands.Cog):
@commands.hybrid_group()
async def first(self, ctx: commands.Context) -> None:
...
@first.command(name='both')
async def second_both(self, ctx: commands.Context) -> None:
...
@first.app_command.command(name='second')
async def second_app(self, interaction: discord.Interaction) -> None:
...
client = discord.Client(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)
cog = MyCog()
tree.add_command(cog.first.app_command)
assert cog.first is not MyCog.first
assert cog.second_both is not MyCog.second_both
assert cog.second_app is not MyCog.second_app
assert cog.first.parent is None
assert cog.second_both.parent is cog.first
assert cog.second_app.parent is cog.first.app_command
assert cog.second_app.binding is cog
assert tree.get_command('first') is cog.first.app_command
first = tree.get_command('first')
assert isinstance(first, app_commands.Group)
both = first.get_command('both')
assert isinstance(both, app_commands.Command)
assert both.parent is first
assert both.binding is cog
second = first.get_command('second')
assert isinstance(second, app_commands.Command)
assert second.parent is first
assert second.binding is cog
def test_cog_hybrid_group_manual_nested_command():
class MyCog(commands.Cog):
@commands.hybrid_group()
async def first(self, ctx: commands.Context) -> None:
pass
@first.group()
async def second(self, ctx: commands.Context) -> None:
pass
@second.app_command.command()
async def third(self, interaction: discord.Interaction) -> None:
pass
client = discord.Client(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)
cog = MyCog()
tree.add_command(cog.first.app_command)
assert cog.first is not MyCog.first
assert cog.second is not MyCog.second
assert cog.third is not MyCog.third
assert cog.first.parent is None
assert cog.second.parent is cog.first
assert cog.third.parent is cog.second.app_command
assert cog.third.binding is cog
first = tree.get_command('first')
assert isinstance(first, app_commands.Group)
second = first.get_command('second')
assert isinstance(second, app_commands.Group)
third = second.get_command('third')
assert isinstance(third, app_commands.Command)
assert third.parent is second
assert third.binding is cog
def test_cog_hybrid_group_wrapped_instance():
class MyCog(commands.Cog):
@commands.hybrid_group(fallback='fallback')
async def first(self, ctx: commands.Context) -> None:
pass
@first.command()
async def second(self, ctx: commands.Context) -> None:
pass
@first.group()
async def nested(self, ctx: commands.Context) -> None:
pass
@nested.app_command.command()
async def child(self, interaction: discord.Interaction) -> None:
pass
cog = MyCog()
fallback = cog.first.app_command.get_command('fallback')
assert fallback is not None
assert getattr(fallback, 'wrapped', None) is cog.first
assert fallback.parent is cog.first.app_command
assert cog.second.app_command is not None
assert cog.second.app_command.wrapped is cog.second
|