简体   繁体   English

Discord Python bot 删除某个频道中不是命令的消息

[英]Discord Python bot that deletes messages in a certain channel which are not commands

So I have a #support channel in which the members of my discord can type =support and the bot will message them to assist them, after that the bot will delete the =support command that the user typed in the channel to keep the channel clean, however in this channel they can also type any message that is not a command and the bot will not delete this, is there a way for the bot to delete a message if it isn't a command?所以我有一个#support 频道,我的 discord 的成员可以在其中输入 =support 并且机器人会向他们发送消息以帮助他们,之后机器人将删除用户在频道中输入的 =support 命令以保持频道清洁,但是在这个频道中,他们也可以输入任何不是命令的消息,并且机器人不会删除它,如果不是命令,机器人有没有办法删除消息?

This can be done using on_message这可以使用on_message来完成

@bot.event
async def on_message(m):
    if m.channel.id == ChannelIDOfSupport Channel:
        if m.content.replace(bot.prefix, "") not in [i.name for i in bot.commands]:
            await m.delete()

So basically what I did is replace the actual command for a message listener and if it starts with support to do the command, then I restored the commands function.所以基本上我所做的就是替换消息侦听器的实际命令,如果它开始支持执行命令,那么我恢复了命令 function。 It looks like this:它看起来像这样:

async def on_message(m):
    if m.channel.id == 735866701069025301:

        if m.content.startswith('=ticket'):

            await m.author.send('Your ticket will be created according to what you choose, use the = before choosing a number and then put the number next to it (Example: =2) \n \n **1. General Support** \n \n **2. Staff Application**')
            await m.channel.purge()

        else: {
            await m.delete()
}
    await client.process_commands(m)

The code below was somewhat functional however, all my @client.command or @bot.command if you want, were not functioning properly, not your fault @Poojan, I didn't paste code so you couldn't know if I was using commands instead of events.下面的代码有点功能但是,如果你愿意,我所有的@client.command 或@bot.command 都不能正常运行,不是你的错@Poojan,我没有粘贴代码,所以你不知道我是否在使用命令而不是事件。 The code above works perfectly, thank you for your help, thank you for your help too @Xetera, I followed the code and looked a bit into it and came to that conclusion:)上面的代码完美运行,感谢您的帮助,也感谢您的帮助@Xetera,我按照代码进行了一些研究并得出了这个结论:)

vvvvvvvvvvvv That answer helped me lots. vvvvvvvvvvvv 这个答案对我帮助很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM