forked from SFUAnime/Ren
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Restructure `QRChecker` according to SFUAnime#608. No functional changes. - Move constants to `constants.py`. - Remove redundant type hints (e.g., those fully inferrable from the return types of called functions).
- Loading branch information
1 parent
c634c8c
commit de6dcb6
Showing
7 changed files
with
191 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from redbot.core import commands | ||
from redbot.core.commands import Context | ||
|
||
from .commandsCore import CommandsCore | ||
|
||
|
||
class CommandHandlers(CommandsCore): | ||
@commands.group(name="qrchecker") | ||
@commands.guild_only() | ||
@commands.admin_or_permissions(manage_guild=True) | ||
async def _grpQrChecker(self, ctx: Context): | ||
"""Configure QR code checker""" | ||
|
||
@_grpQrChecker.command(name="toggle") | ||
async def _cmdQrCheckerToggle(self, ctx: Context): | ||
await self.cmdQrCheckerToggle(ctx=ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from redbot.core.commands import Context | ||
|
||
from .constants import KEY_ENABLED | ||
from .core import Core | ||
|
||
|
||
class CommandsCore(Core): | ||
async def cmdQrCheckerToggle(self, ctx: Context): | ||
"""Toggle QR code checking""" | ||
guild = ctx.guild | ||
if not guild: | ||
return | ||
guildConfig = self.config.guild(guild) | ||
|
||
enabled: bool = await guildConfig.get_attr(KEY_ENABLED)() | ||
if enabled: | ||
await guildConfig.get_attr(KEY_ENABLED).set(False) | ||
await ctx.send("QR code checking is now **disabled** for this guild.") | ||
else: | ||
await guildConfig.get_attr(KEY_ENABLED).set(True) | ||
await ctx.send("QR code checking is now **enabled** for this guild.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
KEY_ENABLED = "enabled" | ||
|
||
BASE_GUILD = {KEY_ENABLED: False} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from logging import getLogger | ||
|
||
from redbot.core import Config | ||
from redbot.core.bot import Red | ||
|
||
from .constants import BASE_GUILD | ||
|
||
|
||
class Core: | ||
def __init__(self, bot: Red): | ||
self.bot = bot | ||
self.logger = getLogger("red.luicogs.QRChecker") | ||
self.config = Config.get_conf(self, identifier=5842647, force_registration=True) | ||
self.config.register_guild(**BASE_GUILD) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from discord import Message | ||
|
||
from redbot.core import commands | ||
|
||
from .eventsCore import EventsCore | ||
|
||
|
||
class EventHandlers(EventsCore): | ||
@commands.Cog.listener("on_message") | ||
async def _evtListener(self, message: Message): | ||
"""Find QR code in message attachments""" | ||
await self.evtListener(message=message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from io import BytesIO | ||
from typing import List | ||
|
||
from discord import AllowedMentions, Message | ||
from PIL import Image | ||
from pyzbar.pyzbar import Decoded, decode, ZBarSymbol | ||
|
||
|
||
from redbot.core.commands import Context | ||
from redbot.core.utils.chat_formatting import box, pagify | ||
|
||
from .constants import KEY_ENABLED | ||
from .core import Core | ||
|
||
|
||
class EventsCore(Core): | ||
async def evtListener(self, message: Message): | ||
"""Find QR code in message attachments""" | ||
if not message.guild: | ||
return | ||
# check if enabled | ||
if not await self.config.guild(message.guild).get_attr(KEY_ENABLED)(): | ||
self.logger.debug( | ||
"QR Checker disabled for %s (%s); return early", | ||
message.guild.name, | ||
message.guild.id, | ||
) | ||
return | ||
if not message.attachments: | ||
self.logger.debug("No attachments, return early") | ||
return | ||
for attachment in message.attachments: | ||
contentType = attachment.content_type | ||
if not contentType: | ||
self.logger.debug("Unknown content type, continue") | ||
continue | ||
elif contentType and "image" not in contentType: | ||
self.logger.debug("Not an image, continue") | ||
continue | ||
|
||
# At this point we decern that it's an image. | ||
try: | ||
fp = BytesIO(await attachment.read()) | ||
image = Image.open(fp) | ||
codes: List[Decoded] = decode(image, symbols=[ZBarSymbol.QRCODE]) | ||
self.logger.debug("Found %s codes", len(codes)) | ||
except Exception: | ||
self.logger.error("Couldn't check file.", exc_info=True) | ||
return | ||
|
||
if not codes: | ||
self.logger.debug("No QR codes found.") | ||
return | ||
|
||
self.logger.info( | ||
"%s#%s (%s) posted some QR code(s) in #%s (%s)", | ||
message.author.name, | ||
message.author.discriminator, | ||
message.author.id, | ||
message.channel.name, | ||
message.channel.id, | ||
) | ||
|
||
numQrCodes = len(codes) | ||
if numQrCodes == 1: | ||
code = codes[0] | ||
data: str = code.data.decode() | ||
if len(data) == 0: | ||
self.logger.debug("No data in QR code.") | ||
return | ||
if len(data) > 1900: | ||
contents = f"{data[:1900]}..." | ||
else: | ||
contents = data | ||
msg = ( | ||
f"Found a QR code from {message.author.mention}, " | ||
f"the contents are: {box(contents)}" | ||
) | ||
await message.reply( | ||
msg, mention_author=False, allowed_mentions=AllowedMentions.none() | ||
) | ||
else: | ||
hasData: bool = False | ||
pages: List[str] = [] | ||
pages.append( | ||
f"Found several QR codes from {message.author.mention}, their contents are:" | ||
) | ||
for code in codes: | ||
data: str = code.data.decode() | ||
if len(data) == 0: | ||
self.logger.debug("No data in QR code.") | ||
continue | ||
if len(data) > 1990: | ||
contents = f"{box(data[:1990])}..." | ||
else: | ||
contents = f"{box(data)}" | ||
pages.append(contents) | ||
hasData |= True | ||
|
||
if not hasData: | ||
self.logger.debug("No data in %s QR codes.", numQrCodes) | ||
return | ||
|
||
firstMessage: bool = True | ||
sentMessages: int = 0 | ||
|
||
ctx: Context = await self.bot.get_context(message) | ||
for textToSend in pagify("\n".join(pages), escape_mass_mentions=True): | ||
if firstMessage: | ||
await message.reply( | ||
textToSend, | ||
mention_author=False, | ||
allowed_mentions=AllowedMentions.none(), | ||
) | ||
firstMessage = False | ||
elif sentMessages > 10: | ||
self.logger.debug("Sent more than 10 messages, bail early") | ||
break | ||
else: | ||
await ctx.send(textToSend, allowed_mentions=AllowedMentions.none()) | ||
sentMessages += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters