forked from devmitch/heic2jpg-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
31 lines (24 loc) · 1.2 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import discord, os, subprocess
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
if message.author == self.user:
return
for attachment in message.attachments:
if attachment.url.lower().endswith('.heic') or attachment.url.lower().endswith('.heif'):
in_filename = "./tmp/" + str(attachment.id) + ".HEIC" # store all files with .HEIC by default
out_filename = "./tmp/" + str(attachment.id) + ".jpg"
# save file
await attachment.save(in_filename)
# convert file, send stdout output to /dev/null
subprocess.run(["heif-convert", "%s" % in_filename, "%s" % out_filename], stdout=subprocess.DEVNULL)
# upload file to same channel that send .heic file
await message.channel.send(file=discord.File(out_filename), reference=message)
# delete local files
os.remove(in_filename)
os.remove(out_filename)
if not os.path.exists("./tmp"):
os.makedirs("./tmp")
client = MyClient()
client.run(os.environ.get("DISCORD_TOKEN"))