web-dev-qa-db-ja.com

不可解な単語が入っている場合は、Discord.pyメッセージを削除する

ノーなしの単語がない場合は、ボットがメッセージを削除してほしいが、その「Word」のみが送信された場合にのみ削除されます。

例:NOの単語は「憎しみ」ですが、「嫌い」を送信するとメッセージが削除されません...「嫌い」と言った場合にのみ削除してください。


nono = ['Hate']

@client.event
@commands.has_permissions(manage_messages = False)
async def on_message(message):
    if message.author.id == client.user.id:
      return
    
    if message.guild is not None:
        for Word in nono:
            if Word in message.content:
                await message.delete()
                await message.author.send('I hate you too')

                await client.process_commands(message)
            else : 
                return
 _
1

あなたが「私はあなたを憎む」と書いたことを確信していますか?

私はあなたのコードに従って最小限の例を構築しました、そしてそれはうまく機能します。

nono = ['Hate']
message = "I hate you"

for Word in nono:
    m = message.upper()
    if Word.upper() in m:
        print("delete")
    else:
        print("Nothing")
 _

あなたの検出が大文字と小文字を区別しないように、文字列を大文字の大文字にすることをお勧めします。

2
C0rn

更新:私の友人はそれを考え出しました。基本的にこれは彼からのコードです

nono ['Hate']

for Word in nono:
            if message.content.lower().find(Word)!=-1:
                await message.delete()
                await message.author.send("I hate you too dummy!")
 _

彼に賛美する:D.

0

JSONファイルを使用してみてください、私は個人的にいくつかのオートモットボットを作りました、そしてそれらすべてがJSONを使います。

これが例です。

 @commands.Cog.listener()
    async def on_message(self, message):
        user = message.author
        guild_id = message.guild.id

        em = discord.Embed(title="Profanity filter", description="Please refrain from using blacklisted words!", color=Magenta)
        em.set_footer(text=user.name, icon_url=user.avatar_url)
        em.set_author(name=message.guild.name, icon_url=message.guild.icon_url)
        
        with open(f"json/{guild_id}/blacklist.json", 'r') as f:
            badwords = json.load(f)

        if any(Word in message.content.lower() for Word in badwords):
            await message.delete()
            return await message.channel.send(embed=em)
 _

Discord Guild IDに対応する名前のフォルダ内のJSONファイルを読み取ります。その単語がそのファイルにある場合は削除します。

これはJSONファイルがどのように見えるものです。

{
"test": {
    "test": "test"
},
"test": {
    "test2": "test2"
}
}
 _
0
NikkieDev