-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
executable file
·101 lines (87 loc) · 3.46 KB
/
discord_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
# discordのbotです
# サーバで動かす場合、gcloud関連とdiscord関連でそれぞれセットアップをする必要があります
# https://qiita.com/thinceller/items/6bc7d28a04a8da75e818
# https://mattyan1053.hatenablog.com/entry/2019/02/18/205020
import asyncio
import os
from time import sleep
from googleapiclient import discovery
import discord
BOT_TOKEN = os.getenv('BOT_TOKEN')
# Instance information
# PROJECT = 'minecraft-server'
PROJECT = 'confident-slice-243412'
ZONE = 'asia-northeast1-b'
INSTANCE = 'mc-server'
# Build and nitialize google api
compute = discovery.build('compute', 'v1')
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
if message.content =="/cat":
await message.channel.send('にゃーん')
if message.content.startswith('/minecraft'):
# ex.) message.content: '/minecraft start' => command: 'start'
command = message.content.split(' ')[1]
if command == 'start':
m = await message.channel.send('Server starting up...')
start_server(PROJECT, ZONE, INSTANCE)
await m.edit(content='Success! Server started up.')
# sleep(10)
# await m.delete()
elif command == 'stop':
m = await message.channel.send( 'Server stopping...')
stop_server(PROJECT, ZONE, INSTANCE)
await m.edit(content='Success! Server stopped.')
# sleep(10)
# await m.delete()
elif command == 'status':
status = get_server_status(PROJECT, ZONE, INSTANCE)
if status == 'RUNNING':
m = await message.channel.send('Server is running! Please enjoy Minecraft!')
# sleep(10)
# await m.delete()
elif status in {'STOPPING', 'STOPPED'}:
m = await message.channel.send(
'Server is stopped. If you play Minecraft, please chat in this channel, `/minecraft start`.')
# sleep(10)
# await m.delete()
else:
m = await message.channel.send(
'Server is not running. Please wait for a while, and chat in this channel, `/minecraft start`.')
# sleep(10)
# await m.delete()
elif command == 'help':
m = '''
```Usage: /minecraft [start][stop][status]
start Start up minecraft server
stop Stop minecraft server
status Show minecraft server status(running or stopped)```
'''.strip()
await message.channel.send(m)
def start_server(project, zone, instance):
for _ in range(3):
try:
compute.instances().start(project=project, zone=zone, instance=instance).execute()
sleep(1)
break
except:
pass
def stop_server(project, zone, instance):
for _ in range(3):
try:
compute.instances().stop(project=project, zone=zone, instance=instance).execute()
sleep(1)
break
except:
pass
def get_server_status(project, zone, instance):
res = compute.instances().get(project=project, zone=zone, instance=instance).execute()
return res['status']
client.run(BOT_TOKEN)