This is the code
@botmand()
@commands.has_permissions(manage_messages=True)
async def delMessages(ctx, id=None, reason=None):
guild = ctx.guild
id = discord.TextChannel.id(id)
await guild.delete_messages(id)
await ctx.delete_channel(ctx.delete_messages(), id)
await ctx.send(f'The user {ctx.author.mention} has been delete all messages for {reason} on the channel {id}')
await asyncio.sleep(10)
await ctx.delete()
i try start the command .delMessages for delete all messages, so that a spam bot isn't sending messages like crazy, but dont working
This is the code
@botmand()
@commands.has_permissions(manage_messages=True)
async def delMessages(ctx, id=None, reason=None):
guild = ctx.guild
id = discord.TextChannel.id(id)
await guild.delete_messages(id)
await ctx.delete_channel(ctx.delete_messages(), id)
await ctx.send(f'The user {ctx.author.mention} has been delete all messages for {reason} on the channel {id}')
await asyncio.sleep(10)
await ctx.delete()
i try start the command .delMessages for delete all messages, so that a spam bot isn't sending messages like crazy, but dont working
Share asked Mar 6 at 21:09 KristKrist 11 Answer
Reset to default 0Most of your code is not valid in the discord.py library. I recommend you read the quickstart guide to get a better base of understanding for typical command structures, and more closely reference example designs. It feels to me like you just made up commands hoping they would work. This is not a good way to do it.
You cannot get a channel by instantiating
discord.TextChannel
and then giving it an ID. Usechannel = ctx.bot.get_channel(channel_id)
.I'm not sure of the point of doing
id = channel.id
, if you already have the ID from the command arguments you don't need to redo that.await guild.delete_messages(id)
is not a valid command. There is no command to delete messages from a user on a guild-wide scale. Please check the commands available to the discord.Guild object and the discord.TextChannel object. The basic ways to delete a message are:# assuming "channel" is a discord.TextChannel and "message_id is an integer # Method 1: msg = await channel.fetch_message(message_id) # Method 2 (be careful you don't delete all messages in channel) for async msg in channel.history(...): if [ ... ]: # put in condition here for which messages to delete await msg.delete() # deletes last # Method 3 await channel.purge(..., check=...) # use "check" to choose which messages to delete
await ctx.delete_channel()
is not a valid command, but if it were, it's closer to deleting the channel itself than deleting messages from a channel, which is certainly not what you want. I think. I assume what you intended was something like "delete messages by author ID in this channel". If so, see above point.Your last line,
ctx.delete()
will delete the message of the command invocation of the user, which could be fine if that's what you want. If you want to delete the confirmation response from the bot as well, save the message returned from thesend()
command:msg = await ctx.send("...") await asyncio.sleep(10) await msg.delete() # delete user message (your message) await ctx.message.delete() # delete bot's reply # or: await ctx.message.delete() # delete user's message immediately msg = await ctx.send("...", delete_after=10) # delete bot reply after 10s