Discord.py: Sending a message without using a command or loop.

Created on 25 Jun 2018  路  6Comments  路  Source: Rapptz/discord.py

I'm having my bot retrieve information from a form. Once the information is submitted it calls a function. How can I have the discord bot to send a message when that function is called without it waiting for a command or a loop. (Loop causes it to break if someone enters another input into the form)

I've looked around for a solution but haven't found anything yet.

Sudo Code example:
If form submitted:
discord_run()

def discord_run():
*Need something here
await send_message(channel_id, "form completed")

Some help would be appreciated.

question

All 6 comments

Webhook.

It seems like you want to schedule the message send as a task, if the issue is that the function being called isn't a coroutine.

For future questions like this, you should join either the official discord.py server or the Discord API server for help, as the README recommends.

All I'm wanting is that when I call my function it sends a message to a channel ID without a command needed. The function calls but I just have no idea how to send a message without using something like:

async def my_background_task():
await client.wait_until_ready()
counter = 0
channel = discord.Object(id='channel_id_here')
while not client.is_closed:
counter += 1
await client.send_message(channel, counter)
await asyncio.sleep(60) # task runs every 60 seconds

Again, if the issue is that the function being called isn't a coroutine, you can schedule sending the message as a task. If it is a coroutine, then you should be able to just send the message normally.

GitHub issues are not the proper place to ask for help like this. You should join one of the Discord servers if you need additional help.

The way to do this in the latest version would appear to be this:
for guild in client.guilds:
if str(guild) == "your_server_name":
for channel in guild.channels:
if str(channel) == "your_channel_name":
await channel.send('your_message')

Please don't necrobump old closed issues.
This is also a lousy way to retrieve a channel and not relevant to this issue.
For further help, you should refer to the documentation or join either the official discord.py server or the Discord API server, as the README also links.
For code block usage, see https://help.github.com/articles/creating-and-highlighting-code-blocks/.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Spyder-exe picture Spyder-exe  路  3Comments

AraHaan picture AraHaan  路  3Comments

tairabiteru picture tairabiteru  路  3Comments

synbitz picture synbitz  路  3Comments

jzburda picture jzburda  路  3Comments