Search code examples
pythondiscord

"Persistent Pursuit: Getting My discord.py Discord Bot Online in Python"


Struggling to make my discord.py bot work in Python, I've attempted it 50 times without success. I'm determined to get it online at least once. Need guidance and troubleshooting help to achieve this goal.

import discord

from discord.ext import commands

Create a bot instance with a prefix

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

Event handler for when the bot is ready

@bot.event

async def on_ready():

print(f'Logged in as {bot.user.name}')

Command to say hello

@bot.command()

async def hello(ctx):

await ctx.send('Hello!')

Replace 'YOUR_TOKEN_HERE' with your bot's token

bot.run('YOUR_TOKEN_HERE ')


Solution

  • I'll guide you through it.

        import discord
        from discord.ext import commands
        
        bot = commands.Bot(command_prefix="!",intents=discord.Intents.all())
        
        @bot.event
        async def on_ready():
            print("Bot is online")
    
        @bot.command()
        async def test(ctx:commands.Context):
            await ctx.channel.purge(limit=1)
            await ctx.channel.send(f"Hello {ctx.author.mention}")
        
        bot.run("token")