skip to Main Content

I’m wondering on how to send a message in a specific channel using PYTHON and DISCORD.PY library.
I’m just a little bit confused.
I wrote a piece of code running correctly under python-binance library and sending messages via telegram library without any issues. The messages are sent when some conditions are met in the code without any telegram events, so the code is not locked in any point of the program.
I need to achieve the same target but using discord instead.
I’m not able to do so because I cannot find any example of this type.
Could someone lights up an idea or a piece of code for me?
Thanks to all.

2

Answers


  1. # You can get the channel ID by enabling developer mode
    # and right-clicking on the channel and clicking "Copy ID"
    channel = client.get_channel(12324234183172)
    # Normally you would await, but since you want to leave your code running
    channel.send('Here is your message')
    
    Login or Signup to reply.
  2. Here:

    import discord
    
    client = discord.Client(intents = discord.Intents.default())
    
    @client.event
    async def on_ready():
    
        channel = client.get_channel(<channel_id>)
    
        await channel.send("Hello World")
    
    client.run("<your_token>")
    

    Insert your values ​​in <channel_id> and <your_token>, after this, execute the code in terminal.

    I hope it helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search