Reputation: 33
so I'm currently trying to make a command that watches over a channel and if there are no messages sent in the last x minutes it will send a message
my code is:
@client.command()
async def watch(ctx, time : int):
channel = ctx.channel
async for message in channel.history(limit = 1):
past_message = message.created_at
await asyncio.sleep(time)
async for message in channel.history(limit = 1):
new_message = message.created_at
if past_message == new_message:
if not new_message:
channel = client.get_channel(816741369682985012)
await channel.send("<@400349192125415436> bot is down")
i got the code from this post but this command will send me the message only if the there are no message sent after I use the command, if there is any messages the command will stop or after the warn message is sent the command stops watching, I want to make it so its always watching the the channel
Upvotes: 1
Views: 312
Reputation: 2289
If you want to check the channel, after you execute the command, you could simply run it in loop
@client.command()
async def watch(ctx, time : int):
channel = ctx.channel
while True:
async for message in channel.history(limit = 1):
past_message = message.created_at
await asyncio.sleep(time)
async for message in channel.history(limit = 1):
new_message = message.created_at
if past_message == new_message:
channel = client.get_channel(816741369682985012)
await channel.send("<@400349192125415436> bot is down")
With this however, you will have to use the command to start watching.
If you want to start watching without using a command, you will need to register the function in your client main-loop.
async def watch():
channel = client.get_channel(816741369682985012)
while True:
async for message in channel.history(limit = 1):
past_message = message.created_at
await asyncio.sleep(time)
async for message in channel.history(limit = 1):
new_message = message.created_at
if past_message == new_message:
await channel.send("<@400349192125415436> bot is down")
And then register the function via
client.loop.create_task(watch())
Upvotes: 1