This article is a continuation of A story about a GCP beginner building a Minecraft server on GCE. If you haven't seen the last time, I would appreciate it if you could see it from there.
Well, since I built a Minecraft server with GCP last time, This time I would like to be able to operate it from discord.
Is there any merit to operate the instance from discord? I think, Ask someone playing Minecraft to join the discord channel Since it can be started and stopped on behalf of the administrator, the burden on the administrator can be reduced.
--Creating a server for bot --Create a service account for instance control --Create Discord Bot
The server used for the bot this time uses Google Compute Engine (GCE). GCE has Always Free resources that are completely free to use, and this time we'll use them. Since it is always free, you will not be charged even if it is always running, and this time there is no use other than bot, so this resource is sufficient for operation.
For more information on GCE's Always Free, please refer to the official documentation below.
The instance was created with such settings. Detailed settings will be omitted this time.
item | Contents |
---|---|
Instance name | botserver |
region | us-west1 |
zone | us-west1-a |
Machine type | f1-micro(vCPUx1,Memory 0.6GB) |
Boot disk | CentOS 7 |
IP address | Create a static IP address |
You can create a service account in GCP with only certain permissions to access projects and resources. For more information on service accounts, please refer to the official documentation below.
About Service Accounts (https://cloud.google.com/iam/docs/understanding-service-accounts?hl=ja#managing_service_accounts)
This time, give this service account permission to start and stop the instance (Minecraft server). First, create a "role" from "IAM and management".
Select IAM & Administration-> Roles-> Create Role and set the following:
item | Contents |
---|---|
title | Arbitrary title name |
Authority | compute.instances.get compute.instances.start compute.instances.stop |
** [Description of authority] **
item | Description |
---|---|
compute.instances.get | Permissions to log in to your Compute Engine instance |
compute.instances.start | Permission to launch a Compute Engine instance |
compute.instances.stop | Permission to stop a Compute Engine instance |
At this point, the creation of the "role" is complete. Next, create a "Service Account" from "IAM and Administration".
Select IAM & Administration-> Service Accounts-> Create Service Account and configure as follows:
item | Contents |
---|---|
Service account name | Any service account name |
Service account ID | Any service account ID@{ProjectID}.iam.gserviceaccount.com |
Service account description | Write a description to make it easier for you to understand |
Role selection | The "role" created earlier |
Creating a key | JSON type *Click Create and download the json file. |
Please do not lose the json file downloaded when creating the key, as you will need it later.
First, create a developer account for your bot at the Discord Developer Portal.
I referred to the following articles for various settings from the creation of a developer account.
Log in to the instance created in 1. and make various settings.
First, install the required packages.
#CentOS7 does not have python3 installed by default, so install python3
$ yum install python3
# discord.py installation
$ python3 -m pip install -U discord.py
Next, authenticate the service account you created earlier.
#Upload the key file (json file) downloaded when creating the service account to the bot server, and authenticate with the following command.
$ gcloud auth activate-service-account --key-file "./<projectName>-xxxxxx.json"
#Check if the service account is active.*OK if your account is active
$ gcloud auth list
#Execution example below
---------------------------------------------------------------
ACTIVE ACCOUNT
* <Service account name>@<Project name>.iam.gserviceaccount.com
---------------------------------------------------------------
Creating a bot program
mineserver-op.py
#Installed discord.Load py
import discord
import os
import time
#Your bot's access token
TOKEN = '<Access token>'
client = discord.Client()
#Processing that operates at startup
@client.event
async def on_ready():
#When started, a login notification will be displayed in the terminal
print('Yaho! I logged in!')
print('/You can check the command with help')
#Processing that operates when receiving a message
@client.event
async def on_message(message):
#Do not process if the message sender is a bot
if message.author.bot:
return
#Start the server
if message.content == '/start':
await message.channel.send('Server starting up...')
await message.channel.send('* Do not execute other commands until "start up" is displayed. *')
os.system('gcloud --account=<Service account name>@<Project name>.iam.gserviceaccount.com compute instances start <Instance name> --project <Project name> --zone <Zone name>')
await message.channel.send('up .minecraft_server starting...')
time.sleep(60)
await message.channel.send('start up')
#Server outage
if message.content == '/stop':
await message.channel.send('Server is stopping')
await message.channel.send('* Do not execute other commands until "down" is displayed. *')
os.system('gcloud --account=<Service account name>@<Project name>.iam.gserviceaccount.com compute instances stop <Instance name> --project <Project name> --zone <Zone name>')
await message.channel.send('down')
#View help
if message.content == '/help':
await message.channel.send('/start :Start the server')
await message.channel.send('/stop :Stop the server')
client.run(TOKEN)
Execution of the created bot program
#Make sure to keep the execution log and execute
$ nohup python3 mineserver-op.py > ./out.log &
At this point, you can operate the instance from discord. Let's check if you can actually operate the instance by typing a command from discord.
This time, we have implemented only the minimum functions for starting and stopping instances, It is possible to implement various functions by giving the desired authority to the service account.
As an aside, I played Minecraft a little in this operation check. Since I was playing, there have been a lot of new elements, and I wanted to do Minecraft again (laughs).
Recommended Posts