Reputation: 11
I am writing a discord bot with discord.py. I wrote an initial version but I decided it all needed reorganizing so I moved code to different files.
The code is all the same as it was before but now when I boot up the bot, the bot detects no messages being sent in any server unless the message came from the bot itself.
The main code that handles the client
is:
import discord
import time
from command_list import *
from resource_functions import grab_setting_from_category
print("Main initialized")
client = discord.Client()
token = "BOT TOKEN"
prefix = "!"
@client.event
async def on_ready():
print("*** BOT IS READY ***")
async def server_count_loop():
while True:
servers = client.servers
await client.change_presence(
game=discord.Game(name=" in {0} Servers".format(str(len(servers)))))
time.sleep(10)
for server in client.servers:
for channel in server.channels:
if channel.name == "general":
await client.send_message(channel, content="Bot Online")
await server_count_loop()
@client.event
async def on_message(message):
print("Message detected from {0} as '{1}'".format(message.author, message.content))
if not message.author.bot:
global prefix
prefix = grab_setting_from_category(message.server.id, "server", "prefix")
if message.content.startswith(prefix):
for i in range(0,len(commands)):
key = list(commands.keys())[i]
table = list(commands.values())[i]
if message.content.startswith(prefix+key):
table["function"](message, commands)
client.run(token)
In the code there's the print(...)
at the start of the on_message
function which I am using as a basic way to let me know if the bot is detecting messages. The print statement prints whenever the bot sends a message in on_ready
but no messages from other users on Discord are triggering the function.
Upvotes: 1
Views: 780
Reputation: 33714
Don't use:
time.sleep(...)
Instead, use:
await asyncio.sleep(...)
time.sleep
is a blocking call, it blocks the asyncio loop to continue to run. That is why you need to have an asynchronous .sleep(...)
method.
Upvotes: 2