Fix Image command

Mobile status
This commit is contained in:
Michał Gdula 2023-09-09 00:45:26 +01:00
parent dd3b322e68
commit 7b7f30067b
3 changed files with 89 additions and 19 deletions

View file

@ -2,13 +2,16 @@ import asyncio
import discord
from discord.ext import commands
from discord.gateway import DiscordWebSocket
from lynxie.config import DISCORD_TOKEN, LYNXIE_PREFIX
from lynxie.commands import Help, Ping, Hello, Music, Animals, Img
from lynxie.database import CommandHistory, Database
from lynxie.utils import mobile_status
db = Database()
DiscordWebSocket.identify = mobile_status
lynxie = commands.Bot(
intents=discord.Intents.all(),
command_prefix=LYNXIE_PREFIX,
@ -50,10 +53,10 @@ async def on_message_edit(before, after):
):
return
await before.channel.send(
f"@{before.author} edited their message!!!\n"
f'"{before.content}" --> "{after.content}"'
)
# await before.channel.send(
# f"@{before.author} edited their message!!!\n"
# f'"{before.content}" --> "{after.content}"'
# )
async def run():

View file

@ -1,7 +1,7 @@
import os
import datetime
import requests
from io import BytesIO
import datetime
from PIL import Image
@ -16,17 +16,30 @@ class Img(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._overlays = {
"bubble": Image.open(os.path.join(ASSETS_PATH, "bubble.png")).convert(
"RGBA"
),
"gang": Image.open(os.path.join(ASSETS_PATH, "gang.png")).convert("RGBA"),
"bubble": Image.open(os.path.join(ASSETS_PATH, "bubble.png")),
"gang": Image.open(os.path.join(ASSETS_PATH, "gang.png")),
}
@commands.command()
async def overlay(self, ctx, style: str = None):
start_time = datetime.datetime.now()
style = style.lower().strip() if style else None
image_attachments = None
if ctx.message.attachments:
image_attachments = ctx.message.attachments[0]
elif ctx.message.reference and ctx.message.reference.resolved.attachments:
image_attachments = ctx.message.reference.resolved.attachments[0]
elif ctx.message.embeds and ctx.message.embeds[0].image:
image_attachments = ctx.message.embeds[0].image
else:
async for message in ctx.guild.get_channel(ctx.channel.id).history(limit=10):
if message.attachments:
image_attachments = message.attachments[0]
break
elif message.embeds and message.embeds[0].image:
image_attachments = message.embeds[0].image
break
# Check if image should be processed
async with ctx.typing():
@ -38,14 +51,26 @@ class Img(commands.Cog):
await ctx.reply(embed=error_message(error))
return
if not ctx.message.attachments:
if not image_attachments:
error = "You need to attach an image to use this command!"
await ctx.reply(embed=error_message(error))
return
# Extracts file extension from filename
# Extracts file extension from filename or url
if (
not ctx.message.attachments[0].filename.split(".")[-1].lower()
image_attachments.filename
and not image_attachments.filename.split(".")[-1].lower()
in IMAGE_EXTENSIONS
):
error = (
"Unsupported file type! Supported file types are:\n"
f"`{', '.join(IMAGE_EXTENSIONS)}`"
)
await ctx.reply(embed=error_message(error))
return
elif (
image_attachments.url
and not image_attachments.url.split(".")[-1].lower()
in IMAGE_EXTENSIONS
):
error = (
@ -55,7 +80,10 @@ class Img(commands.Cog):
await ctx.reply(embed=error_message(error))
return
if ctx.message.attachments[0].size > 8_000_000:
if (
image_attachments.size
and image_attachments.size > 8 * 1024 * 1024
):
error = (
"That image is too big! Please use an image that is less than 8MB."
)
@ -63,16 +91,14 @@ class Img(commands.Cog):
return
if (
not 0 < ctx.message.attachments[0].width <= 3500
or not 0 < ctx.message.attachments[0].height <= 3500
not 0 < image_attachments.width <= 3500
or not 0 < image_attachments.height <= 3500
):
error = "Image must be at least 1x1 and under 3500x3500!"
await ctx.reply(embed=error_message(error))
return
# Process image
async with ctx.typing():
response = requests.get(ctx.message.attachments[0].url)
response = requests.get(image_attachments.url)
message_attachment = Image.open(BytesIO(response.content))
if message_attachment.width < message_attachment.height:

View file

@ -1,6 +1,47 @@
import os
import sys
import dotenv
import discord
from discord.gateway import _log
async def mobile_status(self):
payload = {
'op': self.IDENTIFY,
'd': {
'token': self.token,
'properties': {
'$os': sys.platform,
'$browser': 'Discord Android',
'$device': 'Discord Android',
'$referrer': '',
'$referring_domain': ''
},
'compress': True,
'large_threshold': 250,
'v': 3
}
}
if self.shard_id is not None and self.shard_count is not None:
payload['d']['shard'] = [self.shard_id, self.shard_count]
state = self._connection
if state._activity is not None or state._status is not None:
payload['d']['presence'] = {
'status': state._status,
'game': state._activity,
'since': 0,
'afk': False
}
if state._intents is not None:
payload['d']['intents'] = state._intents.value
await self.call_hooks('before_identify', self.shard_id, initial=self._initial_identify)
await self.send_as_json(payload)
_log.info('Shard ID %s has sent the IDENTIFY payload.', self.shard_id)
def error_message(error: str) -> discord.Embed: