What I need
I need a function that will run in a loop and send messages to discord channel
Problem
I have a function that sends messages to a discord channel
def send_discord(msg):
token = '0000000000000000000000000000000000'
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
channel = client.get_channel(000000000000000)
await channel.send(msg)
await client.close()
client.run(token)
I need to run this function in a loop to get messages info and send it At the moment I need to open the discord client each time before sending a messge. And then close it after the message is sent. This takes lots of time
If I don't close the client after sending the message - the program stops.
Question
How can I run my function in a loop without opening and closing it each time?
Why not use webhooks?
from discord_webhook import DiscordWebhook
def send_discord(msg)
webhook = DiscordWebhook(url='your webhook url', content=msg)
webhook.execute() # Send a message using the webhook
You can make a webhook just for a specific channel and use it to send messages. It doesn't need to login to discord and just needs the webhook url.
Read more about the module here: https://github.com/lovvskillz/python-discord-webhook