Last active
December 4, 2022 06:08
-
-
Save toodaniels/5ea530268805c03a7174af26bded6d43 to your computer and use it in GitHub Desktop.
Send a simple message to a discord channel using discord.py $python discord_message.py --message "Hola Mundo"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import discord | |
from dotenv import load_dotenv | |
load_dotenv() # Loads envs from .env | |
def parse_args() -> argparse.Namespace: | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--message", help="Message content") | |
return parser.parse_args() | |
def init_client() -> discord.Client: | |
intents = discord.Intents.default() | |
intents.message_content = True | |
return discord.Client(intents=intents) | |
def add_send_message(client: discord.Client, message: str) -> function: | |
@client.event | |
async def on_ready(): # Called when internal cache is loaded | |
# Gets channel from internal cache | |
channel = client.get_channel(int(os.getenv('CHANNEL_ID'))) | |
await channel.send(message) # Sends message to channel | |
await client.close() # Close connection | |
return on_ready | |
def main() -> None: | |
args = parse_args() | |
print(type(args)) | |
client = init_client() | |
add_send_message(client=client, message=args.message) | |
client.run(os.getenv('DISCORD_TOKEN')) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment