-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
105 lines (86 loc) · 3.62 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
103
104
105
import os
import logging
import discord
from discord.ext import commands, tasks
from datetime import datetime
import asyncio
import assets.settings.setting as setting
from glob import glob
token = os.environ["BOT_TOKEN"]
DAY_TIME = "06:00"
NIGHT_TIME = "18:00"
logger = setting.logging.getLogger("bot")
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
description = "Naichen bot."
super().__init__(
command_prefix=commands.when_mentioned_or('!'),
intents=intents,
description=description,
activity=discord.Activity(type=discord.ActivityType.listening, name="後藤さんの呪いだわ..."),
status=discord.Status.online
)
self.initial_extensions = [
"cogs.lofi_transformer_player"
]
self.day_avatar = "img/day_bocchi.jpg"
self.night_avatar = "img/night_bocchi.jpg"
self.init_avatar()
async def on_ready(self):
logger.info(f'Logged in as {self.user} (ID: {self.user.id})')
os.makedirs("./loop_file", exist_ok=True)
loop_files = glob("./loop_file/*")
for f in loop_files:
os.remove(f)
logger.info("Cleaned ./loop_file")
async def setup_hook(self) -> None:
self.update_avatar.start()
for ext in self.initial_extensions:
await self.load_extension(ext)
logger.info("Syncing command to global...")
cmds = await self.tree.sync()
logger.info(f"{len(cmds)} commands synced!")
def switch_avatar(self, is_day: True):
if is_day:
with open(self.day_avatar, 'rb') as image:
asyncio.get_event_loop().create_task(self.user.edit(avatar=image.read()))
logger.info(f'{self.user} changed its avatar to {self.day_avatar}!')
self.day_night_state = "day"
else:
with open(self.night_avatar, 'rb') as image:
asyncio.get_event_loop().create_task(self.user.edit(avatar=image.read()))
logger.info(f'{self.user} changed its avatar to {self.night_avatar}!')
self.day_night_state = "night"
def init_avatar(self):
now = datetime.now()
day_time = datetime.strptime(DAY_TIME, "%H:%M")
night_time = datetime.strptime(NIGHT_TIME, "%H:%M")
day_time = now.replace(hour=day_time.hour, minute=day_time.minute)
night_time = now.replace(hour=night_time.hour, minute=night_time.minute)
if day_time < now < night_time:
self.day_night_state = "day"
else:
self.day_night_state = "night"
@tasks.loop(seconds=10)
async def update_avatar(self):
if bot.is_closed():
logger.warn(f'{self.user} is offline now!')
return
now = datetime.strftime(datetime.now(), '%H:%M')
if now == DAY_TIME and bot.day_night_state == "night":
bot.switch_avatar(is_day=True)
elif now == NIGHT_TIME and bot.day_night_state == "day":
bot.switch_avatar(is_day=False)
bot = Bot()
@bot.event
async def on_voice_state_update(member, before, after):
voice_state = member.guild.voice_client
if voice_state is not None and len(voice_state.channel.members) == 1:
# If the bot is connected to a channel and the bot is the only one in the channel
logger.info(f"{bot.user} is getting out from voice channel.")
await voice_state.disconnect() # Disconnect the bot from the channel
if __name__ == "__main__":
bot.run(token, root_logger=True)