discord bot python python code example

Example 1: how to import discord in python

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

Example 2: discord python 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 {self.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 3: discord bot python

# To make a simple discord bot that when the message content is '.hello'
# first you need to create a bot on discord.com/developers/
#there are many tutorials on how to create a bot
# then use py -3 -m pip install -U discord.py[voice] in terminal
# or if you're on mac/linux python3 -m pip install -U discord.py[voice]
import discord.py

client = discord.Client()
@client.event
async def on_ready():
    print("Logged in as {0.user}".format(client)

@client.event
async def on_message(message):
      #this will prevent the bot from responding to itself
      if message.author == client.user:
      		return
      
      #now let's start with the message content check
      if message.content == '.hello':
          print("Hello there!!")
          
#now run the bot
client.run('here put your token')

#and now when you run the program the bot will be online and working