【问题标题】:Discord Python Script - Issues with scheduling Daily post on connected serversDiscord Python 脚本 - 在连接的服务器上安排每日发布的问题
【发布时间】:2020-07-24 15:03:21
【问题描述】:

计算机:安装了 Python 3.7 和 Discord 包的 MacOS

服务器:安装了 Python 3.6 和 Discord 包的 CentOS 6

我在想可能是代码不好,或者可以优化以解决问题。

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))
        print(self.user.id)
        print('------')
        # loops through connected servers and assigns increments server count variable
        server_count = 0
        quote_count = 0
        for guild in client.guilds:
            server_count += 1
        print("The server count is {}".format(server_count))
        for quote in quotes:
            quote_count += 1
        print("There number of quotes available is: " + str(quote_count))
        print('------')
        while not client.is_closed():
            now = datetime.strftime(datetime.now(), '%H:%M')
            iter_count = 0
            print("current time is: " + now)
            if now == wakeup_time:  # enter for-loop at specific time
                for guild in client.guilds:
                    for channel in guild.channels:
                        if str(channel) == 'chat':
                            await channel.send('Good Morning/Afternoon/Good Night. Here is today\'s random quote:')
                            await channel.send('```' + str(random.choice(quotes)).strip('[]') + '```')
                            print("{} Server Greeted.").format(guild.name)
                            iter_count += 1
                            if iter_count == server_count:
                                await asyncio.sleep(7200)  # puts loop on hold for 1 hour
            else:
                await asyncio.sleep(10)

它在本地做什么:一次性发布到每个不和谐服务器上的#chat 频道,然后将该功能休眠 2 小时。该机器人连接到四个不和谐服务器。 (这就是我想要它做的)。

它在 CentOS Linux 服务器上所做的事情: 仅在其中一台服务器的 #chat 频道上发帖 5 次,然后将该功能休眠两个小时。

问题:有没有更好的方法来优化代码以循环通过服务器并每天早上在聊天频道中发布一次?

【问题讨论】:

  • 澄清一下,这适用于您的本地克隆,但不适用于 CentOS 服务器?
  • 正确。我确信它可以更有效地编写,因为我是新手。但我很好奇为什么它不会在两种环境中做同样的事情。

标签: python datetime discord python-asyncio discord.py


【解决方案1】:

我个人会为此使用后台任务。此外,不要使用guild.channels,而是使用guild.text_channels。我对代码做了一些更改,希望对您有所帮助。

class MyClient(discord.Client):
    async def start_task(self):
        while not client.is_closed():
            now = datetime.strftime(datetime.now(), '%H:%M')
            iter_count = 0
            print("current time is: " + now)
            if now == wakeup_time:  # enter for-loop at specific time
                for guild in client.guilds:
                    for channel in guild.text_channels:
                        if channel.name == 'chat':
                            await channel.send('Good Morning/Afternoon/Good Night. Here is today\'s random quote:')
                            await channel.send('```' + str(random.choice(quotes)).strip('[]') + '```')
                            print("{} Server Greeted.").format(guild.name)
                            iter_count += 1
                            if iter_count == server_count:
                                await asyncio.sleep(7200)  # puts loop on hold for 1 hour
                            break
            else:
                await asyncio.sleep(10)

    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))
        print(self.user.id)
        print('------')
        # loops through connected servers and assigns increments server count variable
        quote_count = 0
        print("The server count is {}".format(len(client.guilds)))
        for quote in quotes:
            quote_count += 1
        print("There number of quotes available is: " + str(quote_count))
        client.loop.create_task(start_task())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 2018-02-20
    • 2014-03-15
    • 2017-11-21
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多