Skip to content

Instantly share code, notes, and snippets.

@ASPJulien
Created November 9, 2022 17:46
Show Gist options
  • Save ASPJulien/5542ee2f89b1bf0e84e91767a9149e06 to your computer and use it in GitHub Desktop.
Save ASPJulien/5542ee2f89b1bf0e84e91767a9149e06 to your computer and use it in GitHub Desktop.
import discord
import aiohttp
import sys
import os
from datetime import datetime
import json
import asyncio
from ast import literal_eval
# Getting all variables set.
batterylevel = int(sys.argv[1])
iosver = sys.argv[2]
user = sys.argv[3]
isCharging = sys.argv[4] == 'True'
config = json.loads(open("config.json", "r", encoding='utf-8').read())
# Exit script if user is not in the authorized list.
if user not in config['phones'].keys():
os._exit(0)
# Set levelemote into the correct emoji corresponding to the battery level.
if int(batterylevel) >= 50:
levelemote = ":battery:"
else:
levelemote = ":low_battery:"
# Read the last charge timestamp and set timestamp value to the actual one.
timestamp = datetime.timestamp(datetime.now())
timevalue = int(open("last" + user, "r").read())
delta = int((timestamp - timevalue) / 60)
if delta > 120:
delta = f"{int(delta / 60)} heures"
else:
delta = f"{delta} minutes"
# Text depedent on charging state
if isCharging:
titleplugtext = "a branché"
timeplugtext = "Dernier chargement:"
else:
timeplugtext = "A chargé pendant:"
titleplugtext = "a débranché"
# Color depending on battery level.
couleur = 0xFF0C00
for i in range(len(config['colors'])):
if batterylevel >= (1 + i) * 100 / (len(config['colors']) + 1):
couleur = literal_eval(config['colors'][-(i + 1)])
# Bot thing.
async def bot():
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(config['phones'][user]['webhook'], session=session)
embed = discord.Embed(title=f"{levelemote} {user} {titleplugtext} son {config['phones'][user]['model']}",
color=couleur)
embed.add_field(name="Niveau de batterie:", value=f"{batterylevel}%")
embed.add_field(name="Version IOS:", value=iosver)
embed.add_field(name=timeplugtext, value=delta)
await webhook.send(embed=embed)
asyncio.run(bot())
# Write the actual timestamp.
f = open("last" + user, "w")
f.write(str(int(timestamp)))
f.close()
os._exit(0)
{
"phones": {
"weyki": {
"model": "iPhone XR 64 Go Blanc",
"webhook": ""
},
"carbon": {
"model": "iPhone 12 Pro Max 256 Go Gris Sidéral",
"webhook": ""
},
"Darky": {
"model": "iPhone 11 64 Go Noir",
"webhook": ""
}
},
"colors": [
"0x00FF00",
"0x5DFF00",
"0x88FF00",
"0xBFFF00",
"0xFFFA00",
"0xFFC300",
"0xFF9400",
"0xFF3200",
"0xFF3200"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment