python 3.x - Why can I not send a message via Discord.py from a function? -


i have created script takes in message formatted !notice [mm/dd/yy hh:mm], message, target calls function using threading.timer call @ time given in message in utc.

where having trouble sending message function, can't seem message send function regardless of input of message.

see below:

import discord import asyncio datetime import * import threading  client = discord.client()  @client.event async def on_message(message):     if message.content[:7].lower() == "!notice".lower():         try:             notice = [datetime.strptime(message.content[message.content.find("[")+1:message.content.find("]")], "%m/%d/%y %h:%m"), message.content.split(", ")[1], message.content.split(", ")[2]]             await client.send_message(message.channel, 'created notice "'+notice[1]+'" sent '+notice[2]+' @ '+str(notice[0])+' utc.')             threading.timer((notice[0] - datetime.utcnow()).total_seconds(), lambda a=notice[1], b=notice[2]: func(a, b)).start()             print(str((notice[0] - datetime.utcnow()).total_seconds())+" seconds until message sent")         except (valueerror, indexerror):             await client.send_message(message.channel, 'incorrect notice format.\nmust "!notice [mm/dd/yy hh:mm], notice contents, target".\neg: "!notice [01/01/2017 12:00], notice, siren raid team".')  def func(message, target):     print("func called")     in client.servers:         c in i.channels:             client.send_message(c, target+message)  client.run(my_session_key) 

this returns "func called" know function being called, no exceptions raised , no message posted in chat.

i tried substituting func with:

async def func(message, target):     print("func called")     in client.servers:         c in i.channels:             await client.send_message(c, target+message) 

however throws exception:

runtimewarning: coroutine 'func' never awaited

frankly, i'm out of depth here. there reason why won't work?

i saw online asyncio not thread-safe. but, unless i'm misunderstanding, first example didn't use library in function. still causing problems?

discord.py's discord.client.send_message coroutine , must awaited, did in second code snippet. however, threading.timer not support coroutines. you're looking create_task, enables run coroutine on event loop. since of coroutine sleeping (mimicking threading.timer), on_message proceed run, given use asyncio.sleep , not time.sleep - latter blocks event loop. here's example, including passing arguments functions:

import asyncio  loop = asyncio.get_event_loop()  async def sleep_and_add(a, b):     await asyncio.sleep(3)     print(a, '+', b, 'is', + b)  async def on_message():     # prepare arguments function     loop.create_task(sleep_and_add(2, 3))     # continue doing other things 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -