-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (88 loc) · 4.5 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
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
#-*- coding:utf-8 -*-
import asyncio
import discord
import os
import datetime
token = os.environ["POSE_BOT_TOKEN"]
app = discord.Client()
dict_interval_reminder = dict()
dict_remind_channel = dict()
dict_status_reminder = dict()
COMMAND_DESCRIPTION = "**!start_pose_reminder**\n" \
"- 해당 서버의 자세 알리미를 현재 채널로 설정하고 작동시킵니다.\n" \
"- 기본 알림 주기는 60분입니다.\n**" \
"!stop_pose_reminder**\n" \
"- 현재 서버의 자세 알리미를 중단합니다.\n" \
"**!set_pose_reminder_interval 주기(분)**\n" \
"- 현재 서버의 알림 주기를 설정합니다. (분 단위)"
# generator for sending notification
async def send_pose_remind(guild_id):
while True:
embed = discord.Embed(title="자세를 바로잡을 시간입니다!",
description="라운드 숄더와 거북목을 퇴치합시다",
color=0xe5c41e)
embed.set_image(url="https://i.imgur.com/ZnsjPNi.png")
# get current time for footer
now_time = str(datetime.datetime.now())
embed.set_footer(text=now_time)
# REMIND!
channel = dict_remind_channel[guild_id]
await channel.send(embed=embed)
print("Remind message has been sent to guild_id: %s, channel: %s(%s)" % (guild_id, channel.name, channel.id))
yield True
# work as timer
async def auto_reminder(guild_id):
async for _ in send_pose_remind(guild_id):
await asyncio.sleep(dict_interval_reminder[guild_id] * 60)
flag = dict_status_reminder[guild_id]
if not flag:
return
@app.event
async def on_ready():
print("Now log in as {nickname: %s, id: %s}" % (app.user.name, app.user.id))
# set gaming status
activity = discord.Game(name="거북목 퇴치!", type=1)
await app.change_presence(status=discord.Status.idle, activity=activity)
# set default intervals
for guild in app.guilds:
dict_interval_reminder[guild.id] = 60
dict_status_reminder[guild.id] = False
@app.event
async def on_message(message):
# Start reminder
if message.content.startswith("!start_pose_reminder"):
dict_remind_channel[message.guild.id] = message.channel
await message.channel.send("자세 알리미가 채널 %s에 설정되었습니다." % message.channel.name)
loop = asyncio.get_event_loop()
loop.create_task(auto_reminder(message.guild.id))
dict_status_reminder[message.guild.id] = True
print("Start on guild: %s(%d), channel: %s(%s)" % (message.guild.name, message.guild.id,
message.channel.name, message.channel.id))
# Stop reminder
elif message.content.startswith("!stop_pose_reminder"):
if message.guild.id in dict_remind_channel:
del dict_remind_channel[message.guild.id]
await message.channel.send("자세 알리미가 해제되었습니다.")
dict_status_reminder[message.guild.id] = False
print("Stopped on guild: %s(%d)" % (message.guild.name, message.guild.id))
# Set interval of reminder
elif message.content.startswith("!set_pose_reminder_interval"):
interval = int(message.content.replace("!set_pose_reminder_interval ", ""))
# input interval is valid
if 0 < interval:
if message.guild.id not in dict_remind_channel:
dict_interval_reminder[message.guild.id] = interval
await message.channel.send("주기가 %d분으로 변경되었습니다."% interval)
print("Interval has been changed on guild: %s(%s), channel: %s(%s)" % (message.guild.name, message.guild.id,
message.channel.name, message.channel.id))
else:
await message.channel.send("알리미가 이미 실행중입니다. 먼저 중단한 후 주기를 변경해주세요.")
# input interval is not valid
else:
await message.channel.send("입력하신 주기가 올바르지 않습니다. 1분 이상의 정수를 입력해주세요.")
elif message.content == "!help_pose_reminder":
embed = discord.Embed(title="명령어 목록",
description=COMMAND_DESCRIPTION,
color=0x49eaf2)
await message.channel.send(embed=embed)
app.run(token)