discord.py message code example

Example 1: send message discord.py

await message.channel.send("Your message")

Example 2: discordpy base code

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run('your token here')

Example 3: on message discord py

@bot.event
async def on_message(message):
    if message.content == "pong":
        await message.channel.send('ping')

Example 4: discord.py fetch channel

await client.fetch_channel(channelId)

Example 5: discord.py message user

if message.content == "dm":
        await message.channel.send("Dming user")
        dm = await message.author.create_dm()  # Creates a dm channel with the user
        await dm.send("What you want to send")  # Sends the user the message

Example 6: discord py fetch channel by id

channel = discord.utils.get(ctx.guild.channels, name=given_name)
channel_id = channel.id

Tags:

Misc Example