Discord bot isn't responding to commands - python

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

Related

#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

Discrod.py rewrite #bot.command not responding no matter what i do

ok, first time ever asking a question so i apologize if i do something wrong. i am trying to use the #bot.command feature and it wont return anything no matter what i do.
from discord.ext import commands
from config import *
from discord import *
import discord
import time
import random
client = discord.Client()
bot = commands.Bot(command_prefix='+')
#bot.command()
async def yes(ctx):
await ctx.send('yes')
print("worked")
bot.add_command(yes)
client.run("token")
bot.run("token")
so in theory when someone types "+yes" is discord it should respond with "yes" as well as print "worked" in the terminal. instead i just get nothing. i know the bot works because i have '''other on_message''' commands but i have to use the offical '''#bot.command''' for this because it will eventually be part of interactivity.
You need to supply the command to the command, That's how I usually do it. Unsure of your method but perhaps try it this way:
client = discord.Client()
bot = commands.Bot(command_prefix='+')
#bot.command(name="yes")
async def yes(ctx):
await ctx.send('yes')
print("worked")
# bot.add_command(yes) # How will it know to map function yes to the command yes?
client.run("token")
bot.run("token)

Why is my discord bot code not running anything?

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.

my Discord bot command prefix not working

Im making a simple discord server bot in python and im tryan use "sudo" as my command prefix, for some strange reason when i try the prefix ex "sudo hi" or something like that it dosent work. When i try a preifx such as "/" it works. Could anyone explain why?
#!/usr/bin/python3
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = "sudo")
#client.event
async def on_ready():
print("Bot is online")
Your current prefix is "sudo", therefore a valid command would be, for example, "sudohi". As #chiragzq suggested, adding a space to your prefix definition should fix your issue.
client = commands.Bot(command_prefix = "sudo ")

Why is my discord bot spamming?

I have previously posted on this same bot and got it working thanks to the people who responded. But, while it finally came to life and turned on, it started spamming messages for no apparent reason. I've looked over the code for typos and can't find any.
Here's the code:
import discord
from discord.ext.commands import bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot (command_prefix = discord)
#client.event
async def on_ready() :
print("Bepis machine fixed")
#client.event
async def on_message(message) :
if message.content == "bepis" :
await client.send_message (message.channel, "bepis")
client.run("Censored Bot Token")
after #client.event is where i need help. also the bottom line if fine this time! turns out i had hit the space bar before the parenthesis and it didn't like that. Help is very appreciated so i can continue adding on to this awesome bot.
It looks like you are sending a message "bepis" in response to the first, then every, message "bepis" - presumably your first response will appear as an entry on the incoming feed which will trigger a second, etc.
Turns out I was not using proper formatting for my bot.
Whenever you say "bepis" in the discord server, the bot will see it and then say "bepis" back, as its intended to do, but, because of my improper formatting, the bot saw itself say "bepis" and responded as if someone else was saying "bepis".
Old lines:
if message.content == "bepis" :
await client.send_message(message.channel, "bepis")
New lines:
if message.content.startswith('bepis'):
await client.send_message(message.channel, "bepis")
So make sure you're using the right format if you're making a bot!
You seemed to already know what the problem is, from this answer you posted. But your solution is far from being able to solve the problem.
on_message is called whenever a new message is sent to anywhere accessible by the bot; therefore, when you type in "bepis" in discord, the bot replies with "bepis", then the message sent by the bot goes into on_message, which the bot re-replies "bepis", and so on...
The simple solution is to check if the message's author is any bot account, or if you want, if the message's author is your bot.
from discord.ext import commands
client = commands.Bot(command_prefix=None)
#client.event
async def on_ready():
print("Bepis machine fixed")
#client.event
async def on_message(message):
# or `if message.author.bot:` # which checks for any bot account
if message.author == client.user:
return
if message.content == "bepis":
await client.send_message(message.channel, "bepis")
client.run("Token")
Note: I also fixed many of your other problems, such as multiple unused imports, another Client, and indentation.
And FYI, command_prefix is only used when command is being processed by a functional command. When you use on_message it has no use, which means you can set it to None.

Categories