RuntimeError: There is no current event loop in thread 'Thread-7'. With discord.py

Viewed 168

I'm trying to bring up a Tkinter GUI and run a discord bot at the same time with threading and asyncio. My code is as follows:

def screen():
    global msg_entry, channel_entry


    main_window = tk.Tk()
    main_window.geometry('720x540+50+50')
    main_window.title('Control')
    main_window.configure(bg=col1) 
    main_window.resizable(0,0)

    msg_box = tk.Entry()




    msg_entry = tk.Entry(main_window, font = 'Helvetica 20',
                                 width = 10, bg = col4, fg = 'white')
    msg_entry.place(x=100, y=240)

    channel_entry = tk.Entry(main_window, font = 'Helvetica 20',
                                 width = 10, bg = col4, fg = 'white')
    channel_entry.place(x=350, y=240)

    submit = HoverButton(main_window, bd=0, highlightthickness=0,  font = 'Helvetica 25 bold', text = "Send",
                             width = 7, height = 1, bg = col4, fg = col2, justify = 'left'
                             ,command = lambda: asyncio.get_event_loop().run_until_complete(send()), activebackground = col5)
    submit.place(x= 220, y = 320)





    main_window.mainloop()

# 614001879831150605


async def send():
    ce = channel_entry.get()
    channel = client.get_channel(int(ce)) 
    await channel.send(msg_entry)

def func1():
    client.loop.create_task(update_meme())
    client.run(token)

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = screen).start()

I get the following error when I run my code:

RuntimeError: There is no current event loop in thread 'Thread-7'.

I'm new to threading and asyncio, so any help is appreciated.

1 Answers

Well, you see you haven't defined an event loop to create a task in it. So in your func1 do this:

def func1():
  loop = asyncio.new_event_loop()
  asyncio.set_event_loop(loop)
  client = commands.Bot(command_prefix='!')
  @client.command()
  async def send():
    ce = channel_entry.get()
    channel = client.get_channel(int(ce)) 
    await channel.send(msg_entry)

  loop.run_until_complete(client.run('token'))

then define your commands below it. also don't forget to join your threads (i made them as variables(1&2) just to demonstrate) thread1.join() thread2.join()

Related