-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
102 lines (79 loc) · 3.21 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
95
96
97
98
99
100
101
102
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 = 'space-is'
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)
# Getting channel ID and setting status for it
channel = guild.get_channel(int(channel_id))
if channel:
# Setting lock emoji and actual status
lock_icon = '🔴🔒' if state == 'closed' else '🟢🔓'
channel_state = 'closed' if state == 'closed' else f"open-{persons or '?'}"
formatted_channel_name = f'{lock_icon}-{channel_name}-{channel_state}'
# Setting actual status
await channel.edit(name=formatted_channel_name)
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'
try:
people = int(spaceapi_json['sensors']['people_now_present'][0].get('value', 0))
except (TypeError, ValueError) as e:
logging.warning(f'Failed to parse people_now_present value: {e}')
people = 0
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)