Uzayer Masud
Uzayer Masud

Reputation: 13

auto mute a member if they say a specific word discord py rewrite

import discord
import re
from itertools import cycle


class Status(commands.Cog):
    def __init__(self, client):
        self.client = client
 @commands.Cog.listener()
    async def on_message(self, ctx):
        if ctx.author.id == self.client.user.id:
            return
        if re.search("\.\.\.", ctx.content):
            await ctx.send("you're... gonna get... muted... if you.. talk... like.. this...")
            user = ctx.message.author
            # print(str(user))
            # print(str(message.content))
            muted_role = discord.utils.get(ctx.author.guild.roles, name="Muted")
            await self.client.add_roles(ctx.author, muted_role)

What I want is to temporarily mute a user if they use ellipses in a message they send. ctx.send doesn't work, the bot does not send a message to the channel. It says that self.client.add_roles doesn't exist.

Muted is a role that I have created that does not have any send message permissions.

Any idea why? Some help would be hugely appreciated. I'm using

AttributeError: 'Message' object has no attribute 'send' this is the error I get

[EDIT]

    @commands.Cog.listener()
    # @commands.command()
    async def on_message(self, message):
        if message.author.id == self.client.user.id:
            return
        if re.search("\.\.\.", message.content):
            await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
            user = message.author
            muted_role = discord.utils.get(message.guild.roles, name="Muted")
            await user.add_roles(muted_role, reason="you know what you did", atomic=True)

I took a look at the documentation and did this, it works, thank you for the support :)

Upvotes: 1

Views: 1058

Answers (1)

Harjot
Harjot

Reputation: 943

There were many mistakes, please have a look at the documentation.

I have correct them for you -

 @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.id == self.client.user.id:
            return
        else:
            await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
            user = message.author
            print(str(user))
            print(str(message.content))
            muted_role = discord.utils.get(message.guild.roles, name="Muted")
            await self.user.add_roles(muted_role)

Let me know if you do still get any errors.

Last edit-

I tried this command for myself outside a cog, and it works perfectly fine :)

@client.event
async def on_message(message):
    if message.author.id == client.user.id:
        return
    elif "test" in message.content:
        await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
        user = message.author
        print(str(user))
        print(str(message.content))
        muted_role = discord.utils.get(message.guild.roles, name="Muted")
        await user.add_roles(muted_role)
    else:
        return
    await client.process_commands(message)

Upvotes: 0

Related Questions