[PYTHON] Try running Discord Bot on an iPhone you no longer use

Motivation

I want to run it 24 hours a day, but the rental server is troublesome and I wish I could run it on my iPhone.

What you have prepared

--iPhone (6s of ios13.4.1) --Pythonista 3 (version is 3.3, you can buy it for 1220 yen) https://apps.apple.com/jp/app/pythonista-3/id1085978097 --File (the one installed by default) https://apps.apple.com/jp/app/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB/id1232058109

Insert StaSh (Pythonista Shell)

Create a Script file with any name under This iPhone and write the code below.

import requests as r; exec(r.get('http://bit.ly/get-stash').text)


 After installation, restart Pythonista and run launch_stash.py (in the same location as the file you created)

## Insert discord.py
 When you execute launch_stash.py, a screen like a terminal will appear, so type the following code there

#### **`pip install discord.py[voice]`**
```py[voice]

 If you don't use voice

#### **`pip install discord.py`**
```py

 With a suitable Script file to check if the installation is successful
```import discord```
 And try to execute

 Then ModuleNotFoundError says that ctypes.macholib is missing

## Insert Cpython
 When you open the following URL in safari, the download will start, so wait
https://github.com/python/cpython/archive/v3.6.1.zip
 Open the file (app) after downloading
 If you click the file called cpython, it will be decompressed, so wait for a while, and if you press and hold the unzipped folder and press move, you will select the location to move, so press> Pythonista 3 in this iPhone to copy (it may take time) )

 Open Pythonista, open the tab on the left, press open ... of EXTERNAL FILES, and select Folder .... Press Pythonista 3 in this iPhone that you copied earlier, and cpython will appear, so select it. And press Done

 Then, since the cpython folder is created, open the lib of the cpython folder, there is a folder called ctypes in it, so press Edit and move the file to site-package-3

 If all goes well, import discord will not give an error

## Try to move
```python

import sys
import time
import discord

TOKEN = 'Your BOT access token'
client = discord.Client()

#Processing at the time of connection
@client.event
async def on_connect():
    print('login')
    time.sleep(5)
    await client.close()
    print('logout')
    sys.exit()

client.run(TOKEN)

It moved, so Yoshi!

The first time it works fine, but after the second time I get an error and it doesn't work I can't solve it because I don't understand asynchronous processing at all The error statement was a simple one called Event loop is closed, and I tried various things to check it, but I couldn't solve it after all. I will study asynchronous processing and start again ...

Code without error

It is the one that drops pythonista when it finishes running.


import os
import time
import discord

TOKEN = 'Your BOT access token'
client = discord.Client()

#Processing at the time of connection
@client.event
async def on_connect():
    print('login')
    time.sleep(5)
    await client.close()
    print('logout')
    os.abort()

client.run(TOKEN)

Reference site below

Install StaSh on Pythonista 3. https://qiita.com/maboy/items/cef5dee13d5b2e9ac843 Missing ctypes.macholib breaks ctypes.util https://github.com/omz/Pythonista-Issues/issues/311

Recommended Posts

Try running Discord Bot on an iPhone you no longer use
Try running a Django application on an nginx unit
Build an environment on windows10 where you can try MXNet
Try running tensorflow on Docker + anaconda
Try running Jupyter Notebook on Mac
Use features that are no longer visible on the UI in Slack