import discord
import asyncio
client = discord.Client()
@client.event
async def on_ready():
print("I'm ready.")
async def send(message):
await client.send_message(client.get_channel("412678093006831617"), message)
client.run("token")
loop = asyncio.get_event_loop()
loop.run_until_complete(send("hello"))
Hi, i want to make a GUI. When someone put in his name and press "OK" my discord bot should send a message. Basically i thought i call the async by it's name, didn't work. Then i made a event loop. worked with a print(), but the bot doesn't send a message, so i thought it is not ready, when i put wait_until_ready() there it executed nothing, so i thought i have to put the client.run("token") before the event loop, didn't work either.
can you guys help me? :)
For responsive behaviour, you have two options: you can write a
on_message
event handler, or use thediscord.ext.commands
module. I recommend usingcommands
, as it's more powerful and doesn't keep everything in a single coroutine.This would be called with
!send Some message
. The*, message
syntax just tells the bot not to try and parse the message contents further.The reason your code is not working is because
client.run
is blocking, meaning that nothing after it will execute. This means yourloop
will never be reached.To get around this, use
client.loop.create_task
.The github of
discord.py
has an example of a background task, found here. You should be able to use this as reference. Currently the task posts a message to the given channel every minute, but you can easily modify it to wait for a specific action.