简体   繁体   中英

Sending a message in a discord channel without events using PYTHON and DISCORD.PY leave running the other part of the code

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.

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.

# 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM