Refactor SilentMode Cog to improve message sending functionality. Replace direct method assignment with functools.partialmethod for better compatibility. Update _silent_send method to accept the messageable parameter, ensuring correct message context when sending silent messages.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
This commit is contained in:
parent
32401f31d8
commit
e549698124
1 changed files with 4 additions and 3 deletions
|
@ -2,6 +2,7 @@ from redbot.core import commands
|
||||||
from discord.ext.commands import Cog
|
from discord.ext.commands import Cog
|
||||||
from redbot.core.bot import Red
|
from redbot.core.bot import Red
|
||||||
import discord
|
import discord
|
||||||
|
import functools
|
||||||
|
|
||||||
|
|
||||||
class SilentMode(commands.Cog):
|
class SilentMode(commands.Cog):
|
||||||
|
@ -10,13 +11,13 @@ class SilentMode(commands.Cog):
|
||||||
def __init__(self, bot: Red):
|
def __init__(self, bot: Red):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.original_send = discord.abc.Messageable.send
|
self.original_send = discord.abc.Messageable.send
|
||||||
discord.abc.Messageable.send = self._silent_send
|
discord.abc.Messageable.send = functools.partialmethod(self._silent_send)
|
||||||
|
|
||||||
def cog_unload(self):
|
def cog_unload(self):
|
||||||
"""Restore the original send method when the cog is unloaded."""
|
"""Restore the original send method when the cog is unloaded."""
|
||||||
discord.abc.Messageable.send = self.original_send
|
discord.abc.Messageable.send = self.original_send
|
||||||
|
|
||||||
async def _silent_send(self, *args, **kwargs):
|
async def _silent_send(self, messageable, *args, **kwargs):
|
||||||
"""Override the send method to add @silent to every message."""
|
"""Override the send method to add @silent to every message."""
|
||||||
if args and isinstance(args[0], str):
|
if args and isinstance(args[0], str):
|
||||||
args = ("@silent " + args[0],) + args[1:]
|
args = ("@silent " + args[0],) + args[1:]
|
||||||
|
@ -25,7 +26,7 @@ class SilentMode(commands.Cog):
|
||||||
elif not kwargs.get("content") and (kwargs.get("embed") or kwargs.get("file")):
|
elif not kwargs.get("content") and (kwargs.get("embed") or kwargs.get("file")):
|
||||||
kwargs["content"] = "@silent"
|
kwargs["content"] = "@silent"
|
||||||
|
|
||||||
return await self.original_send(self, *args, **kwargs)
|
return await self.original_send(messageable, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
async def setup(bot: Red):
|
async def setup(bot: Red):
|
||||||
|
|
Loading…
Add table
Reference in a new issue