import asyncio from unittest.mock import AsyncMock, MagicMock import pytest import discord from discord.ext import commands class MockUser: def __init__(self, id=12345, name="testuser", discriminator="0001"): self.id = id self.name = name self.discriminator = discriminator self.mention = f"@{name}" self.display_name = name self.avatar = None self.bot = False self.guild = None self._state = MagicMock() def __str__(self): return f"{self.name}#{self.discriminator}" class MockChannel: def __init__(self, id=11111, name="test-channel", type=discord.ChannelType.text): self.id = id self.name = name self.type = type self.mention = f"#{name}" self.guild = None self.category = None self._state = MagicMock() async def send(self, *args, **kwargs): return MagicMock() class MockTextChannel(MockChannel): def __init__(self, id=11111, name="test-channel"): super().__init__(id, name, discord.ChannelType.text) class MockVoiceChannel(MockChannel): def __init__(self, id=11112, name="test-voice"): super().__init__(id, name, discord.ChannelType.voice) self.voice_states = {} class MockGuild: def __init__(self, id=99999, name="Test Server"): self.id = id self.name = name self.channels = [] self.roles = [] self.emojis = [] self.me = MockUser(id=111111, name="Sen Bot") self._state = MagicMock() self._default_role = None def get_channel(self, channel_id): for ch in self.channels: if ch.id == channel_id: return ch return None def get_role(self, role_id): for role in self.roles: if role.id == role_id: return role return None def get_member(self, member_id): return None class MockMessage: def __init__(self, id=22222, content="test message", author=None, channel=None, guild=None): self.id = id self.content = content self.author = author or MockUser() self.channel = channel or MockChannel() self.guild = guild self.attachments = [] self.embeds = [] self.jump_url = "https://discord.com/channels/..." self.created_at = None async def reply(self, *args, **kwargs): return MagicMock() class MockInteraction: def __init__(self, user=None, channel=None, guild=None): self.user = user or MockUser() self.channel = channel or MockChannel() self.guild = guild or MockGuild() self.message = None self.response = AsyncMock() self.followup = AsyncMock() self.client = None self.id = 33333 self.token = "test_token" self._state = MagicMock() async def defer(self, *args, **kwargs): pass async def respond(self, *args, **kwargs): pass async def delete_original_response(self, *args, **kwargs): pass async def send(self, *args, **kwargs): return MagicMock() class MockApplicationContext: def __init__(self, bot=None, interaction=None, user=None, channel=None, guild=None): self.bot = bot self.interaction = interaction or MockInteraction(user=user, channel=channel, guild=guild) self.user = user or MockUser() self.channel = channel or MockChannel() self.guild = guild or MockGuild() self.message = None self.response = AsyncMock() self.followup = AsyncMock() self.client = self.bot self.id = 33333 self.token = "test_token" self._state = self.interaction._state self.author = self.user async def defer(self, *args, **kwargs): pass async def respond(self, *args, **kwargs): pass async def delete_original_response(self, *args, **kwargs): pass async def send(self, *args, **kwargs): return MagicMock() async def reply(self, *args, **kwargs): return MagicMock() async def send_followup(self, *args, **kwargs): return MagicMock() @pytest.fixture def mock_guild(): return MockGuild() @pytest.fixture def mock_channel(mock_guild): channel = MockTextChannel() channel.guild = mock_guild return channel @pytest.fixture def mock_voice_channel(mock_guild): channel = MockVoiceChannel() channel.guild = mock_guild return channel @pytest.fixture def mock_user(): return MockUser() @pytest.fixture def mock_member(mock_guild): member = MockUser() member.guild = mock_guild return member @pytest.fixture def mock_message(mock_channel, mock_user, mock_guild): return MockMessage(channel=mock_channel, author=mock_user, guild=mock_guild) @pytest.fixture def mock_interaction(mock_channel, mock_user, mock_guild): return MockInteraction(user=mock_user, channel=mock_channel, guild=mock_guild) @pytest.fixture def bot(): intents = discord.Intents.all() return commands.Bot(intents=intents, command_prefix="!", loop=asyncio.new_event_loop())