Python向不和谐发送消息

时间:2020-02-02 19:05:20

标签: python multithreading async-await discord python-asyncio

我有以下问题。 我有多线程应用程序,当满足特定条件时,我希望它向不和谐通道发送消息。 不一致模块:https://discordpy.readthedocs.io/en/latest/创建为遵循异步/等待表示法。我的应用程序是多线程的,因为它绑定的CPU比IO多。 我想将我的多线程应用程序与async / await discord模块结合使用。 我创建了以下代码。

import discord
import asyncio
from threading import Thread

class DiscordConnector:
    def __init__(self):
        self.token = "token"
        self.imageFolder = "folder"
        self.client = discord.Client()
        self.loop = asyncio.get_event_loop()
        self.loop.create_task(self.client.start(self.token))

        self.thread = Thread(target=self.loop.run_forever)
        self.thread.start()


        self.channels = {
            "chanel1":channel1_id,
            "channel2":channel2_id,
            "channel3":channel3_id,
        }

     def sendMessage(self, symbol, imageFilename):
        symbols = symbol.split("_")
        symbol = symbols[0].lower()
        interval = symbols[1]
        self.client.get_channel(self.channels[symbol]).send("message", file=discord.File(file))

我通过创建DiscordConnector对象并调用sendMessage方法在其他模块中调用它。 我遇到的问题是self.client.get_channel(self.channels [symbol])会返回None对象,看来无法从sendMessage到达self.client对象。 客户端的初始化看起来不错,因为服务器在不和谐应用程序中标记为在线。 您是否有任何想法如何将我的多线程应用程序与discord async / await语法结合使用,以便在有意将信息发送到discord频道时从DiscordConnector调用该方法。

0 个答案:

没有答案