What I'm trying to do: I have an on_message
event for a 'global chat' command that I have created. This would send messages to any server that was in the json, currently supporting images, multiple lines of text, and custom emojis (as long as the bot shared a server with said emoji).
My problem: As you can see in the image above, one person, 'S o u p', has their profile picture replaced with a duck emoji. For context's sake, their avatar is not that of a duck emoji. However, if you were to look at the most recent message, the profile picture is shown. I believe that the problem lies in the size of the message.author
's avatar. The PIL library may be a good solution, but I do not want to save the image.
Code I have tried:
avatar_bytes = await message.author.avatar_url.read() guildem = client.get_guild(738285544911405107) try: em = await guildem.create_custom_emoji(name=f'{message.author.name}', image=avatar_bytes) except: em = '🦆'
The code above is the one I had used in the image example I had provided.
avatar = message.author.avatar_url_as(size=128) # resize avatar avatar_bytes = await avatar.read() # then read as bytes?
Above code gives exact same results. Avatars are turned into duck emojis.
Others I have looked at:
- How to crop an image to a shape for eg. circle using Pillow in Discord.py?: Messages did not send when using the code here. I assumed that using the PIL library would help reduce the file size significantly one way or the other without saving the image.
- How to reduce the image file size using PIL: All the answers required saving the image, which I do not want to do. I tried tweaking the code a little bit, but caused all avatars to be turned into duck emojis.
If necessary, I will include the entire on_message
event. However, this does not feel relevant nor necessary to include since the emojis are only a small portion of said event.
Edit: Here is the code to the entire on_message
event.
@client.event async def on_message(message): if message.author == client.user: # stop bot from replying to self return else: if not message.guild: # prevent errors when messaging in bot dm's return f = open("global text.json") # existing json """ { "747061937673732097": 765059798131539988, "724783642546536458": 818707151122989119, "761524419003809832": 813963773295591485, "755309786232258630": 760381389685784587, "738285544911405107": 738285544911405110 } """ data = json.load(f) for i in data: if str(i) == str(message.guild.id): if data[str(message.guild.id)] == message.channel.id: avatar_bytes = await message.author.avatar_url.read() guildem = client.get_guild(738285544911405107) # guild to upload emojis to try: em = await guildem.create_custom_emoji(name=f'{message.author.name}', image=avatar_bytes) # create emoji using avatar_bytes in guildem server except: em = '🦆' ### functions ### def censor(text): # censor words for safety banned = ["test"] # bad words were here, removed for stackoverflow reasons text = text.split() nearly = [] for word in text: if any(item in word.lower() for item in banned): word = word.lower() replaced = word.replace(word, ('#'*(len(word)))) nearly.append(replaced) else: nearly.append(word) message = ' '.join(nearly) return message def parag(string): # check for \n length = [string] if string in ["", " ", " ", " "]: return "<:transparent:774136812813811713>" final_list = [] if '\n' in string: length = string.split('\n') for item in length: thing = censor(item) final_list.append(thing) return paragger(final_list) def paragger(list: list): # returns original message with \n message = "" for item in list: message += f"> {item}\n" return message msg = parag(message.content) ### attachment handling ### embed_list = [] if message.attachments: if len(message.attachments) == 1: embed = discord.Embed(title=f"{message.author}'s file", color=0xc39ce6) for file in message.attachments: embed.set_image(url=file.url) embed_list.append(embed) else: count = 1 for file in message.attachments: embed = discord.Embed(title=f"{message.author}'s file {count}") embed.set_image(url=file.url) embed_list.append(embed) for i in data: if str(i) != str(message.guild.id): channel = client.get_channel(data[str(i)]) try: await channel.send(f"{em} {message.author} \n{msg}", allowed_mentions=allowed_mentions) if len(embed_list) > 0: for embed in embed_list: await channel.send(embed=embed) except: await channel.send(f"{em} {message.author} \n> {em}", allowed_mentions=allowed_mentions) # only happens if message is larger than 2000 char if em != '🦆': await em.delete() # delete the custom emoji to make space for more emojis later on break await client.process_commands(message) # process, prevent commands breaking
https://stackoverflow.com/questions/66938009/discord-py-changing-file-size-to-add-custom-emoji April 04, 2021 at 12:27PM
没有评论:
发表评论