Slash commands produce "interaction failed" when invoked in chat? - python

I am using the discord-py-slash-command library and have setup my commands. However when I type my command in the chat I get interaction failed each time. I have given my bot the correct scope and reinvited it to the server several times. Can someone take a look at my code an tell me what is going on? I have followed several tutorials with no luck.
TOKEN = open("./token.txt", "r").readline()
bot = commands.Bot(command_prefix='-', intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to the Discord!')
#slash.slash(name="test", description='Test slash command activated', guild_ids=
[928451150515150890])
async def test(ctx: SlashContext):
embed = discord.Embed(title="Embed Test")
await ctx.send(embed=embed)

Edit: I am editing my answer because at the time I was not sure what library you were using.
Your code works just fine, however, If you are going to use intents=discord.Intents.all()
you need to be sure to enable presence as well as server members intents in the developer console of the bot.
If you do not need to use intents for your application, disable them and set it to intents=discord.Intents.default()

i switched over to the nextcord library for slash commands and the behavior is working as expected... I will consider this closed

Related

communication with my bot discord doesnt work

Hello im trying to communicate with my bot discord but its doesnt answer
the bot is online but no answer here the following code :
import discord
client = discord.Client(intents=discord.Intents.default())
client.run("token")
#client.event
async def on_message(message):
if message.content == "ping":
await message.channel.send("pong")
You need to enable the message content intent.
add this in your code under your intents definitions
intents.message_content = True
then head to the developer dashboard
and enable the Message Content at the Privileged Intents after that your code should work ;-)
The message_content intent mentioned above is necessary, but it's not the only thing wrong here.
When you call client.run(), nothing below it will execute until the client goes down. This means that your on_message event is never created. client.run() should be the very last line in your file.
Having your bot to answer to sent messages requires the message_content intent.
With intents=discord.Intents.default() following intents are DISABLED:
self.presences (= having your bot to see rich presence, e.g. what game's being played)
self.members (= having your bot to see the user of a guild)
self.message_content (= having your bot to see the content of messages)
You can now enable all mentioned intents or only specific intents. If you want to send a message, in response to a sent message, you need the intent self.message_content.
You can also add all intents to avoid any problems with them in the future (Note that after a certain amount of Discord servers you need to apply to use all privileged intents.)
intents = discord.Intents.all()
client = discord.Client(intents=intents)
You should consider activating the intents:
Visit the developer portal > choose your app > Privileged Gateway Intents.
For further programming in Discord.py, consider reading the docs, as there is a new version of Discord.py.

Python discord bot doesn't answer to any commands

The bot starts just fine, but it doesn't answer to any commands I make with its prefix, it doesn't even raise the "command not found" exception. The bot was working just fine until some hours ago, I made some changes and it stopped working. I reverted the changes, but it still doesn't work.
I tried commenting the cogs startup and it still didn't work, so I think they're not relevant to the problem. I also left a lot of the code out because it's pretty big, and I assumed it didn't matter because the bot is running and if there were any errors in my code, the commands should at least be executed, but let me know if there's something else I should include.
Here's the code:
(imports)
(global variables)
bot = commands.Bot(command_prefix='!', help_command=None, case_insensitive=True,
intents=discord.Intents.default())
#bot.event
async def on_ready():
(cogs setup)
await bot.change_presence(activity=discord.Game(name='random status'))
print("My prefix is: " + bot.command_prefix)
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def random_command(ctx):
print("Command executed)
def random_method():
(does something)
bot.run(token)
The "on_ready" event is executed just fine, and the bot comes online in discord. I don't have any on_message events and the bot has admin perms in my server.
To improve privacy bots now need a special intent in order to be able to read messages. The "new" commands (which i would recommend you to switch to) are slash commands as they are fully supported by Discord. You can find more information on how to set up slash commands here.
If you want to stick with the old method, here is he to activate the intent: First go to your Developer Portal, select your application and activate the Message Content Intent. Next you need to activate the intent in your code like this:
intents = discord.Intents.default()
intents.messages = True
bot = commands.Bot(command_prefix='!', help_command=None, case_insensitive=True, intents=intents)
Note that if your bot is on 100 or more servers it needs to be verified in order to access intents. More information on that can be found here.

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

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.

Say, send_message and send, do not work in #bot event what to do? discord.py

Hello reread all the documentation on discord.py, and unfortunately did not find a simple thing like the on_member_join event in the chat send a message?
I use a non-standard construction, and such a construction client = discord.Client (), but as I understood a new bot = commands.Bot(command_prefix='!')
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_member_join(member):
print(member.name)
bot.send(member.name);
Print () output to the console properly, but to send it to the chat of the discord unfortunately does not work(
I also tried:
bot.say(member.name);
bot.send_message(member.name)
bot.send(member.name)
But all the time an error is issued "'Bot' object has no attribute 'say'"
Please tell me what is wrong I do?
The version of discord.py you're using will change how you send messages.
discord.py 0.16, the "async" branch, is the current stable release. It has two ways of sending messages. (In the below, note that Bot is a subclass of Client, so every Bot also has access to all of the Client methods)
Using Client.send_message(target, message). This is what you
would use in on_message(message)
await bot.send_message(message.channel, "I am responding to your message")
Using Bot.say(message). This is an easy way of sending
messages back to the channel a command was invoked in. It only
works in commands.
await bot.say("I am responding to your command")
discord.py 1.0, the "rewrite" branch, is the most recent branch. It's considered experimental still, but is entirely usable. One of the many changes is the way that message sending works.
Now, instead of having methods on the client to send messages, we have methods on the things that receive messages to send them a message. These all implement the Messageable abstract base class, and have a send method. In your on_message, this would look like
await message.channel.send("I am responding to your message")
I believe you are using version 1.0

Categories