13
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("123456789"), 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? :)

1
  • just curious, what gui lib are you using ? Commented Jun 23, 2019 at 0:03

4 Answers 4

24

The reason your code is not working is because client.run is blocking, meaning that nothing after it will execute. This means your loop will never be reached.

To get around this, you can use the tasks extension, which was introduced in version 1.1.0 of discord.py. The GitHub has an example of a background task. The below code will post the current iteration of the loop to a channel every minute, but you can easily modify it to wait for a specific action.

import discord
from discord.ext import tasks

client = discord.Client(intents=discord.Intents.all())

@tasks.loop(seconds=60)
async def my_background_task():
    await client.wait_until_ready()
    channel = client.get_channel(123456789)  # replace with channel_id
    await channel.send(my_background_task.current_loop)

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    my_background_task.start()

client.run('token')

Older versions of discord.py

For older versions of discord.py, use client.loop.create_task. An example of a background task can also be found here.

discord.py < 1.1.0 versions

import discord
import asyncio

client = discord.Client()

async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = client.get_channel(id=123456789) # replace with channel_id
    while not client.is_closed():
        counter += 1
        await channel.send(counter)
        await asyncio.sleep(60) # task runs every 60 seconds

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.loop.create_task(my_background_task())
client.run('token')

discord.py < 1.0 versions

import discord
import asyncio

client = discord.Client()

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

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.loop.create_task(my_background_task())
client.run('token')
Sign up to request clarification or add additional context in comments.

8 Comments

Does this still work? I tried it and it didn't send anything
@F.M This is outdated as it uses an older version of discord.py, referred to as the async branch. Things like client.send_message don't exist in the newer versions. You need to use await channel.send() now. See example here.
*** AttributeError: 'Client' object has no attribute 'send_message'
@alper send_message will only work on older versions of discord.py, referred to as the async branch. On the new version, you can use await channel.send(counter)
It should also be channel = client.get_channel(id='channel_id_here')
|
1

For responsive behaviour, you have two options: you can write a on_message event handler, or use the discord.ext.commands module. I recommend using commands, as it's more powerful and doesn't keep everything in a single coroutine.

from discord.ext.commands import Bot

bot = Bot(command_prefix='!')

@bot.event
async def on_ready():
    print("I'm ready.")
    global target_channel
    target_channel = bot.get_channel("412678093006831617")

@bot.command()
async def send(*, message):                         
    global target_channel
    await bot.send_message(channel, message)

This would be called with !send Some message. The *, message syntax just tells the bot not to try and parse the message contents further.

4 Comments

Would you be able to call the command from the GUI? Curious as I have not experimented with something like that
Do you want to send the message from wherever you're running the bot from, or from discord? I'm not sure what GUI you're talking about.
LordSquiddy is asking how to make a bot respond after you press OK on a GUI
@Benjin is right, i would like to send a message when i click OK on a GUI, not from discord.
1

I know this question is old but found a better hack to perform this action. in your bot.py have a global variable for your messages and channel Id if needed

channelId = os.getenv('channelId')
token = os.getenv('discordBotToken')
intents = discord.Intents.default()

# inililzing discord client with default intents
client = discord.Client(intents=intents)

# Global message variable to be set externally and bot to be triggered
message = ""

# event triggered when the bot is ready
@client.event
async def on_ready():
    channel = client.get_channel(int(channelId)) # find the channel with the channel ID
    await channel.send(message) # send the message from the global variable
    await client.close() # close the client to stop the client blocking code

# method to be executed from an external file
def BotRun():
    client.run(token)

Then in your main.py import this file and

import bot as discordBot

discordBot.message = "hello!"
discordBot.BotRun()

This way u can call this bot without having it running in the background.

The only downside of this as u can expect is that we are closing the client and logging in every time we send a message.

Comments

0
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')

@client.event
async def on_ready():
    print(f"Log : {client.user}")
    ch = await client.fetch_channel("enter id text channel")
    await ch.send(content="your content")


client.run("token")

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.