3
votes

TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.

Is there a way to use on_message and specify in an specific channel?

@client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
    await message.delete()
    await user.send("Yes?")
    await user.remove_roles(get(user.guild.roles, "Investigator"))

The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?

Is it possible to make this work through a command instead of an event?

Thanks for your help. :)

1
Apologies! I've found a workaround: @client.command() async def reveal(ctx, *, answer): user=ctx.message.author if answer == solution: await ctx.message.delete() await user.send("You're a winner!") await user.remove_roles(get(user.guild.roles, "Investigator")) else: await user.remove_roles(get(user.guild.roles, "Investigator")) await ctx.message.delete() await user.send("Sorry, " + answer + "is not the correct answer.") - Sigríðr E

1 Answers

1
votes

The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?

About this problem the clue is:

await bot.process_commands(message)

as docs says Without this coroutine, none of the commands will be triggered.

about the main question you could try using get_channel by ID and then purge it: eg.

@client.event
async def on_message(message):
    purgeChannel = client.get_channel([CHANNEL ID]])
     await purgeChannel.purge(limit=1)

you can add check to purge() to check for deleting specific message: eg.:

def check(message):
    return message.author.id == [author's ID]

@client.event
async def on_message(message):
    purgeChannel = client.get_channel([CHANNEL ID]])
     await purgeChannel.purge(limit=1, check=check)