forked from hs3city/glider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
94 lines (73 loc) · 2.75 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
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
import logging
import os
import traceback
import discord
import requests
from discord.ext import tasks
discord_token = os.getenv('DISCORD_TOKEN')
space_endpoint = os.getenv('SPACE_ENDPOINT')
channel_id = os.getenv('DISCORD_CHANNEL_ID')
avatars = {}
usernames = {
'closed': 'Closed',
'open': 'Open'
}
online_status = {
'closed': discord.Status.offline,
'open': discord.Status.online
}
people_indicator = '🧙'
channel_name = 'INFORMACJA 📑'
current_state = None
current_persons = None
# Logging configuration
logging.basicConfig(level=logging.INFO)
intents = discord.Intents.default()
client = discord.Client(intents=intents)
async def update_state(state, persons):
if client.user:
logging.info(f'Updating the presence to "{state}, {persons}"')
nick = f"{usernames[state]} ({persons} {people_indicator})" if state == 'open' and persons is not None \
else usernames[state]
for guild in client.guilds:
member = guild.get_member_named(client.user.name)
await member.edit(nick=nick)
channel = guild.get_channel(int(channel_id))
if channel:
channel_state = "🔒" if state == "closed" else f"🔓/{persons or '?'}"
await channel.edit(name=f"{channel_name} [{channel_state}]")
else:
logging.warning(f"Channel {channel_id} not found")
async def update_presence(state, persons):
global current_state, current_persons
if state != current_state or persons != current_persons:
await update_state(state, persons)
current_state = state
current_persons = persons
# Fire every minute
@tasks.loop(minutes=1)
async def is_there_life_on_mars():
logging.info('Checking the status')
spaceapi_json = requests.get(space_endpoint).json()
if spaceapi_json['state']['open']:
space_state = 'open'
else:
space_state = 'closed'
people = spaceapi_json['sensors']['people_now_present'][0]['value']
logging.info(f'Current status: {space_state} ({people} in da haus)')
await update_presence(space_state, people)
@client.event
async def on_ready():
for guild in client.guilds:
logging.info(f'{client.user} has connected to Discord server {guild} using endpoint {space_endpoint}!')
for state in ['closed', 'open']:
with open(f'res/glider_{state}.png', 'rb') as avatar:
avatars[state] = avatar.read()
try:
await client.user.edit(username='glider')
await client.user.edit(avatar=avatars['open'])
await client.change_presence(activity=discord.Activity(name="the Space", type=discord.ActivityType.watching))
except:
logging.error(traceback.format_exc())
is_there_life_on_mars.start()
client.run(discord_token)