-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordbot.py
109 lines (89 loc) · 3.19 KB
/
discordbot.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
from googleapiclient.discovery import build
import discord
import time
import os
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
SERVICE_ACCOUNT_ID = os.getenv("SERVICE_ACCOUNT_ID")
GCP_PROJECT_NAME = os.getenv("GCP_PROJECT_NAME")
MINECRAFT_INSTANCE_NAME = os.getenv("MINECRAFT_INSTANCE_NAME")
MINECRAFT_INSTANCE_ZONE = os.getenv("MINECRAFT_INSTANCE_ZONE")
client = discord.Client()
compute = build("compute", "v1")
# サーバー起動処理
def server_start():
compute.instances().start(
project=GCP_PROJECT_NAME,
zone=MINECRAFT_INSTANCE_ZONE,
instance=MINECRAFT_INSTANCE_NAME,
).execute()
return
# サーバー停止処理
def server_stop():
compute.instances().suspend(
project=GCP_PROJECT_NAME,
zone=MINECRAFT_INSTANCE_ZONE,
instance=MINECRAFT_INSTANCE_NAME,
).execute()
return
# サーバー情報取得
def server_status():
res = (
compute.instances()
.get(
project=GCP_PROJECT_NAME,
zone=MINECRAFT_INSTANCE_ZONE,
instance=MINECRAFT_INSTANCE_NAME,
)
.execute()
)
return [res["status"], res["networkInterfaces"][0]["accessConfigs"][0]["natIP"]]
@client.event
async def on_ready():
print("ログインしました")
@client.event
async def on_message(message):
if message.author.bot:
return
if message.content == "/start minecraft":
await message.channel.send("サーバーを開始します...")
server_start()
time.sleep(30)
result = server_status()
time.sleep(5)
if result[0] == "RUNNING":
await message.channel.send("サーバーは起動済みです\nIP: " + str(result[1]))
return
time.sleep(30)
result = server_status()
time.sleep(5)
if result[0] == "RUNNING":
await message.channel.send("サーバーは起動済みです")
return
if message.content == "/stop minecraft":
await message.channel.send("サーバーを停止します...")
server_stop()
time.sleep(30)
result = server_status()
time.sleep(5)
if result[0] in {"TERMINATED", "STOPPING", "SUSPENDED", "SUSPENDING"}:
await message.channel.send("サーバーを停止しました")
return
time.sleep(30)
result = server_status()
time.sleep(5)
if result[0] in {"TERMINATED", "STOPPING", "SUSPENDED", "SUSPENDING"}:
await message.channel.send("サーバーを停止しました")
return
if message.content == "/status minecraft":
await message.channel.send("サーバーの状態を確認中です...")
result = server_status()
time.sleep(5)
if result[0] == "RUNNING":
await message.channel.send("サーバーは起動済みです\nIP: " + str(result[1]))
if result[0] == "STAGING":
await message.channel.send("サーバーを起動中です。しばらくお待ちください")
if result[0] in {"TERMINATED", "STOPPING", "SUSPENDED", "SUSPENDING"}:
await message.channel.send("サーバーは停止しています")
client.run(TOKEN)