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

我查看了文档并做了这个,它有效,感谢您的支持:)

4

1 回答 1

0

有很多错误,请看文档

我已经为你更正了——

 @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)
于 2020-08-15T08:12:41.423 回答