-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew-event-listenor.py
116 lines (94 loc) · 4.07 KB
/
new-event-listenor.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
106
107
108
109
110
111
112
113
114
115
116
import json
import datetime
import discord
import ctypes
ctypes.windll.kernel32.SetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), 0x0007)
STRFTIME_ARG = "%Y-%m-%d %H:%M.%S"
def log(content: str = "Empty") -> str:
print(f"\033[032m[{datetime.datetime.now().strftime(STRFTIME_ARG)} / NEListen]", content,"\033[0m")
return f"[NEListen] {content}"
log("Loading...")
intents = discord.Intents.all()
client = discord.Client(intents=intents)
with open("env.json","r") as f:
ENV = json.loads(f.read())
BOT_TOKEN = ENV["bot_token"]
CONTENTS_FILE_PASS = "C:/Users/crab_/OneDrive/デスクトップ/codes/python-codes/auto-postings/contents.json"
METS_SERVER_ID = 842320961033601044
@client.event
async def on_ready():
# 変数
log_channel = client.get_channel(1074249516871602227)
now = datetime.datetime.now()
# 起動メッセージ
log("Ready!")
log(f"status:\n- at {now.strftime(STRFTIME_ARG)}\n- discord.py ver: {discord.__version__}")
on_ready_log_embed = discord.Embed(
title=f":white_check_mark: イベントが出来たら宣伝の内容に追加するやつが起動しました",
description=now.strftime(STRFTIME_ARG),
color=0x00ff00
)
await log_channel.send(embed=on_ready_log_embed)
@client.event
async def on_scheduled_event_create(event: discord.ScheduledEvent):
log("Catched new event")
if event.guild.id != METS_SERVER_ID:
log("Isn't mets event")
return
log("Catched event has mets event!, opening data-file...")
with open(CONTENTS_FILE_PASS, "r", encoding="utf-8") as file_read:
contents_json = json.loads(file_read.read())
contents_json["events"]["enabled"] = True
contents_json["events"]["contents"][f"{event.id}"] = {
"enable": True,
"name": event.name,
"description": discord.utils.remove_markdown(event.description),
"times":
{
"start": event.start_time.strftime(STRFTIME_ARG),
"end": event.end_time.strftime(STRFTIME_ARG)
}
}
with open(CONTENTS_FILE_PASS, "w", encoding="utf-8") as file_write:
file_write.write(json.dumps(contents_json))
log(f"Writed new event: {event.name}")
@client.event
async def on_scheduled_event_delete(event: discord.ScheduledEvent):
log("Catched deleted event")
if event.guild.id != METS_SERVER_ID:
log("Isn't mets event")
return
log("Catched event has mets event!, opening data-file...")
with open(CONTENTS_FILE_PASS, "r", encoding="utf-8") as file_read:
contents_json = json.loads(file_read.read())
contents_json["events"]["enabled"] = True
contents_json["events"]["contents"].pop(f"{event.id}")
if len(contents_json["events"]["contents"]) >= 0:
contents_json["events"]["enabled"] = False
with open(CONTENTS_FILE_PASS, "w", encoding="utf-8") as file_write:
file_write.write(json.dumps(contents_json))
log(f"Deleted event: {event.name}")
@client.event
async def on_scheduled_event_update(before: discord.ScheduledEvent, after: discord.ScheduledEvent):
log("Catched updated event")
if before.guild.id != METS_SERVER_ID:
log("Isn't mets event")
return
log("Catched event has mets event!, opening data-file...")
with open(CONTENTS_FILE_PASS, "r", encoding="utf-8") as file_read:
contents_json = json.loads(file_read.read())
contents_json["events"]["enabled"] = True
contents_json["events"]["contents"][f"{after.id}"] = {
"enable": True,
"name": after.name,
"description": discord.utils.remove_markdown(after.description),
"times":
{
"start": after.start_time.strftime(STRFTIME_ARG),
"end": after.end_time.strftime(STRFTIME_ARG)
}
}
with open(CONTENTS_FILE_PASS, "w", encoding="utf-8") as file_write:
file_write.write(json.dumps(contents_json))
log(f"Writed updated event: {after.name}({before.name})")
client.run(BOT_TOKEN)