-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspacebot.py
More file actions
132 lines (104 loc) · 3.96 KB
/
spacebot.py
File metadata and controls
132 lines (104 loc) · 3.96 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Space Stauts Bot
# J Beeley Oct 2025
# Connect switch between GPIO4 and GND. Switch closed indicates space is open
# Connect SSD1306 OLED to I2C port 1 SCL/SCA, 3V3 and GND
# config.json format:
# {
# "glashack channel": 1234,
# "glashack token": "TOKEN"
# }
import discord, json
from discord.ext import tasks
from gpiozero import Button
from time import sleep
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep
import os
serial = i2c(port=1, address=0x3C)
device = ssd1306(serial, rotate=0)
ip_str = os.popen("ifconfig").read().split(" ")
# print(ip_str)
for c in range(0, len(ip_str)):
# print(c, ip_str[c])
if "wlan0" in ip_str[c]:
ip_addr=ip_str[c+13]
print(ip_addr)
...
with canvas(device) as draw:
draw.text((20, 20), ip_addr, fill="white")
try:
with open("config.json", mode="r", encoding="utf-8") as config_file:
config_json = json.load(config_file)
except IOError:
print("Unable to open config.json")
with canvas(device) as draw:
draw.text((20, 40), "Cant open config", fill="white")
exit()
button=Button(4)
count=0
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
global status
super().__init__(*args, **kwargs)
status=""
# an attribute we can access from our task
self.counter = 0
async def setup_hook(self) -> None:
# start the task to run in the background
self.my_background_task.start()
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
@tasks.loop(seconds=1) # task runs every 1 second
async def my_background_task(self):
global status
channel = self.get_channel(config_json["glashack channel"]) # channel ID goes here
if button.is_pressed: #Pin low - space open
if status=="" or status=="closed":
status="open"
print("Switch: Space open")
with canvas(device) as draw:
draw.text((20, 20), ip_addr, fill="white")
draw.text((20, 40), "Open", fill="white")
await channel.send("Space open 🟢")
else: #Pin high - space closed
if status=="" or status=="open":
status="closed"
print("Switch: Space closed")
with canvas(device) as draw:
draw.text((20, 20), ip_addr, fill="white")
draw.text((20, 40), "Closed", fill="white")
await channel.send("Space closed 🔴")
@my_background_task.before_loop
async def before_my_task(self):
await self.wait_until_ready() # wait until the bot logs in
status=""
client = MyClient(intents=discord.Intents.default())
@client.event
async def on_message(message):
global status
print("Status: ", status)
print(message.content,"XX",str(message.content))
if message.author == client.user:
return
print("message.author !=client.user")
# if (message.content.startswith('!status') or message.content.startswith('status')):
# if message.content=='status':
# if 'status' in message.content.lower():
if message.content=="!status":
print('if status=="open"')
if status=="open":
print("Discord mention: Space open")
with canvas(device) as draw:
draw.text((20, 20), ip_addr, fill="white")
draw.text((20, 40), "Open", fill="white")
await message.channel.send("Space open")
elif status=="closed":
print("Discord mention: Space closed")
with canvas(device) as draw:
draw.text((20, 20), ip_addr, fill="white")
draw.text((20, 40), "Closed", fill="white")
await message.channel.send("Space closed")
client.run(config_json["glashack token"])