pycord bot quits slash command too quick - python

When my bot is very busy, it sometimes takes a few seconds to respond to the slash commands. However, before he can reply, Discord sends the message "The application did not respond". How can I make Discord wait longer for a message from the bot?

Have you tried using Interaction.defer()? Here's a quick example on how to use that:
#bot_instance.slash_command(name="hi")
async def hi(ctx):
await ctx.defer()
# fairly long task?
await ctx.followup.send( # Whatever you want to send...
For more information check out the API Reference: https://docs.pycord.dev/en/master/api.html#discord.Interaction
Also see a GitHub issue related to this question: https://github.com/Pycord-Development/pycord/issues/264

There is something called defer, you can do that to increase waiting time of the function
#bot_instance.command(description="Description of the command"
async def command(ctx:discord.Interaction,keyword:str)#Keyword if any
await ctx.response.defer(ephemeral=True)#This line defers the function
#Do your process here
await ctx.followup("Followup message")#This is the confirmation message

Related

"Application doesn't respond" but command sends output anyway (discord.py slash commands)

I'm making slash commands for discord bot in dicord.py and when command is executed it shows that app doesn't respond but sends command output anyway. What is wrong?
Command's code:
#bot.tree.command(name="ping", description="Check bot's latency.")
async def ping(ctx):
await ctx.channel.send(f"``Pong! {round(bot.latency * 1000)}ms`` 📡")
Try adding a defer command. For application commands, you must first defer and then followup afterwards if the interaction takes longer than 3 seconds. The library can handle this automatically for you in hybrid commands using Context.typing().
#bot.tree.command(name="ping", description="Check bot's latency.")
async def ping(ctx):
await ctx.defer(ephemeral=True)
await ctx.channel.send(f"``Pong! {round(bot.latency * 1000)}ms`` 📡")
Also might be worth to use
ctx.reply
Instead of
ctx.channel.send
You are using application command, so there should be interaction. There is no ctx for application command.
Do something like:
#bot.tree.command(name="ping", description="Check bot's latency")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message(f"``Pong! {round(bot.latency * 1000)}ms`` 📡")
See more examples here
If you want to keep both application command and traditional command (before dpy2.0 with ctx), consider using hybrid command

How to quit a discord bot without using 'except' python

need quick help this is the code that simply posts to a discord channel I am using it as a subpart of my project all I want from this to post and shuts (without killing the program) so I can't use 'exit', I have used while loops and all that but nothing is working.
def post_discord():
print('POSTING NEW OFFER TO DISCORD')
token = 'token here'
while True:
bot = commands.Bot(command_prefix='!')
#bot.command()
async def on_ready():
await bot.wait_until_ready()
channel = bot.get_channel(12345678) # replace with channel ID that you want to send to
# await channel.send()
await channel.send(text,file =discord.File('promo.png'))
await asyncio.sleep(2)
# exit()
# await bot.close()
bot.loop.create_task(on_ready())
bot.run(token)
time.sleep(3)
If you really want to just stop the bot from running after it finishes doing whatever you want it to do, what you're looking for is discord.Client.logout(). In your scenario, the usage would be bot.logout()
Although, as someone who replied to your post said, a webhook may be more suitable for your use case. A simple webhook for sending a message using requests would look something like this:
import requests
data = {"content": "Message Content"}
requests.post("Webhook URL", data=data)
Discord's documentation for this can be found here, notable limitations for this route are that you're unable to attach files, but if you're just needing to send an image along with some text, a .png link may suffice as Discord will hide the link in the message.

Python - Discord bot on ready send message to channel

I have problem creating simple python script which sends message to default channel when executed from terminal.
import discord
#bot.event
async def on_ready(ctx):
print('Online')
await ctx.send('Message sent!')
bot.run('MYTOKEN')
with this example I keep getting "ctx" is not defined
The issue here is that the on_ready event should not receive any parameters. See the Minimal Bot documentation here and the on_ready documentation in the Event Reference here. If you want to send a message when the bot connects. You must first get the channel object and then use the send method. You can find an example of getting a channel and sending a message in the FAQ section of the docs here

Command not found error in Discord Python code

Whenever I run my discord python code, and test it in the discord chat, it says the ping command is not found even though I defined it in the code.
I've tried to use both Bot and Client and both gave the same error.
import discord
from discord.ext import commands
bot_prefix= "]"
bot = commands.Bot(command_prefix=bot_prefix)
bot.run("*")
#bot.event
async def on_ready():
print("ok")
#bot.event
async def on_message(message):
print(message.content)
#bot.command()
async def ping(ctx):
latency = bot.latency
await ctx.send(latency)
Personal information replaced with "*"
The bot should send a message in the user's channel saying the latency of the bot, but instead I just get an error that says:
"Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "ping" is not found" even though I defined the ping command in the code.
Also, it should be noted that the on_ready event never runs; I never get the print statement in the console log.
Any help is appreciated thanks :)
bot.run must be the last line in your code. Python executes sequentially, so all of the stuff below bot.run isn't called until after the bot is finished running.
Okay I got it fixed!!
Apparently there's an issue with the on_message function, I guess I just skipped over it in the FAQ. Anyone confused about this, just add the line:
await bot.process_commands(message)
into your on_message function. When you define your own on_message function it overrides the original one that passes the message into the commands handler.
Also make sure to use bot.run() at the end of your code, after your function declarations. Simple mistakes, but they're all fixed now :)

How to delete bot direct messages?

So, long story short I was making a bot that's supposed to send a direct message to a user after a command was executed, then wait a bunch of time before deleting it
It works fine and all now, but for a bit of time it didn't, and while I did some testing, my dms with that bot have been flooded with test messages
Is there any command I could add to delete every old messages that this bot sent to my dms ?
You could write a censor command that calls purge_from on the channel it's invoked from.
#bot.command(pass_context=True)
async def censor(ctx, limit: int= 100):
await bot.purge_from(ctx.message.channel,
check=lambda message: message.author == bot.user,
limit=limit)

Categories

Resources