discord.py events code example

Example 1: 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 2: on message discord py

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

Example 3: get guild from a channel discord py

channel.guild