How do I code a Discord Bot in Python on an Iphone or Chromebook?

October 31, 2019 161 views
Python

**I am trying to code a bot for discord, but I do not have a laptop or an android. The chromebook I have is a school owned one, so things like downloads and developer mode are blocked. I can’t get the linux demo either. If it is not possible to code on this through web, then I do have an Iphone, which I have been told is impossible to code on.. I’m not worried about how long the code takes to develop, I just want to be able to code it without any laptop.

Additionally, all attempts to use a code given with this website: https://github.com/Rapptz/discord.py/tree/async

I enter this example:

import discord
import asyncio

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.startswith('!test'):
        counter = 0
        tmp = await client.send_message(message.channel, 'Calculating messages...')
        async for log in client.logs_from(message.channel, limit=100):
            if log.author == message.author:
                counter += 1

        await client.edit_message(tmp, 'You have {} messages.'.format(counter))
    elif message.content.startswith('!sleep'):
        await asyncio.sleep(5)
        await client.send_message(message.channel, 'Done sleeping')

client.run('token')

but it gives me a syntax error like this:

Unknown Error.
  File "<stdin>", line 7
    async def on_ready():
        ^
SyntaxError: invalid syntax
Unknown Error.

This is insanely frustrating… can someone assist?

(i am using https://thumbsdb.herokuapp.com/python/?state=%7B%22folderId%22:%220AIiLka239X73Uk9PVA%22,%22action%22:%22create%22,%22userId%22:%22117443184978319349054%22%7D and https://www.onlinegdb.com/ to enter and run code)

1 Answer

Hi there @eX2ii2t,

This error normally means that you’re using a version of Python that does not support async syntax. This is normally Python 2.x

Looking at the website you linked, it does indeed appear that it is using Python 2, which is why your program is not working.

Loading Python library.......Ready!
>>> import sys
>>> sys.version
'2.7.4 (default, May 10 2013, 01:23:53) \n[GCC 4.2.1 Compatible Clang 3.2 (tags/RELEASE_32/final)]'
>>>

You’ll need to find an online editor that supports Python 3.5+, as documented in the GitHub repository for this library: https://github.com/Rapptz/discord.py

(As a side note, the async branch of the library is no longer maintained and you should use the version of the library contained within master, which is available on PyPi.org)

Hope that helps!
- Matt.

Have another answer? Share your knowledge.