Discord.py: How to send a message without using async and await
Question:
How can I create a function (without async) that sends a message to a specific channel every time it (the function) gets executed somewhere in the code?
def sendMsg():
channel = client.getChannel(Channel id)
message.channel.send("example message")
#excecuting the function
sendMsg()
Doesn’t do anything
async def on_message():
await message.channel.send("example message")
Only this one works
So my question is if I can modify the code on the top make it work?
Answers:
Async is really needed for synchronize the message
If your wanna make it as selfbot response message, do
# Import's here
from discord.ext import commands
bot = commands.Bot(command_prefix='!', help_command=None, self_bot=True)
@bot.event
async def on_message(message):
if message.content.startswith("Hello"):
await message.channel.send(f"Hi @{message.author}")
bot.run('token ofc', bot=False)
Anyways this for selfbot if you wanna do it with bots, delete self_bot=True
and bot=False
If you need to send the message outside of an async context, you need to add the task into the event loop. Note that when you "call" a coroutine, you get the actual coroutine object. "Calling" it doesn’t actually run the coroutine, you need to put it in the event loop for it to run.
asyncio.get_event_loop().create_task(<coro>)
# use it like this
asyncio.get_event_loop().create_task(ctx.send('test'))
asyncio.get_event_loop().create_task(message.channel.send("example message"))
Eric’s answer is correct. Moreover, in order to get the loop event from discord.py, you can use client.loop
to get discord.py’s asyncio eventloop
that said, use asyncio.run_coroutine_threadsafe(def,loop)
to safely submit task to event_loop
client = discord.Client()
async def send_message_to_specific_channel(message='abc',id=123):
channel = client.get_channel(id)
await channel.send(message)
asyncio.run_coroutine_threadsafe(send_message_to_specific_channel('abc',123),client.loop)
How can I create a function (without async) that sends a message to a specific channel every time it (the function) gets executed somewhere in the code?
def sendMsg():
channel = client.getChannel(Channel id)
message.channel.send("example message")
#excecuting the function
sendMsg()
Doesn’t do anything
async def on_message():
await message.channel.send("example message")
Only this one works
So my question is if I can modify the code on the top make it work?
Async is really needed for synchronize the message
If your wanna make it as selfbot response message, do
# Import's here
from discord.ext import commands
bot = commands.Bot(command_prefix='!', help_command=None, self_bot=True)
@bot.event
async def on_message(message):
if message.content.startswith("Hello"):
await message.channel.send(f"Hi @{message.author}")
bot.run('token ofc', bot=False)
Anyways this for selfbot if you wanna do it with bots, delete self_bot=True
and bot=False
If you need to send the message outside of an async context, you need to add the task into the event loop. Note that when you "call" a coroutine, you get the actual coroutine object. "Calling" it doesn’t actually run the coroutine, you need to put it in the event loop for it to run.
asyncio.get_event_loop().create_task(<coro>)
# use it like this
asyncio.get_event_loop().create_task(ctx.send('test'))
asyncio.get_event_loop().create_task(message.channel.send("example message"))
Eric’s answer is correct. Moreover, in order to get the loop event from discord.py, you can use client.loop
to get discord.py’s asyncio eventloop
that said, use asyncio.run_coroutine_threadsafe(def,loop)
to safely submit task to event_loop
client = discord.Client()
async def send_message_to_specific_channel(message='abc',id=123):
channel = client.get_channel(id)
await channel.send(message)
asyncio.run_coroutine_threadsafe(send_message_to_specific_channel('abc',123),client.loop)