ruff format
This commit is contained in:
parent
f569a826e2
commit
d618ebf51f
1 changed files with 40 additions and 35 deletions
75
main.py
75
main.py
|
@ -2,14 +2,10 @@
|
||||||
|
|
||||||
# Bitlair HobbyBot
|
# Bitlair HobbyBot
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from typing import Optional
|
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from string import Template
|
|
||||||
from discord import Intents
|
from discord import Intents
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord_webhook import DiscordWebhook, DiscordEmbed
|
from discord_webhook import DiscordWebhook, DiscordEmbed
|
||||||
from datetime import datetime
|
|
||||||
import pytz
|
import pytz
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import paho.mqtt.subscribe as subscribe
|
import paho.mqtt.subscribe as subscribe
|
||||||
|
@ -17,44 +13,47 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
mqtt_host = os.getenv('MQTT_HOST')
|
mqtt_host = os.getenv("MQTT_HOST")
|
||||||
if not mqtt_host:
|
if not mqtt_host:
|
||||||
print('MQTT_HOST unset')
|
print("MQTT_HOST unset")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
token = os.getenv('DISCORD_TOKEN')
|
token = os.getenv("DISCORD_TOKEN")
|
||||||
if not token:
|
if not token:
|
||||||
print('DISCORD_TOKEN unset')
|
print("DISCORD_TOKEN unset")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
webhook_url = os.getenv('DISCORD_WEBHOOK_URL')
|
webhook_url = os.getenv("DISCORD_WEBHOOK_URL")
|
||||||
if not webhook_url:
|
if not webhook_url:
|
||||||
print('DISCORD_WEBHOOK_URL unset')
|
print("DISCORD_WEBHOOK_URL unset")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
timezone = pytz.timezone('Europe/Amsterdam')
|
timezone = pytz.timezone("Europe/Amsterdam")
|
||||||
|
|
||||||
# Discord bot stuff
|
# Discord bot stuff
|
||||||
intents = Intents.default()
|
intents = Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
intents.members = True
|
intents.members = True
|
||||||
HobbyBot = commands.Bot(command_prefix='!', description='Bitlair Bot', intents=intents)
|
HobbyBot = commands.Bot(command_prefix="!", description="Bitlair Bot", intents=intents)
|
||||||
|
|
||||||
|
|
||||||
def mqtt_get_one(topic):
|
def mqtt_get_one(topic):
|
||||||
try:
|
try:
|
||||||
msg = subscribe.simple(topic, hostname=mqtt_host, keepalive=10)
|
msg = subscribe.simple(topic, hostname=mqtt_host, keepalive=10)
|
||||||
return msg.payload.decode()
|
return msg.payload.decode()
|
||||||
except err:
|
except Exception as err:
|
||||||
print(err)
|
print(err)
|
||||||
return ''
|
return ""
|
||||||
|
|
||||||
|
|
||||||
# Define bot commands
|
# Define bot commands
|
||||||
@HobbyBot.event
|
@HobbyBot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'Logged in as {HobbyBot.user} (ID: {HobbyBot.user.id})')
|
print(f"Logged in as {HobbyBot.user} (ID: {HobbyBot.user.id})")
|
||||||
|
|
||||||
|
|
||||||
# !state
|
# !state
|
||||||
@HobbyBot.command(description='Bitlair Space State')
|
@HobbyBot.command(description="Bitlair Space State")
|
||||||
async def state(ctx):
|
async def state(ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
spaceState = mqtt_get_one("bitlair/state")
|
spaceState = mqtt_get_one("bitlair/state")
|
||||||
|
@ -63,28 +62,33 @@ async def state(ctx):
|
||||||
elif spaceState == "closed":
|
elif spaceState == "closed":
|
||||||
await ctx.send("Bitlair is closed :pensive:")
|
await ctx.send("Bitlair is closed :pensive:")
|
||||||
|
|
||||||
|
|
||||||
# !co2
|
# !co2
|
||||||
@HobbyBot.command(description='co2 levels')
|
@HobbyBot.command(description="co2 levels")
|
||||||
async def co2(ctx):
|
async def co2(ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/co2_ppm")
|
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/co2_ppm")
|
||||||
await ctx.send("Hoofdruimte: %s ppm\n" % hoofdruimte)
|
await ctx.send("Hoofdruimte: %s ppm\n" % hoofdruimte)
|
||||||
|
|
||||||
|
|
||||||
# !temp
|
# !temp
|
||||||
@HobbyBot.command(description='Temperature')
|
@HobbyBot.command(description="Temperature")
|
||||||
async def temp(ctx):
|
async def temp(ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/temperature_c")
|
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/temperature_c")
|
||||||
await ctx.send("Hoofdruimte: %s °C\n" % hoofdruimte )
|
await ctx.send("Hoofdruimte: %s °C\n" % hoofdruimte)
|
||||||
|
|
||||||
|
|
||||||
# !humid
|
# !humid
|
||||||
@HobbyBot.command(description='Humidity')
|
@HobbyBot.command(description="Humidity")
|
||||||
async def humid(ctx):
|
async def humid(ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/humidity_pct")
|
hoofdruimte = mqtt_get_one("bitlair/climate/hoofdruimte_ingang/humidity_pct")
|
||||||
await ctx.send("Hoofdruimte: %s pct\n" % hoofdruimte)
|
await ctx.send("Hoofdruimte: %s pct\n" % hoofdruimte)
|
||||||
|
|
||||||
|
|
||||||
# !np
|
# !np
|
||||||
@HobbyBot.command(description='Now Playing')
|
@HobbyBot.command(description="Now Playing")
|
||||||
async def np(ctx):
|
async def np(ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
await ctx.send("Now playing: Darude - Sandstorm")
|
await ctx.send("Now playing: Darude - Sandstorm")
|
||||||
|
@ -106,12 +110,13 @@ def webhook_message(msg):
|
||||||
|
|
||||||
|
|
||||||
retained = {
|
retained = {
|
||||||
'bitlair/alarm',
|
"bitlair/alarm",
|
||||||
'bitlair/photos',
|
"bitlair/photos",
|
||||||
'bitlair/state',
|
"bitlair/state",
|
||||||
'bitlair/state/djo',
|
"bitlair/state/djo",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# post to mqtt discord channel when state changes
|
# post to mqtt discord channel when state changes
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
try:
|
try:
|
||||||
|
@ -124,17 +129,17 @@ def on_message(client, userdata, msg):
|
||||||
retained.remove(topic)
|
retained.remove(topic)
|
||||||
return
|
return
|
||||||
|
|
||||||
if topic == 'bitlair/alarm':
|
if topic == "bitlair/alarm":
|
||||||
webhook_message('Alarm: %s' % msg)
|
webhook_message("Alarm: %s" % msg)
|
||||||
elif topic == 'bitlair/state':
|
elif topic == "bitlair/state":
|
||||||
webhook_message('Bitlair is now %s' % msg.upper())
|
webhook_message("Bitlair is now %s" % msg.upper())
|
||||||
elif topic == 'bitlair/state/djo':
|
elif topic == "bitlair/state/djo":
|
||||||
webhook_message('DJO is now %s' % msg.upper())
|
webhook_message("DJO is now %s" % msg.upper())
|
||||||
elif topic == 'bitlair/photos':
|
elif topic == "bitlair/photos":
|
||||||
webhook = DiscordWebhook(url=webhook_url, rate_limit_retry=True)
|
webhook = DiscordWebhook(url=webhook_url, rate_limit_retry=True)
|
||||||
embed = DiscordEmbed(title='WIP Cam', color='fc5d1d')
|
embed = DiscordEmbed(title="WIP Cam", color="fc5d1d")
|
||||||
embed.set_url('https://bitlair.nl/fotos/view/' + msg)
|
embed.set_url("https://bitlair.nl/fotos/view/" + msg)
|
||||||
embed.set_image('https://bitlair.nl/fotos/photos/' + msg)
|
embed.set_image("https://bitlair.nl/fotos/photos/" + msg)
|
||||||
webhook.add_embed(embed)
|
webhook.add_embed(embed)
|
||||||
webhook.execute()
|
webhook.execute()
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue