I can't make commands in discord.py - python

So I'm having a problem with discord.py.
I'd like to make a command to kick/ban people.
I tried this at first and it didn't work, so I went to check if commands worked at all for me. Apparently it doesn't. I've tried everything and nothing works.
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='!')
Token = 'TOKEN'
#bot.command(pass_context=True)
async def test(ctx):
await ctx.send('test')
client.run(Token)
I type !test on the channel but nothing happens.
I've tried basically everything.
I've changed the prefix, done: #bot.command(name='test), basically everything you could think of. Nothing works. I'd like to know if I'm missing something crucial. Like do I have to download something first or am I missing something in the code or are there permissions I need to enable. I've looked all over the discord py API reference. Anything would be helpful thanks.

Your problem is because of bot = commands.Bot(). You can use this code instead:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.command()
async def test(ctx):
await ctx.send('test')
client.run(token)
So you just have to delete bot = commands.Bot() then replace the #bot.command() with #client.command.

Related

Discord.py bot wont reply to commands

This is my first time using the Discord.py library. I have the following code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
bot = commands.Bot(intents=intents, command_prefix='$')
#client.event
async def on_ready():
print(f'Logged in as {client.user}!')
#bot.command()
async def say(ctx, arg):
await ctx.send(arg)
client.run('MY_TOKEN')
When I say "$say "hello world"" I don't get any error message, but I also don't get any response from the bot. Can someone tell me what the issue is, I'm sure it's a simple mistake. Thanks.
I figured out what I did wrong. I didn't need to include client and bot as separate things. I simply removed client = discord.Client(intents=intents) and the code ran fine (obviously I replaced every instance of 'client' with 'bot').

Why is my slash command not displayed despite correct usage?

I am currently working with the discord-py-slash-command library and have read through the documentation here: https://discord-py-slash-command.readthedocs.io/en/latest/quickstart.html
But for whatever reason it doesn't work, the command is not recognized/on the two servers, and the private messages of the bot the slash command doesn't show up.
I know that discord.py doesn't support slash commands yet, but this lib actually seems to work, at least from what I saw. Does anyone here see the mistake I made? I followed tons of tutorials with no success...
I already removed sync_commands=True or even tried to remove guild_ids and then wrote a message to the bot. Global commands take up to an hour to be displayed, but I actually avoid that with guild_ids.
Here is my code so far:
import discord
from discord.ext import commands
from discord_slash import SlashCommand # The lib
intents = discord.Intents.all()
client = commands.Bot(command_prefix="-", intents=intents)
slash = SlashCommand(client, sync_commands=True)
TOKEN = "..."
#client.event
async def on_ready():
print("Ready!")
print(slash.commands)
# Prints: {'ping': <discord_slash.model.BaseCommandObject object at 0x000002184B23E708>}
guild_ids = [812510632360149XXX, 871078836870185XXX]
# Directly from the docs!
#slash.slash(name="Ping", description="Ping command", guild_ids=guild_ids)
async def _ping(ctx): # Defines a new "context" (ctx) command called "ping."
await ctx.send("Pong!")
#client.command(name="test") # Test command which works
async def test(ctx):
await ctx.send("test")
client.run(TOKEN)
I also looked at other questions like: Discord.py | Slash commands aren’t working but they did not help either. Is this a problem with how I code/the program? (PyCharm)
You first have to set it to "True" in the App Settings of Discord. Here we turn on Text Box and can now use Slash commands, Most of the time this will be the error, because your code itself does not contain any, at least from what I have tested.
Here is a picture:
What about PRESENCE INTENT and SERVER MEMBERS INTENT in bot settings? cause i copied your code and tried to run, it worked for me. I've attached the images here. https://imgur.com/a/S0IuSlY
import discord
from discord.ext import commands
from discord_slash import SlashCommand # The lib
intents = discord.Intents.all()
client = commands.Bot(command_prefix="-", intents=intents)
slash = SlashCommand(client, sync_commands=True)
#client.event
async def on_ready():
print("Ready!")
guild_ids = [85528118773940xxxxx]
#slash.slash(name="Ping", description="Ping command", guild_ids=guild_ids)
async def _ping(ctx): # Defines a new "context" (ctx) command called "ping."
await ctx.send("Pong!")
#client.command(name="test") # Test command which works
async def test(ctx):
await ctx.send("test")
client.run('token')

