【发布时间】: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