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
Related
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>
So i startet today with python and wanted to code a discord bot. Now I ran into that problem that I have 4 asyncs all of them back to back but only the first one is working.
bot = commands.Bot(command_prefix="$")
async def Member(ctx):
await ctx.channel.send("||#Member||")
async def Supporter(ctx):
await ctx.channel.send("||#Supporter||")
async def everyone(ctx):
await ctx.channel.send("||#everyone||")```
So, I think you might be new to this. But when we post questions here we provide the code along with an error if there is one. Otherwise, how are we going to know your issue. But I am going to try to guess. Your bot.start() before the other functions because if so, the bot wont recognize them.
Needs to be like this.
#commands.has_permissions(administrator=True)
#bot.command()
async def setdefaultrole(ctx, defualtRole):
guildID = ctx.guild.id
updateDefualtRole(guildID, defualtRole)
################################ Add Defualt Role ##################################################
##commands.has_permissions(administrator=True)
##bot.command()
#async def setdefualtrole(ctx, defualtRole):
#guildID = ctx.guild.id
#updateDefualtRole(guildID, defualtRole)
bot.run("TOKEN")
See how the bot.run() or bot.start() is at the bottom of all the functions. If this is not the problem, then add a code snippet from your code and then at me. Good luck.
So the answer to this question for anyone in the future. He need to add bot.command() to every new command he was trying to make. Otherwise, it will not register as a command.
New code:
#bot.command()
async def Member(ctx):
await ctx.channel.send("||#Member||")
#bot.command()
async def Supporter(ctx):
await ctx.channel.send("||#Supporter||")
#bot.command()
async def everyone(ctx):
await ctx.channel.send("||#everyone||")
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.
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 :
So I am working on a little project here, and pretty much, I want to have one of those "Please type the name of a channel in this server" feature.
So pretty much, the bot asks for a channel name, and I put in for example "#changelog" - and then it will ask for what it should write in that channel, etc etc.
So need to get the channel id (I am guessing), but I don't want users to write the ID, instead only writing the #server-name. And then whenever I have done that, the bot shall write in that channel.
Here is my current code!
class Changelog(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
print('Changelog is loaded')
#commands.command()
async def clhook(self, ctx):
await ctx.send('Write text-channel: ')
text_channel = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=300)
clhook = self.client.get_channel(text_channel)
def setup(client):
client.add_cog(Changelog(client))
Edit:
The channel ID shall be saved "forever", meaning that I do not have to re-write the channel name where the message should go!
You can use discord.utils.get() with this example:
text_channel = await self.client.wait_for("message", check=lambda message: message.author == ctx.author, timeout=300)
channel = discord.utils.get(ctx.guild.text_channels, name=text_channel)
await channel.send('Bla Bla')
So when you type (prefix)clhook then only the channel name, for example general, it will send Bla Bla to the channel named general .
There is another way to do this and I think it's simple than the first option, here it is:
#commands.command()
async def clhook(self, ctx, channel: discord.TextChannel):
await channel.send('Bla Bla')
So in this command, usage is changed. You can use that with this: (prefix)clhook #general(mention the channel). I suggest this solution and I think it's more useful.
You can use message.channel_mentions. This will return a list of all channels that were mentioned using the #channel-name notation. That way, you can just use channel.id to get the id of the channel they mentioned.
Don't forget, however, to check if the user did in fact tag a channel (which you can also put in your check). I put it in a separate function to make it a bit more readable for the sake of this reply, but you can fit that in your lambda if you really want to.
Also, make sure to check if it's a Text Channel and not a Voice Channel or Category Channel.
#commands.command()
async def clhook(self, ctx):
def check(self, message):
author_ok = message.author == ctx.author # Sent by the same author
mentioned_channel = len(message.channel_mentions) == 1 and isinstance(message.channel_mentions[0], discord.TextChannel)
return author_ok and mentioned_channel
await ctx.send("Write text-channel: ")
text_channel = await self.client.wait_for("message", check=check)
chlhook = text_channel.channel_mentions[0]
I put two conditions on the mentioned_channel line, because if the first one fails, the second one could cause an IndexError. Alternatively you can also use an if-statement to return sooner at that place to solve the same issue.