Scrython icon indicating copy to clipboard operation
Scrython copied to clipboard

Discord Implementation

Open buinesha opened this issue 7 years ago • 9 comments

When I try to run a command to use scrython in my discord bot, it creates an error when I start using the scrython library. My code is as follows:

import discord
import BotUtils
from discord.ext import commands
import scrython

bot = commands.Bot(description="FetchBot", command_prefix="!")

@bot.command()
async def getcard(name):
    getCard = str(name)

    card = scrython.cards.Named(fuzzy=getCard)

    if card.type_line() == 'Creature':
        PT = "({}/{})".format(card.power(), card.toughness())
    else:
        PT = ""

    if card.cmc() == 0:
        mana_cost = ""
    else:
        mana_cost = card.mana_cost()

    string = """
    {cardname} {mana_cost}
    {type_line} {set_code} {rarity}
    {oracle_text}{power_toughness}
    """.format(
        cardname=card.name(),
        mana_cost=mana_cost,
        type_line=card.type_line(),
        set_code=card.set_code().upper(),
        rarity=card.rarity(),
        oracle_text=card.oracle_text(),
        power_toughness=PT
    )

    print(string)

bot.run(BotUtils.AUTH_TOKEN)

When I run the !getcard command using any card name (I've tested with Opt, Moat, Hijack, Entomb, Naturalize, Progenitus, Mindslaver, and Glimmervoid) I get this error:


Ignoring exception in command getcard
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "C:/Users/User/PycharmProjects/bot1.1/botBase.py", line 142 in getcard
    card = scrython.cards.Named(fuzzy=getCard)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scrython\cards\named.py", line 18, in __init__
    super(Named, self).__init__(self.url)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scrython\cards\cards_object.py", line 25, in __init__
    loop.run_until_complete(main(loop))
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 454, in run_until_complete
    self.run_forever()
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 408, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
 
The above exception was the direct cause of the following exception:
 
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: This event loop is already running






buinesha avatar Feb 22 '18 18:02 buinesha

I believe that this is due to discord.py creating it's own event loop, and I'm pretty sure you can't have more than 1 event loop running. I'll try this out on my machine and see how I can fix this.

NandaScott avatar Feb 22 '18 18:02 NandaScott

Update: I've tried numerous options for fixing this, including creating a new parameter that you can pass an event loop into. Nothing so far has worked, and I'm welcome to ideas.

NandaScott avatar Feb 23 '18 17:02 NandaScott

I wish I could be of more help, but I'm not terribly experienced at all with this, so your guess is as good as mine...

buinesha avatar Feb 24 '18 19:02 buinesha

Alright I got this to work by using the threading library. I don't know if this is the best way, but it's the only way I could get it to work. I'll create an example and release this on version 1.3.0.

NandaScott avatar Mar 01 '18 02:03 NandaScott

This is now fixed as of version 1.3.0. Make sure to update to the latest version anyways.

NandaScott avatar Mar 01 '18 04:03 NandaScott

After trying to implement scrython in my own discord bot, I'm running into the issue I should have fixed. Reopening and re investigating.

NandaScott avatar Mar 28 '18 00:03 NandaScott

I believe if you create a global area from which the modules can grab the event loop, then you just need functionality for setting that loop and only creating a new loop (or using the default loop via asyncio.get_event_loop) if it wasn't explicitly set by the user. This way it can be initialized with a discord bot's internal event loop. This would only cause issues if blocking code is called in the event loop, that would need to be fixed for this to work.

The-Duchess avatar Sep 06 '18 22:09 The-Duchess

Sorry to necro this in 2020

A workaround for this is to use nest_asyncio, e.g. these imports should work:

import discord
import scrython
import nest_asyncio
nest_asyncio.apply()

jsumali avatar Jan 24 '20 19:01 jsumali

Hi all! This also seems to take place with the latest version on Jupyter Notebooks.

I'll try to look into it in that scenario. I'd really love to get this working for my data explorations 😁

diogojapinto avatar Oct 16 '22 12:10 diogojapinto