[PYTHON] A disocrd bot that even beginners can regularly post Splatoon 2 results to stat.ink for free

Introduction

The basic idea is to be able to create a discord bot ** that automatically uploads Splatoon's battle record to ** stat.ink, whether you are a PC beginner or someone who only has a smartphone.

** For those who want to create or use a Discord Bot that posts the results to stat.ink for free for the time being, please refer to GitHub Repository for how to prepare and use it. I wrote it in detail in the README of, so please check there. ** **

In this article, I'll talk about some ideas about Discord Bot running on Heroku.

Functions of discord bot to create

--Automatically upload the results of multiple accounts to stat.ink using splatnet2stat.py. --After starting discord bot ** You can get iksm_session (preparation for automatic upload) only with the bot function ** --All work can be completed ** with just a smartphone, including preparation for starting the bot (although it is more convenient to use a PC)

Problems and solutions

Heroku has restrictions on creating and writing files.

--You can only create files in / tmp for creating temporary files. (* The original file is under / app.) --Furthermore, the contents of / tmp are also reset at regular intervals (up to 24 hours). --In other words, ** there is no place where you can continuously save the files generated after starting the bot **.

There are two problems associated with this.

Location of iksm_session

After starting discord bot ** You can get iksm_session (preparation for automatic upload) only with the bot function **

Due to the specifications of the above function, iksm_session (strictly speaking, the contents of config.txt) is obtained after running the bot on heroku. In order to save those data forever, I used Heroku's environment variables this time.

Heroku environment variables can be set from the Heroku admin screen, but can also be updated from Heroku's REST API. In other words, it is possible to output to environment variables from within the bot running on Heroku and save the data continuously **. -> For more information Platform API Reference # config-vars-update

app_name=os.getenv("HEROKU_APP_NAME", "app-splat") #HEROKU_APP_NAME
if app_name=="":
    print("Environment variable HEROKU_APP_NAME is not defined.")
    return
patch_url = f"https://api.heroku.com/apps/{app_name}/config-vars"
headers= {"Authorization": f"Bearer {os.getenv('HEROKU_APIKEY')}",
    "Content-Type":"application/json",
    "Accept":"application/vnd.heroku+json; version=3"}
res=requests.patch(patch_url, headers=headers, json=new_envs)

Dyno will be restarted once after updating the environment variables. Also, note that ** Heroku environment variables can only be used up to a total of 32KB **.

Position of config.txt when uploading battle record

splatnet2statink.py assumes that config.txt with iksm_session etc. is located in the same directory. As it is, rewrite config_path.

In addition, assuming an experiment in local, environment variables are used properly depending on the presence or absence of os.getenv ("DYNO") , which is set by default on Heroku.

splatnet2statink


## splatnet2statink/iksm.py
config_path="/tmp/config.txt" if os.getenv("DYNO", False) else f"{os.path.dirname(__file__)}/../tmp/config.txt"
## splatnet2statink/splatnet2statink.py
config_path=iksm.config_path

Other issues include:

For some reason json.loads () becomes a string

I wrote this in another article. -> Python json.loads () returns str in Heroku Since it may not be a string, we are currently trying to determine the type.

Heroku


loaded_tmp=json.loads(dumped_string)
loaded_dict=eval(loaded_tmp) if (loaded_tmp)==str else loaded_tmp

GitHub Repository

Recommended Posts

A disocrd bot that even beginners can regularly post Splatoon 2 results to stat.ink for free
8 services that even beginners can learn Python (from beginners to advanced users)
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Make a Discord Bot that you can search for and paste images
A memo for making a figure that can be posted to a journal with matplotlib
Automatic browser operation that even beginners can do