i am Making a MassDM command but it dont works well, it sends a Message not to Everyone it stops automatically i think because they turn their DM's off thats why how can i Bypass it? or how can i Fix this error?
Code
#client.command()
async def massdm(self, ctx, *, message):
await ctx.message.delete()
embed = discord.Embed(description=f'**Sent everyone a DM with:** ``{message}``',color=embcolor)
await ctx.send(embed=embed, delete_after=delemb)
for user in list(ctx.guild.members):
try:
await asyncio.sleep(0.1)
await user.send(message)
except:
pass
To debug it properly, the first step would be to remove the try, except block and find out what exceptions are you actually getting. It's a bad habit to catch every exceptions at once. Without knowing what exception is it throwing, I'm unable to help more.
Another thing I would recommend is to benefit from the discord.py being an asynchronous package, and instead of iterating over the users synchronously, try something like that:
coros = [await user.send(message) for user in ctx.guild.members]
await asyncio.gather(*coros)
This way it will send the message to every user at the same time.
EDIT:
I also found another bug while using a loop like that. At some point the bot will try to send the DM to itself, and that is raising AttributeError. Make sure that you handle that, for example:
coros = [user.send(message) for user in ctx.guild.members if user != ctx.guild.me]
await asyncio.gather(*coros)
Maybe that was your issue all along.
Try this:) Hope it helps
#client.command(pass_context=True)
async def massdm(ctx):
await ctx.message.delete()
for member in list(client.get_all_members()):
try:
embed = discord.Embed(title="Test",
description="Test!",
color=discord.Colour.blurple())
embed.set_thumbnail(
url="test")
embed.set_footer(
text=
"test"
)
#delay. Set it to 10 to 20 seconds to avoid flagging
await asyncio.sleep(30)
await member.send(embed=embed)
except:
pass
#await ctx.send(f"Messaged: {member.name}")
print(f"Messaged: {member.name}")
Related
Pretty much, for some reason the actual purging works, but it wont send the response embed after its purged. Sometimes it does, sometimes it just spams terminal with errors.
Heres is my code:
#tree.command(name= "purge", description="Purge messages in a channel", guild=discord.Object(id=guildId))
#app_commands.checks.has_permissions(manage_messages=True)
async def purge(interaction, limit: int):
await interaction.channel.purge(limit=limit)
embed = discord.Embed(description=f"Purged {limit} message(s)", color=0x3375FF)
await interaction.response.send_message(embed=embed)
Honestly, im not too sure what is wrong with it. I tried it without an embed and that still didnt work.
https://pastebin.com/uibRCN1F Here is a link to the error I am getting in the termina
It might be that your interaction is timing out. Perhaps try deferring and then using the followup property to send your message later.
#tree.command(name= "purge", description="Purge messages in a channel", guild=discord.Object(id=guildId))
#app_commands.checks.has_permissions(manage_messages=True)
async def purge(interaction, limit: int):
await interaction.response.defer()
await interaction.channel.purge(limit=limit)
embed = discord.Embed(description=f"Purged {limit} message(s)", color=0x3375FF)
await interaction.channel.send(embed=embed)
Ok so I've tried everything for my command but nothing worked for my !say command to work and not tag #everyone. I tried doing it myself but it still doesn't want to. I'm a beginner so i'm bad but where is the problem and what do i do?
here is my code:
async def say(ctx, msg=None):
if msg is not None:
await ctx.send(msg)
await ctx.message.delete()
if message == "#everyone" or "#here":
break:
Avoid specific mention when sending a message
discord.py already has a feature built-in, to allow or not specific type of mention.
Combining discord.AllowedMentions and discord.abc.Messageable.send, as the following example.
async def avoid_everyone(ctx: commands.Context, *message: str)
# Disallow everyone when sending message
allowed = discord.AllowedMentions(everyone=False)
# Raise an exception if #everyone in message
await ctx.send(message, allowed_mentions=allowed)
#MassDM Command
#bot.command()
async def massdm(ctx, msg):
await ctx.message.delete()
show_cursor()
wipeinput = input(Fore.RED+"Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n): ")
hide_cursor()
if wipeinput == "y" or wipeinput == "Y":
for user in ctx.guild.members:
if user != bot.user:
try:
channel = await user.create_dm()
await channel.send(msg)
print(Fore.GREEN + f'Sent to user "{user.name}"')
except:
print(Fore.YELLOW + f'Failed to DM user "{user.name}"')
pass
print(Fore.GREEN+"Finished")
When I run this it just says "Finished" and doesnt do anything. when i remove the try/except it gives not error? I have all the proper intents set up i think
You're making a mistake in this line:
wipeinput = input(Fore.RED+"Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n): ")
It seems that you're waiting for a response. The way to do that is with the wait_for function. You should change that to this:
await ctx.send("Are you sure you want to mass dm? (WARNING: This happens really fast so it will probably flag your account)(y or n)?")
wipeinput = bot.wait_for('message') # You can add another parameter to check if the input is valid and what you want.
In addition, I am unsure of what the functions show_cursor() and hide_cursor() are. They may also be causing something to not work.
EDIT: (Thanks to IPSDSILVA for pointing this out) Even though the code that I gave doesn't revolve around the issue in the post, any issue in the code may cause your code to not work
Here is the full code with everything working.
#client.command(pass_context=True)
async def massdm(ctx):
await ctx.message.delete()
for member in list(client.get_all_members()):
try:
embed = discord.Embed(title="Test",
description="Test!",
color=discord.Colour.blurple())
embed.set_thumbnail(
url="test")
embed.set_footer(
text=
"test"
)
await asyncio.sleep(30)
await member.send(embed=embed)
except:
pass
#await ctx.send(f"Messaged: {member.name}")
print(f"Messaged: {member.name}")
Here Is My Code:
#client.command(pass_context=True)
async def banall(ctx):
await ctx.message.delete()
for user in list(ctx.guild.members):
try:
await ctx.guild.ban(user)
await ctx.send(f"{user.name} has been banned from {ctx.guild.name}")
except:
await ctx.send(f"{user.name} has FAILED to be banned from {ctx.guild.name}")
I'm trying to make it so when someone says "banall" it will ban all the members in the server. I found have found this command works but with for example !banall" and I want to get rid of the "!" and so it will just be "banall". Does anyone know how I would go about doing this. Thanks :D
Adapt the command to work with on_message(). This can be done by removing the method decorator (#client.command(pass_context=True)) and changing ctx to message since the on_message() function gets a message object.
async def banall(message):
await message.delete()
for user in list(message.guild.members):
try:
await message.guild.ban(user)
await message.channel.send(f"{user.name} has been banned from {message.guild.name}")
except:
await ctx.send(f"{user.name} has FAILED to be banned from {ctx.guild.name}")
Run the function within the on_message(). This can be done with an if statement to check whether the message's content is "banall".
#client.event
async def on_message(message):
if message.content == "banall":
await banall(message)
If you want to cut the "!" from "!banall" you need to do this:
string = "!banall"
string2 = string.replace('!', '')
I'm trying to make a simple clear command & have it return an error message if the member does not have the manage messages permission.
#bot.command()
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=10):
await ctx.channel.purge(limit=amount)
This will do the trick.
#bot.command()
async def clear(ctx, amount = 10):
authorperms = ctx.author.permissions_in(ctx.channel)
if authorperms.manage_messages:
await ctx.channel.purge(limit=amount)
else:
await ctx.send("You don't have the permissions to do that!")
There is probably a better way of doing this by using the Exception raised when someone without the right permissions tries to use the command, or by overwriting the on_error method but I don't know how. This should fix the problem for you until you can find a better solution.