Why is my discord bot code not running anything? - python

Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py

As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.

Related

Discord Python bot commands not responding

basically i have no idea why this code is not working, ive tried using on_message and that works so long as i dont include and if statement to filter for the messages with the prefix at the start. so i scrapped that, this code worked a few months ago with a different bot i made and ive ended up scrapping all the other stuff i was doing and bringing it down to the bare basics because i cant figure out why the bot doesnt recognise messages starting with the prefix.
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = "Token_here"
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(TOKEN)
ive tried print('Test') debugging aswell see below
#bot.command(name='tst')
async def test(ctx):
print('Test")
then in discord typing the !test command still does nothing and the terminal also remains empty other than the on_ready result of
Successful login as botname#0001
i have no idea whats going on honestly
There are a couple of things that could be causing your problems. I fixed the issue by enabling intents for the bot. This also must be done on the developer portal. https://discord.com/developers/applications
intents = discord.Intents().all()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
you also don't need the name parameter in the command decorator. It can just be written as :
#bot.command()
async def test(ctx):
await ctx.send('testt')
Try these changes:
bot= commands.Bot(command_prefix='!!', intents=discord.Intents.all())
#bot.command()
async def test():
await ctx.channel.send("Test successful")

#command decorator not working on discord.py

My discord bot connects just fine, event on_ready is working with no problems. However when I call any of the commands declared with #bot.command, the bot doesn't recognize the message as a command (at least that's what I think it's happening).
Code:
import discord
import pickle
from discord.ext import commands
from discord.ext.commands import bot
token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def test(ctx):
print('received')
await ctx.send('msg received')
bot.run(token)
That's pretty much it, when I run it the bot connects just fine, but the !test command doesn't work.
Let me know if I didn't add important information, first time asking something here.
EDIT: It's working locally, it doesn't work when it's being hosted on heroku (bot connects and is online, doesn't recognize commands).
Requirements file:
git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0

discord.py not responding to commands

When I give the bot a command, it doesn't reply.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix=">")
#bot.event
async def on_ready():
print("Bot is ready!")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(TOKEN)
The on_ready function works perfectly, displaying "Bot is ready!" in the console, however, when I type ">hello" in the discord channel, it doesn't reply.
Make sure the Bot has enough permissions to send messages.
To do so. Go to https://discord.com/developers/applications then click your application, head to the OAuth2 tab, then you will see several choices on Scopes find the bot and then Check it. After clicking the Bot you'll see a new tab on which below are all of the permissions. Click the permission(s) you desire, then copy the link that appears... Lastly reinvite the bot.
This should resolve the error, however if have more question, please comment them down.

Discord bot isn't responding to commands

I've been using the
import os
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle,
activity=discord.Game(''))
print('Successfully logged in as {0.user}'.format(client))
#client.command()
async def hello(ctx):
await ctx.send("Hello! It's great to see you!")
client.run(os.getenv('TOKEN'))
code for commands. I expected to have it respond "Hello! It's great to see you!" when I said !hello (! = prefix). But it returned nothing. Not even an error. Does anyone know why? Also, please don't show discord bot not responding to commands or Discord command bot doesn't responde (python) because I tried both, and neither worked.
I cant chat here because of lack of reputation so I answer it here instead. you should post the full code to make the job easier.
.
Cause 1:
You didn't add a prefix. You need a prefix to use the command
look onto your client. Did you tell the prefix already? like:
client = commands.Bot(command_prefix = "-")
if so, add this and it should work!
.
Cause 2:
You didn't import from discord.ext import commands
this is important if you want to use commands, import that and it should work!
.
Cause 3:
You mixing up client and bot
this might not happen but here it goes, You might mix up client or bot on the decorator, which is #client.command
if you use client (like client = commands.Bot(command_prefix = "-")), use #client.command on the decorator, while if you use Bot (bot = commmands.Bot(command_prefix = "-")) then you should use #bot.command on the decorator.
Hope these will help :D

Discord Bot not coming online

I'm using Visual Studio Code for my bot. I just started and I can't get my bot to come online. This is my code
from discord.ext import commands
client = commands.Bot(command_prefix = '-')
#client.event
async def on_ready():
print('YEEEEEEEEET')
client.run('BOT_TOKEN')
I'm following tutorial from https://www.youtube.com/watch?v=nW8c7vT6Hl4
You need to create a bot and generate a token following: https://discordpy.readthedocs.io/en/latest/discord.html
It was a certificate problem, and not problem with the code.

Categories