Replies: 1 comment
-
Hello, generally for this sort of operation you should probably switch to a webhook, discord.py also supports them out of the box. There is a small example in the docs on how to send a single message with the asynchronous webhook adapter, but there is also a sync webhook using a requests session instead if that better fits your needs. Otherwise if the channel is dynamic over multiple servers and creating webhooks in each is deemed too complicated you could switch your setup to a client that does not actually connect to the gateway, saving some time and resources. I've adapted your example above with as few changes as possible as I don't think there is one in the repository: import asyncio
import discord
class SingleMessageSender(discord.Client):
def __init__(self, message: str, channel_id: int) -> None:
# Intents passed don't really matter as we don't connect to the gateway
super().__init__(intents=discord.Intents.default())
self.message: str = message
self.channel_id: int = channel_id
async def send_message(self, token: str) -> None:
await self.login(token) # Ensure the internal HTTP client is initialized with a token
channel = await self.fetch_channel(self.channel_id)
await channel.send(self.message)
await self.close() # Close the HTTP client again
async def main() -> None:
client = SingleMessageSender('Hello, World', 847214599858421860)
await client.send_message('token')
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a more elegant way of doing the below:
Beta Was this translation helpful? Give feedback.
All reactions