如果成员说出特定单词不和谐py,则自动使成员静音

问题描述

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)

如果用户在发送的邮件中使用省略号,我要使其暂时静音。 ctx.send不起作用,该漫游器不会向该频道发送消息。它说self.client.add_roles不存在。

Muted是我创建的角色,没有任何发送消息权限。

知道为什么吗?一些帮助将不胜感激。我正在使用

AttributeError: 'Message' object has no attribute 'send'这是我得到的错误

[编辑]

    @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)

我看了一下文档并做到了,它可以正常工作,谢谢您的支持:)

解决方法

有很多错误,请查看documentation

我为您纠正了它们-

 @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)

让我知道您是否仍然遇到任何错误。

最后编辑-

我在嵌齿轮外为自己尝试了此命令,并且效果很好:)

@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)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...