-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (52 loc) · 2.08 KB
/
main.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
import os
import json
import random
import logging
import asyncio
import datetime
import discord
from dotenv import load_dotenv
from jsontypes import PeriodicEntry
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
handlers=[
logging.FileHandler("tsumiki.log"),
logging.StreamHandler()
]
)
class Tsumiki(discord.Client):
def __init__(self):
super().__init__()
self.voice_channel_id = os.environ["VOICE_CHANNEL_ID"]
self.text_channel_id = os.environ["TEXT_CHANNEL_ID"]
async def on_ready(self):
logging.info('Logged on as {0}!'.format(self.user))
self.loop.create_task(self.daily_rename_voice())
@staticmethod
async def __pick_random_element() -> PeriodicEntry:
with open("periodictable.json", "r", encoding="utf-8") as fh:
data = json.load(fh)
return PeriodicEntry.from_dict(random.choice(data["elements"]))
async def daily_rename_voice(self):
while True:
logging.info("Renaming voice and text channels")
voice_element: PeriodicEntry = await self.__pick_random_element()
voice_channel: discord.VoiceChannel = self.get_channel(int(self.voice_channel_id))
await voice_channel.edit(
name=voice_element.name,
reason=f"Daily rename to {voice_element.name} ({voice_element.symbol})"
)
text_element: PeriodicEntry = await self.__pick_random_element()
text_channel: discord.TextChannel = self.get_channel(int(self.text_channel_id))
await text_channel.edit(
name=f"general-{text_element.name.lower()}",
topic=text_element.summary,
reason=f"Daily rename to general-{text_element.name.lower()}"
)
remaining_seconds = 86400 - datetime.datetime.now().timestamp() % 86400
logging.info(f"Sleeping for {remaining_seconds} seconds")
await asyncio.sleep(remaining_seconds)
client = Tsumiki()
client.run(os.environ["TOKEN"])