Hello programmers friends!
I have a problem deleting a message after sending another one.
new_msg = await message.answer(attachment=photo)
await asyncio.sleep(2)
try:
new_msg.delete()
except Exception as e:
pass
await message.answer(response['choices'][0]['text'])
I read the documentation but didn't really understand it.
Can you please help?
Related
I want send to all servers, but command send me error
Command:
#bot.command(pass_context=True)
async def brd(ctx, *, msg):
for server in bot.guilds:
serv = bot.get_channel( 891652546853212161, 805527975612252180 )
for channel in serv:
try:
await channel.send(msg)
except Exception:
continue
else:
break
Error:
get_channel() takes 2 positional arguments but 3 were given
You basically cannot pass more than one id to the get_channel function.
One way to solve it is to create a list of ids and iterate trough that, so for example you can change your for loop to iterate trough channel ids, instead of creating server channel list.
ids = [891652546853212161, 805527975612252180]
for i in ids:
serv = bot.get_channel(int(i))
try:
await channel.send(msg)
#Wide and bare exceptions is a bad practice. Try to focus the exception on one error.
except Exception:
continue
else:
break
I'm trying to make a small spambot with discord.js, as one of my first python bots. It honestly works fine, but there's one problem. The bot is supposed to spam every channel in a server, but it only sends one message. I don't know python that well, and I know where the problem is and what I need to do to fix it, I just don't know how to fix it. I'm guessing I need to put an amount of messages that I want the bot to send, but I don't know how to do that. I'm hoping someone can help! (P.S. I'm not using the bot for anything bad, I just wanna test it out.) Anyway, here's my code:
#bot.command()
async def sall(ctx, *, message=None):
if message == None:
for channel in ctx.guild.channels:
try:
await channel.send(random.choice(spam_messages))
except discord.Forbidden:
print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
return
except:
pass
else:
for channel in ctx.guild.channels:
try:
await channel.send(message)
except discord.Forbidden:
print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
return
except:
pass
Well, you really only send one message!
#bot.command()
#bot.is_owner()
async def sall(ctx, *, message=None):
if message == None:
for channel in ctx.guild.channels:
try:
for i in range(10):
await channel.send(random.choice(spam_messages))
except discord.Forbidden:
print(f"{C.RED}Spam Error {C.WHITE}[Cannot send messages]")
return
except:
pass
else:
for channel in ctx.guild.channels:
try:
for i in range(10):
await channel.send(message)
except discord.Forbidden:
print(f"{C.RED}Sall Error {C.WHITE}[Cannot send messages]")
return
except:
pass
It should work like this. I added a for i in range(10), what it does is, it repeats this 10 times. So just change it to number of times you want it to send a message.
So I want to make my bot allow specific messages, like "/verify" in a specifc channel, if someone sent a message other than "verify" the bot should delete the message, however only in a specific channel, I'm new to all of that but I made this code and it's not working
async def verify(ctx):
user = ctx.message.author
role = 'Member' #change the role here
try:
await user.add_roles(discord.utils.get(user.guild.roles, name=role))
await ctx.send('Welcome to our server! :white_check_mark:', delete_after=1)
except Exception as e:
await ctx.send('Cannot assign role. Error: ' + str(e))
if not msg.content == '/verify':
await client.delete_message(msg)
await ctx.message.delete()
any help would be much appreciated.
You can just write a simple check right at the beginning of the function.
For example,
#bot.command()
async def verify(ctx):
if ctx.channel.name != "verify":
return
# rest of the code
Also, you have not defined msg in your code. So that'll also raise an error
You have to specify what exactly your "msg" is that you want to delete.
So instead of msg.delete it should be enough to instead write:
if ctx.message.content == "verify":
await ctx.message.delete
Because the ctx/context is where you get the information about what kind of message it is, which channel it was posted in and so on.
I'm trying to make a discord bot have the same functionality as an
input()command, but as discord.py rewrite didn't have that command, I searched the API and found wait_for. But, of course, it brought a whole load of problems with it. I searched the internet for this, but most of the answers were in a #command.command and not async def on_message(message) and the others weren't really helpful. the furthest I got was:
def check(m):
if m.author.name == message.author.name and m.channel.name == message.channel.name:
return True
else:
return False
msg = "404 file not found"
try:
msg = await client.wait_for('message', check=check, timeout=60)
await message.channel.send(msg)
except TimeoutError:
await message.channel.send("timed out. try again.")
pass
except Exception as e:
print(e)
pass
```
First of all, you're using the same variable msg for multiple things. Here is a working example I can make with the information you've given.
msg = "404 file not found"
await message.channel.send(msg)
def check(m):
return m.author == message.author and m.channel == message.channel
try:
mesg = await client.wait_for("message", check=check, timeout=60)
except TimeoutError: # The only error this can raise is an asyncio.TimeoutError
return await message.channel.send("Timed out, try again.")
await message.channel.send(mesg.content) # mesg.content is the response, do whatever you want with this
mesg returns a message object.
Hope this helps!
It's not finished but I'm adding a poll feature to my bot. But I want to be able to do something if no question or options are provided. I don't want to make an error handler because I feel it is extremely far beyond what I know.
#bot.command(pass_context=True)
async def poll(ctx, *, msg):
try:
split_msg = msg.split(";")
question = split_msg[0]
option1 = split_msg[1]
option2 = split_msg[2]
embed = discord.Embed(title="Question", description=question)
embed.add_field(name="Option 1", value=option1, inline=True)
embed.add_field(name="Option 2", value=option2, inline=True)
error = discord.Embed(title=":warning: Incorrect Syntax!", description="Usage: {}poll <question>; <option1>; <option2>".format(prefix))
await bot.say(embed=embed)
except CommandInvokeError:
print("CommandInvokeError")
No. CommandInvokeError is the exception that is raised by the bot invoking the command when command execution raises any uncaught error. Your code won't raise a CommandInvokeError, so you can't catch it. If you examine the full error message, you'll likely see that your CommandInvokeError is being caused by another error, which you can catch.