omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Discord.py

    Pythonista
    8
    15
    16658
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SpiesWithin
      SpiesWithin last edited by

      How do I use something called discord.py? https://github.com/Rapptz/discord.py I tried using stash but I couldn't get it to work.

      1 Reply Last reply Reply Quote 0
      • SpiesWithin
        SpiesWithin last edited by

        There is a variable called client, which isn't defined in the module because you have to define it in the code you write. This is my current road block.

        1 Reply Last reply Reply Quote 0
        • Webmaster4o
          Webmaster4o last edited by Webmaster4o

          How to use discord.py has nothing to do with Pythonista itself. Why don't you read the example on the page you linked:

          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')
          

          I'm not sure whether asyncio works in Pythonista, though, it may not. The last time I used Discord.py was almost a year ago, and it didn't use asyncio at that point. I believe there's still a legacy API available.

          SpiesWithin 1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by

            Asyncio works in Pythonista and it is supercool!!

            1 Reply Last reply Reply Quote 1
            • SpiesWithin
              SpiesWithin @Webmaster4o last edited by

              @Webmaster4o Try it out,
              It doesn't work for me, it says client not defined and I have no idea how to define it.

              Webmaster4o 1 Reply Last reply Reply Quote 0
              • Webmaster4o
                Webmaster4o @SpiesWithin last edited by

                @SpiesWithin This example is correct. client is defined with client = discord.Client()

                SpiesWithin 1 Reply Last reply Reply Quote 0
                • SpiesWithin
                  SpiesWithin @Webmaster4o last edited by

                  @Webmaster4o I'm telling you, just try it out I am getting error after error

                  1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB last edited by JonB

                    I have always found the best way to get help is to describe exactly what error you get, and exactly what steps led you to that error. People might know the easy answer, and can give you the answer quickly by seeing the traceback, or an explanation of what you tried. I ignored this thread 5 or 6 times because I did not feel like figuring out where and how to install discord, or solving the other roadblock you implied that you encountered.. I guess seven times was the charm....

                    I installed discord.py using stash:
                    $ pip install discord.py
                    There were a few errors, because stash runs in py2, and aiohttp required py3, and also setup failed to run on most of the dependencies. This happens, it is a limitation of the stash pip and ios environment, sometimes you need to do a little extra work. But, we'll try anyway, see if it worked:

                    >>> import discord
                    Traceback (most recent call last):
                      File "<string>", line 1, in <module>
                      File "/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages/discord/__init__.py", line 20, in <module>
                        from .client import Client, AppInfo, ChannelPermissions
                      File "/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages/discord/client.py", line 42, in <module>
                        from .voice_client import VoiceClient
                      File "/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages/discord/voice_client.py", line 64, in <module>
                        from .gateway import *
                      File "/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages/discord/gateway.py", line 30, in <module>
                        import aiohttp
                      File "/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages/aiohttp/__init__.py", line 4, in <module>
                        import multidict  # noqa
                    ImportError: No module named 'multidict'
                    

                    Ok, so sure enough, problem with importing aiohttp. Seems like we need a module named multidict. A good first guess is pip...Back to stash, and pip install multidict. import discord still fails, no package named chardet. Ok, do you sense a pattern? pip install chardet. Importing discord now says NameError: name 'errors' is not defined. That is wierd, since it is on a line that does not even mention errors. Sometimes when you get an import error on package, it gets stuck on a bad state. So we restart pythonista and try again. Now import discord works (in python3 -- discord won't work as written in py2), and client=discord.Client() works too.

                    1 Reply Last reply Reply Quote 2
                    • oakandsage
                      oakandsage last edited by oakandsage

                      So I've been looking into this, and I've hit a snag. I can't tell whether or not it's insurmountable.

                      The discord.py library depends on websockets.
                      Websockets won't install via python 2, so StaSh doesn't install it properly. You can , in theory, run setup.py manually. But when I look at the websockets setup.py file, I see this in the setuptools call:

                      ext_modules = [
                          setuptools.Extension(
                              'websockets.speedups',
                              sources=['websockets/speedups.c'],
                              optional=not os.path.exists(os.path.join(root_dir, '.cibuildwheel')),
                          )
                      ]
                      

                      Now, to me it looks like this is an optional extension in C, but I'm not sure whether websockets is ever going to install correctly. I don't have any experience with setuptools, so I don't know how SOL Pythonista's going to be when it hits that.

                      I also can't figure out how to run setup.py (I just get a whole bunch of permission errors), so I haven't been able to verify whether or not it's possible to use websockets. It's even possible that websockets is actually installed properly (despite the issue report that says it can't be installed with pip install if pip is using python 2.x) and the issue with the failed coroutine detection is something else entirely.

                      Regardless, it looks like it would be a challenge to do anything meaningful with the Discord API using discord.py in Pythonista.

                      1 Reply Last reply Reply Quote 0
                      • dgelessus
                        dgelessus last edited by

                        It's not possible to run setup.pys under Pythonista directly, because of how different the directory structure is compared to a Python installation on a regular Unix or Windows system. (Even on normal systems the recommended way to install packages is to pip install the folder containing the setup.py, rather than running setup.py install.) Stash's pip does some hackery to make most setup.pys work in Pythonista, but since Stash is basically Python 2-only, that isn't an option for Python 3-only packages.

                        If I'm reading the code correctly, the speedups are indeed optional. The optional=not os.path.exists(...) part makes it required only when running a CI build (which the library devs have presumably set up to build wheels for PyPI, so they want to force the speedups to be included there).

                        You could try installing the library "the manual way" - install all dependencies somehow (Stash pip, or also manually), then find the directory or Python file containing the library's source code and copy that into site-packages-3. Assuming the library is used as import discord, that would be either a directory discord or a file discord.py, most likely located in the same directory as the setup.py.

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by JonB

                          For what its worth, I have websockets installed in pythonista. I believe it only really needs asyncio, which is included in python 3 on pythonista.

                          I frankly forget why or how I installed it -- at a minimum, just pip download, then untar.p, or it might be easier to use git clone.

                          1 Reply Last reply Reply Quote 0
                          • Ivoah
                            Ivoah last edited by

                            Has anyone made any progress on this? I've been trying for the last hour to get discord.py working on the latest Pythonista beta (3.1.1), but haven't had much luck. Currently I'm stuck on getting aiohttp to work. A little bit of googling shows that at one point pythonista beta had aiohttp built in (https://forum.omz-software.com/topic/4456/ui-and-aiohttp), but that doesn't seem to be the case anymore. I was able to install aiohttp using pip from stash, but it seems the imports are messed up, with python complaining about not being able to find parts of the library.

                            1 Reply Last reply Reply Quote 0
                            • zrzka
                              zrzka last edited by zrzka

                              • Install Pythonista 3.2 (latest version from the App Store > latest Pythonista beta 3.1.1)
                              • pip install aiohttp
                                • Extensions are gonna fail, ... but you can ignore them
                              • pip install idna
                              • Working aiohttp example in Pythonista

                              with python complaining about not being able to find parts of the library

                              No crystal ball, always paste what's the problem. If you read the error message you'll see that there's module import error - missing idna.

                              1 Reply Last reply Reply Quote 0
                              • Ivoah
                                Ivoah last edited by Ivoah

                                I followed your instructions, but it still doesn't work. When trying to import aiohttp it fails in the __init__.py file. It looks like it tries to import other parts of the library with from .<something> import *, but it isn't working. The exact message it gives is name 'client' is not defined. Pythonista says the error is on line 38, but I think it's actually line 29.

                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB last edited by

                                  paste the entire traceback...

                                  Also, be sure to quit pythonista and try again. Sometimes if you get an error after starting to import, things get into a stuck state.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post
                                  Powered by NodeBB Forums | Contributors