Command for mp a role - Discord.py - discord.py

I want I i want to make an command that mp a role when you send a message such as "#role, message"
Here is my code :
#bot.command()
async def DM(ctx, role : discord.Role.members, content):
channel = await role.create_dm()
await channel.send(content)
Thank you in advance

I assume you mean by mp you mean mass ping. Regardless to have a command like this would almost certainly be painfully ratelimited (10 user dms per 10 seconds), but let's say hypothetically you don't care, this is how I would approach it
(this requires the members intent)
#bot.command()
async def DM(ctx, role : discord.Role.members, content):
for member in ctx.guild.members:
if role in member.roles:
channel = await member.create_dm()
await channel.send(content)

first of all thank you for your help! But from my side it seems that the code doesn't work ^^
Besides this order will not be used to harass people lol Just to send a newspaper when these people will have chosen a role beforehand
The Error :

Related

how to mention a specific channel in a await message

Problem: don't know how to mention a specific channel, it would be nice if some of you could help me
this code run but it doesn't mention the playground channel
#bot.command()
async def prov(ctx):
if ctx.channel.id == 1028873503010717717:
await ctx.send("The bot is disabled in this channel!,\n try in #playground")
else:
If you mention as in just display a clickable link/mention, just use <#channelid>
Eg: <#1283768190123132>

Advanced Discord Ban Command

i need a ban command that bans a member and than dms the banned member to inform them about it and when it happened.
I have a few problems:
What if the user's dms are closed?
It is not dming the banned member.
Also includes a perm ban like `!Ban
If anyone can help me out that will be great and i will greatly appricate it.
Thanks!
Like you want, this is example of simple ban member for discord.py
#client.command()
async def ban(ctx, member:discord.Member, *, reason=None)
await member.ban
await member.send(f"You've been banned by {ctx.author}\nReason = {reason}")
If you looking for .cogs version
#commands.command()
async def ban(self, ctx, member:discord.Member, *, reason = None)
await member.ban
await member.send(f"You've been banned by {ctx.author}\nReason = {reason}") ##this send message by DMs for banned member to tell him/her why getting banned
If you using discord-py-slash-command you can create new question
for the 1:
try:
await user.send("msg")
except discord.errors.Forbidden:
code here
I don't know if this can help you

Discord bot sends many questions in dm at a time without waiting for the user to respond

I need my bot to send questions to users for them to answer when the user writes a command.
but the bot ended up sending 2 or 3 questions at a time randomly without waiting for the user to answer.
Image that shows the output of the bot.
which makes the value of RP N = "Name:"
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!add'):
await message.author.send(
"``Fill in the blanks by replying, then the bot will give a confirmation message.``")
for element in report:
await message.author.send("**"+element+"**")
response = await client.wait_for('message')
var(response.content)
report is the list of the questions
var is list.append
Edit: I have tried to add check argument.
def check(m):
return m.content != element in report
And that didn't work, still having the same issue.
This fixed my problem.
def check(m):
return message.author == m.author and isinstance(m.channel, discord.DMChannel)
To delay commands I suggest using time.sleep() TimeSleep if you don't want it to respond so fast you can put a little delay.

How do I delete a channel in discord.py?

No, this isn't a duplicate, none of the other solutions I've tried have worked.
I'm trying to make a bot that deletes a channel on command, say, deletechannel, but none of the solutions I've tried have worked. Can I get a full code snippet, with all the first-time stuff? I'm outright puzzled, and the docs haven't been any help.
You can use channel = bot.get_channel() and then channel.delete() that should solve your problems
This is what you are looking for.
#client.group(name='say', invoke_without_command=True)
async def say(ctx):
pass
#say.command(name='delete')
#has_permissions(manage_channels=True)
async def delete_subcommand(ctx, channel: discord.TextChannel = None):
if not channel:
await ctx.send("You must provide a channel.")
return
await channel.delete()
await ctx.send(f"{channel} has been removed.")
You can use this by doing {prefix_of_your_bot}say delete #channel
If this answer helped you, I would appreciate it if you marked this answer as the one that answered your question.
Using Greedy input with discord.TextChannel you can get channel objects and after that delete them all.
commands.Greedy A special converter that greedily consumes arguments until it can’t
#bot.command()
#commands.guild_only()
async def deletechannel(ctx, channels: commands.Greedy[discord.TextChannel] = None):
if not channels:
return await ctx.reply("Please enter a vailid channel")
for channel in channels:
await channel.delete()
Sample usage $deletechannel #channel1 #channel2 123456 where 123456 is a channel id

Discord.py DM command (To send and recieve a dm)

I'm pretty new to python and well, I wanted to make a discord bot that sends a dm, and when the other user receives the dm, they answer back, and the owner of the code or an admin of a server will be able to see what it says!
Here is my code so far for the dm:
#client.command()
async def dm(ctx, user: discord.User, *, message):
await user.send(message)
How do I make it so that i can see what the user replied to the bot?
For this, you can use client.wait_for() with check(), so add this after await user.send(message):
def check(msg):
return msg.author == user and isinstance(msg.channel, discord.DMChannel)#check for what the answer must be, here the author has to be the same and it has to be in a DM Channel
try:
answer = await client.wait_for("message", check=check, timeout=600.0)#timeout in seconds, optional
await ctx.send(answer.content)
except asyncio.TimeoutError:
await author.send("Your answer time is up.")#do stuff here when the time runs out
all if this is in your dm function.
References:
wait_for(event, *, check=None, timeout=None)

Resources