Reputation: 347
Computer: MacOS w/ Python 3.7 and Discord package installed
Server: CentOS 6 with Python 3.6 and Discord package installed
I'm thinking maybe the code is bad or can be optimized to clear the issue.
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)
What it's doing locally: Posting to the #chat channel on each discord server one-time, then sleeping the function for 2 hours. The bot is connected to four discord servers. (this is what I want it to do).
What it's doing on the CentOS Linux server: posting to the #chat channel of only one of the servers 5 times and then sleeping the function for two hours.
Question: is there a better way to optimize the code to loop through servers and post in the in the chat channels once per morning?
Upvotes: 1
Views: 109
Reputation: 368
Personally I would use a background task for this. Also, instead of using guild.channels
use guild.text_channels
. I've made a few changes to the code, hopefully this helps.
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())
Upvotes: 2