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

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)

Related

discord.ext.commands.bot Ignoring exception in command None discord.ext.commands.errors.CommandNotFound: Command "ping" is not found

so i was trying to create my first discord bot, with no experience in Python whatsoever just to learn stuff by doing so and stumbled on an Error (Title) which was answered quiet a lot, but didn't really help me.
Im just gonna share my code:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix="=", intents=discord.Intents.all())
#client.event
async def on_ready():
print("Bot is connected to Discord")
#commands.command()
async def ping(ctx):
await ctx.send("pong")
client.add_command(ping)
#client.command()
async def magic8ball(ctx, *, question):
with open("Discordbot_rank\magicball.txt", "r") as f:
random_responses = f.readlines()
response = random.choice(random_responses)
await ctx.send(response)
client.add_command(magic8ball)
client.run(Token)
I tried to run this multiple times, first without the client.add_command line, but both methods for registering a command didn't work for me. I also turned all options for privileged gateway intents in the discord developer portal on. What am i doing wrong?
Because you have an incorrect decorator for your command ping. It should be #client.command() in your case.
Also, in most of the cases, you don't need to call client.add_command(...) since you use the command() decorator shortcut already.
Only when you have cogs, you will use #commands.command() decorator. Otherwise, all commands in your main py file should have decorator #client.command() in your case.
A little bit of suggestion, since you already uses commands.Bot, you could rename your client variable to bot since it is a little bit ambiguous.
You have to use Slash commands cause user commands are no longer available
Example code using Pycord library
import discord
bot = discord.Bot()
#bot.slash_command()
async def hello(ctx, name: str = None):
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
bot.run("token")

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

Im trying to setup a clear/purge command for my discord.py bot. There are no errors but when I run the command, nothing happens

This the start of the code. Did I type something wrong in the code that doesn't make the command work?
import discord
import os
from keep_alive import keep_alive
from discord.ext import commands
import random
client = commands.Bot(command_prefix= '>')
#client.event
async def on_ready():
print('The bot is online')
await client.change_presence(
activity=discord.Game('>help ┃ Created by ColeTMK'))
#client.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=6):
await ctx.channel.purge(limit=amount)
I have explained properly how to do that. Using the coroutine it is possbile.
There seems to be no error in your code. I would just say create the command as I have mentioned below and try again.
I created a purge command first which has the following code:
#nucleobot.command()
#commands.has_permissions(manage_messages=True)
async def purge(ctx, limit: int):
await ctx.message.delete()
await asyncio.sleep(1)
await ctx.channel.purge(limit=limit)
purge_embed = discord.Embed(title='Purge [!purge]', description=f'Successfully purged {limit} messages. \n Command executed by {ctx.author}.', color=discord.Colour.random())
purge_embed.set_footer(text=str(datetime.datetime.now()))
await ctx.channel.send(embed=purge_embed, delete_after=True)
How does this code work?
The command usage is deleted, i.e., !purge 10 would be deleted when sent into the chat.
It would pause for 1 second due to await asyncio.sleep(1). You would need to import asyncio in order to use it. (You might know that already :D)
The number of messages your entered are cleared from the channel using await ctx.message.delete(limit=limit) (This is a discord.py coroutine)
purge_embed is the embed variable which is used to send the embed after the deletion. I have used datetime module to add the time of the command completion on the embed. (You need to import datetime as well, but only if you want to use it. If not then remove the footer code.)
This would make up your complete and working purge command. :D
Examples with images.
I created a new channel and added 10 messages with numbers from 1 to 10 as shown below:
Then I entered the command in the message box and it was like (I know it was not needed but never mind):
After I sent this message and the command was executed, the purge successful embed was posted by the bot:
I was glad I could help. Any doubts of confusions are appreciated. Ask me anytime. :D
Thank You! :)

discord.py (rewrite) How can I narrow a commands use ability to a single channel?

I'm trying to make this code work only in a specific channel. It just sends a ton of errors when I try to do the command in the right channel. Ill add my imports if you want to test it. Last time I tried to ask this question it got denied. I still don't know what to do and just need a little help as i'm new to coding a discord bot.
import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather
client = commands.Bot(command_prefix='.')
#client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
channel = discord.utils.get()
if channel == 705161333972140072:
await ctx.channel.purge(limit=1)
await ctx.send(f'{random.choice(pictures)}')
client.run('token')
You need a few changes in the command function:
Fix indentation
Use ctx.channel.id instead of channeland discord.utils.get()
Rather than purging, delete the command msg
#client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
if ctx.channel.id == 705161333972140072:
await ctx.message.delete()
await ctx.send(random.choice(pictures))
client.run('token')
You can also use checks

Why is my bot.command function in discord.py not working?

I have tried looking around the internet for answers but I'm not finding anything that fixes my problem:
import os
import asyncio
import discord
from discord.ext import commands
token = "here is my token"
bot = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.command()
async def length(ctx):
await ctx.send('your message is {} characters long.'.format(len(ctx.message.content)))
print("test print")
bot.run(token)
The length function does not work at all, and its not printing test print in the console.
Does anyone know what the problem is? there are a few other bot.xxx functions in there that works.
It does not work because you have double bot.
Your should only have bot = commands.Bot(command_prefix='!')

Categories