discord bot python api code example

Example 1: bot discord python

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!", description="The description")

@bot.event
async def  on_ready():
    print("Ready !")

@bot.command()
async def ping(ctx):
    await ctx.send('**pong**')

bot.run("enter the token here between the quotes")

Example 2: how to import discord in python

py -3 -m pip install -U discord.py

Example 3: discord.py

# Discord.py is a API wrapper for python. 
Docs = "https://discordpy.readthedocs.io/en/latest/"
PyPI = "pip install -U discord.py"

# --- A simple bot ---

import discord
from discord.ext import commands 

client = commands.Bot(comand_prefix='bot prefix here') # You can choose your own prefix here

@client.event()
async def on_ready(): # When the bot starts
    print(f"Bot online and logged in as {client.user}")

# A simple command
@client.command(aliases=["ms", "aliases!"]) # You make make the command respond to other commands too
async def ping(ctx, a_variable): # a_variable is a parameter you use in the command
    await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")

client.run('your token here') # Running the bot

Example 4: on message discord py

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

Example 5: discord python bot

from discord.ext import commands

bot = commands.Bot(command_prefix="^^")
async def on_ready():
	print("Logged in as - {} | {}".format(bot.user, bot.user.id))
	print("Invite link | https://discord.com/oauth2/authorize?client_id={}&scope=bot&permissions=8".format(
			bot.user.id))

bot.run("__TOKEN__")