Discord Bot Not Responding to Commands (Python)

I've just gotten into writing discord bots. While trying to follow online instructions and tutorials, my bot would not respond to commands. It responded perfectly fine to on_message(), but no matter what I try it won't respond to commands. I'm sure it's something simple, but I would appreciate the help.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
TOKEN = '<token-here>'
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
#bot.command(name='go')
async def dosomething(ctx):
print("command called") #Tried putting this in help in debugging
await message.channel.send("I did something")
bot.run(TOKEN)
Picture of me prompting the bot and the results
I made the same mistake at first.
#bot.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
This function overiding the on_message event so it is never sent to bot.command()
To fix it you just have to add await bot.process_commands(message) at the end of the on_message function:
async def on_message(message):
if message.content == 'test':
await message.channel.send('Testing 1 2 3')
await bot.process_commands(message)
Haven't tested yet but that should fix your issue.
Ok. First of all, the only import statement that you need at the top is from discord.ext import commands. The other two are not necessary.
Second of all, I tried messing around with your code myself and found that the on_message() function seems to interfere with the commands so taking that out should help.
Third of all, I only found this out when I duplicated one of my own working bots and slowly changed all of the code until it was identical to yours. For some reason python didn't like it when I just copied and pasted your code. I have never seen something like this before so I honestly don't know what to say other than that your code is correct and should work as long as you take the on_message() function out.
Here's the final code that I got working:
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
TOKEN = "<token-here>"
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.command()
async def dosomething(ctx):
await ctx.send("I did something")
bot.run(TOKEN)
As you can see the only things that I have changed from your code are that I removed the redundant imports at the top, and I deleted the on_message() function. It works perfectly like this on my end so I would suggest that you re-type it out like this in a new file and see if that works.
If that doesn't work for you then my next guess would be that there is a problem with your installation of discord.py so you might try uninstalling it and then reinstalling it.
If none of that helps let me know and I will see if I can help you find anything else that might be the cause of the problem.
just put client.process_commands(message)
in on_message event at last..

Changing discord.py bot using command

How would I go about changing my bots avatar with a command such as pfpchange or something similar?
Any help would be appreciated, I've tried the following code with zero luck.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='=')
client.remove_command('help')
pfp = 'https://cdn.discordapp.com/attachments/751402019046162494/774903321844645968/pinkbulba.jpg'
#client.command()
async def pfpchange(ctx):
await client.edit_profile(password=None, avatar=pfp)
client.run('super secret token')
Bot.edit_profile() was renamed to ClientUser.edit(), which is why it doesn't work. You're supposed to use Bot.user.edit().
More info in the relevant API documentation

Discord.py - command ping has not been defined even when i have defined it

I was following a tutorial for discord.py and the person who was teaching was getting pretty complicated.
He told use to create a main_cogs.py file and i did it.
This is the exact code as him:
import config
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='-')
class Test(commands.Cog):
def __init__(self,bot):
self.bot= bot
#commands.command
async def ping(self ,ctx):
await ctx.send("Pong")
bot.run(config.token)
BTW config is a file with the token and all that stuff.
When i run it and type -ping(in chat) i get this in the console and nothing in chat:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "ping" is not found
for him it just works.
The video
Thank You for the help!
change
#bot.command() to #commands.command()
remove ctx from the def __init__(self,bot,ctx): so its def __init__(self, bot):
Hm, I use a different way of coding the bot, I'll show you my way of making a bot reply.
from discord.ext import commands
import discord
bot= commands.Bot(command_prefix='-')
#bot.command()
async def ping(ctx):
await ctx.send('pong!')
bot.run('token')
and my opinion on the issue? It might be because of the indented block. Since that means that that would need to trigger for that to even exist in the first place. and the missing parentheses after the command is also probably an issue. 100% recommend you to remove the block and add the parentheses before just trying what I made.
I was dumb. I still am.
I DID NOT CALL THE FUNCTION!

Categories

Resources