close

How to send a message with discord.py without a command

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to send a message with discord.py without a command in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to send a message with discord.py without a command?

  1. How to send a message with discord.py without a command?

    You can create a background task that does this and posts a message to the required channel.

  2. send a message with discord.py without a command

    You can create a background task that does this and posts a message to the required channel.

Method 1

You can create a background task that does this and posts a message to the required channel.

You also need to use asyncio.sleep() instead of time.sleep() as the latter is blocking and may freeze and crash your bot.

I’ve also included a check so that the channel isn’t spammed every second that it is 7 am.

from discord.ext import commands
import datetime
import asyncio

time = datetime.datetime.now

bot = commands.Bot(command_prefix='!')

async def timer():
    await bot.wait_until_ready()
    channel = bot.get_channel(123456789) # replace with channel ID that you want to send to
    msg_sent = False

    while True:
        if time().hour == 7 and time().minute == 0:
            if not msg_sent:
                await channel.send('Its 7 am')
                msg_sent = True
        else:
            msg_sent = False

    await asyncio.sleep(1)

bot.loop.create_task(timer())
bot.run('TOKEN')

Method 2

From the Discord.py docs when you have a client setup, you can directly send a message to a channel using the format:

channel = client.get_channel(12324234183172)
await channel.send('hello')

Once you have your channel (after you have setup your client), you can place that snippet of code edited as needed to select the appropriate channel along with the needed message. Keep in mind "You can only use await inside async def functions and nowhere else." so you need to setup an async function to do so and your simple While True: loop may not work

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read