Answers for "discord bot example in python"

14

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")
Posted by: Guest on January-02-2021
10

how to make a discord bot python

# pip install discord

import discord

class MyClient(discord.Client):
	async def on_connect(self):
        print('[LOGS] Connecting to discord!')

    async def on_ready(self):
        print('[LOGS] Bot is ready!')
        print("""[LOGS] Logged in: {}n[LOGS] ID: {}n[LOGS] Number of users: {}""".format(self.bot.user.name, self.bot.user.id, len(set(self.bot.get_all_members()))))
        await self.bot.change_presence(activity=discord.Game(name="Weeke is a god!"))
	
    async def on_resumed(self):
        print("n[LOGS] Bot has resumed session!")

    async def on_message(self, message):
        # don't respond to ourselves
        if message.author == self.user:
            return

        if message.content == 'ping':
            await ctx.send(f'Client Latency: {round(self.bot.latency * 1000)}')

client = MyClient()
client.run('token')
Posted by: Guest on April-06-2020
1

Discord.py bot example

#Anything commented out is optional

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='prefix here')

@bot.event
async def on_ready():
#   await bot.change_presence(activity=discord.Game(name="Rich Presence Here"))
    print('Logged in as: ' + bot.user.name)
    print('Ready!n')
    
@bot.command()
async def commandname(ctx, *, somevariable)
#If you don't need a variable, then you only need (ctx)
#	"""Command description"""
	Code goes here
	await ctx.send('Message')

bot.run('yourtoken')
Posted by: Guest on January-09-2021
9

how to make a discord bot in python

#________________________________________________________________________________________#
#                                                                                        #
#                               How to make a discord bot                                #
#________________________________________________________________________________________#


# BE AWARE! YOU NEED TO CUSTOMIZE/CONFIGURE THIS CODE OTHERWISE IT WON'T WORK. 
# THIS CODE IS JUST THE BASICS



# IMPORT DISCORD.PY. ALLOWS ACCESS TO DISCORD'S API.
import discord

# IMPORTS EXTENSIONS FOR COMMANDS
from discord.ext import commands


# IMPORT THE OS MODULE.
import os

# IMPORT LOAD_DOTENV FUNCTION FROM DOTENV MODULE.
from dotenv import load_dotenv

# IMPORT LOGGING

import logging


# LOADS THE .ENV FILE THAT RESIDES ON THE SAME LEVEL AS THE SCRIPT.
load_dotenv()

# GRAB THE API TOKEN FROM THE .ENV FILE.
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")

# GETS THE CLIENT OBJECT FROM DISCORD.PY. CLIENT IS SYNONYMOUS WITH BOT.
bot = discord.Client()

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)


bot = commands.Bot(command_prefix='Yourprefix')

# UNDER THIS LINE OF CODE ARE THE COMMANDS FOR THE BOT. YOU CAN ADD/CHANGE THOSE SAFELY WITHOUT DESTORYING THE CODE

@bot.command()
async def test(ctx, *, arg):
	await ctx.send(arg, file=discord.File('yourfile.png'))


@bot.command()
async def laugh(ctx):
	await ctx.send("typeherealink")




@bot.command()
async def die(ctx):
	exit()



# EXECUTES THE BOT WITH THE SPECIFIED TOKEN. DON'T REMOVE THIS LINE OF CODE JUST CHANGE THE "DISCORD_TOKEN" PART TO YOUR DISCORD BOT TOKEN
bot.run(DISCORD_TOKEN)

#________________________________________________________________________________________#

# If this code helped you please leave a like on it. If you want to see more of this follow me
# Or just take a look at another answer of my answers
#
# THIS CODE HAS BEEN MADE BY : Vast Vicuña
Posted by: Guest on September-04-2021
0

discord python application bot

q_list = [
    'What is your favorite color?',
    'Is the Sky Blue?',
    'Am I the best bot ever?'
]

a_list = []


@client.command(aliases=['staff-application'])
async def staff_application(ctx):
    a_list = []
    submit_channel = client.get_channel(<your channel id>)
    channel = await ctx.author.create_dm()

    def check(m):
        return m.content is not None and m.channel == channel

    for question in q_list:
        sleep(.5)
        await channel.send(question)
        msg = await client.wait_for('message', check=check)
        a_list.append(msg.content)

    submit_wait = True
    while submit_wait:
        await channel.send('End of questions - "submit" to finish')
        msg = await client.wait_for('message', check=check)
        if "submit" in msg.content.lower():
            submit_wait = False
            answers = "n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1))
            submit_msg = f'Application from {msg.author} nThe answers are:n{answers}'
            await submit_channel.send(submit_msg)
Posted by: Guest on May-19-2021

Python Answers by Framework

Browse Popular Code Answers by Language