python discord bot code 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 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

Example 4: python discord bot moderate chat

import discord

class MyClient(discord.Client):

    async def on_ready(self):
        print('Logged on as', self.user)

    async def on_message(self, message):
        word_list = ['cheat', 'cheats', 'hack', 'hacks', 'internal', 'external', 'ddos', 'denial of service']

        # don't respond to ourselves
        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            for word in word_list:
                if word in messageContent:
                    await message.delete()
                    await message.channel.send('Do not say that!')
            
        messageattachments = message.attachments
        if len(messageattachments) > 0:
            for attachment in messageattachments:
                if attachment.filename.endswith(".dll"):
                    await message.delete()
                    await message.channel.send("No DLL's allowed!")
                elif attachment.filename.endswith('.exe'):
                    await message.delete()
                    await message.channel.send("No EXE's allowed!")
                else:
                    break

client = MyClient()
client.run('token here')

Example 5: Discord bot source code Python

# bot.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

Example 6: 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__")