如果您想要运行代码...请务必执行 pip install discord.py

我有以下三个python文件,它们一起工作以连接到discord服务器

Launch.py

from Sender import *
from Reciever import *

class Launcher:

    def startThreads(sendOB, recOB, username):
        threads = []
        recThread = threading.Thread(target=recOB.main, args=())
        sendThread = threading.Thread(target=sendOB.main, args=(username))
        threads.append(sendThread)
        threads.append(recThread)
        for i in range(2):
            threads[i].start()
        for i in range(2):
            threads[i].join()
    def launcherMain(self):
        username = input("Enter your username: ")
        theSender = Send()
        theReciever = Recieve()
        Launcher.startThreads(theSender, theReciever, username)

launchOB = Launcher()
launchOB.launcherMain()

Reciever.py

import discord, threading, asyncio

client = discord.Client()

class Recieve:
    index = None

    @client.event
    async def on_message(message):
        #Run through decrypter here
        if message.author == client.user:
            return
        Recieve.index = message.content.find(": ")
        print(str(Recieve.index))
        #print(message.content[0:Recieve.index] + "~-#" + message.content[Recieve.index + 2:len(message.content))



    @client.event
    async def on_ready():
        print("Reciever Ready to go")
        print('---------')


    def main(self):
        client.run('TokenErasedForSecurity')

#rec = Recieve()
#rec.main()

Sender.py

import discord, threading, asyncio
client = discord.Client()


class Send:
    username = ""
    async def messageSender():
        channel = discord.Object(id='IdErasedForSecurity')
        while True:
            messageToSend = Send.username + ": "
            messageToSend += input(Send.username + '~->')
            await client.send_message(channel, messageToSend)


    @client.event
    async def on_ready():
        print("Sender Ready to go")
        print('---------')
        client.loop.create_task(Send.messageSender())

    def main(self, theUsername):
        Send.username = theUsername
        client.run('TokenErasedForSecurity')

Launch.py使用线程运行发送方和接收方,但是当我运行launch.py时,我得到了这个异常

输入您的用户名:线程中的异常Thread-1:Traceback(最近一次调用最后一次):文件“C:\ Program Files \ Python36 \ lib \ site-packages \ discord \ client.py”,第519行,在运行自身中.loop.run_until_complete(self.start(* args,** kwargs))文件“C:\ Program Files \ Python36 \ lib \ asyncio \ base_events.py”,第454行,在run_until_complete中self.run_forever()文件“C: \ Program Files \ Python36 \ lib \ asyncio \ base_events.py“,第408行,在run_forever中引发RuntimeError('此事件循环已在运行')RuntimeError:此事件循环已在运行在处理上述异常期间,发生了另一个异常:Traceback(最近一次调用最后一次):文件“C:\ Program Files \ Python36 \ lib \ threading.py”,第916行,在_bootstrap_inner self.run()文件“C:\ Program Files \ Python36 \ lib \ threading中 . py“,第864行,在运行self._target(* self._args,** self._kwargs)文件”C:\ Users \ user \ Desktop \ Discord \ HopeForBetter \ Reciever.py“,第26行,在主客户端 . 跑('MzAzNjUyODYwNjY1NTI4MzIx.C9bPOA.4ISE_jmY1BYlPq937zGpjISuvAI')F运行self.loop.close()文件“C:\ Program Files \ Python36 \ lib \ asyncio \ selector_events”中的“C:\ Program Files \ Python36 \ lib \ site-packages \ discord \ client.py”,第534行.py“,第107行,关闭引发RuntimeError(”无法关闭正在运行的事件循环“)RuntimeError:无法关闭正在运行的事件循环

我唯一能够想到的是它导致异常,因为两个线程都访问同一个文件,但这似乎不太合理,但我也是线程的新手 .