【问题标题】:modmail not working AttributeError: 'ClientUser' object has no attribute 'send'modmail 不工作 AttributeError: 'ClientUser' 对象没有属性 'send'
【发布时间】:2021-12-31 05:37:31
【问题描述】:

所以我正在制作一个不和谐的机器人并制作了一个 modmail 系统并得到了这个错误。有人可以帮忙告诉我哪里出错了吗?

错误:

Ignoring exception in on_message
Traceback (most recent call last):
File "C:\\discord bot\venv\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\\discord bot\cogs\modmail.py", line 35, in on_message
msg = await message.author.send(embed=embed)
AttributeError: 'ClientUser' object has no attribute 'send'

我的代码:

import discord
import asyncio
from discord.ext.commands import Cog
client = discord.Client()
sent_users = []
class modmail(Cog):

    def __init__(self, client):
        self.client = client

    @Cog.listener()
    async def on_ready(self):
            print("modmail ready")


    @Cog.listener()
    async def on_message(self,message):
        if message.guild:  # ensure the channel is a DM
            return

        if message.author == client.user:
            return

        if message.author.id in sent_users:  # Ensure the intial message hasn't been sent before
            return

        modmail_channel = discord.utils.get(client.get_all_channels(), name="modmail")

        embed = discord.Embed(color=0x00FFFF)
        embed.set_author(name=f"Olympia Gaming Modmail System",
                         icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
        embed.add_field(name='Report a member:', value=f"React with 1️⃣ if you want to report a member.")
        embed.add_field(name='Report a Staff Member:', value=f"React with 2️⃣ if you want to report a Staff Member.")
        embed.add_field(name='Warn Appeal:', value=f"React with 3️⃣ if you would like to appeal a warning.")
        embed.add_field(name='Question:',
                        value=f"React with 4️⃣ if you have a question about our moderation system or the server rules.")
        embed.set_footer(text="Olympia Gaming | Modmail")
        msg = await message.author.send(embed=embed)
        await msg.add_reaction("1️⃣")
        await msg.add_reaction("2️⃣")
        await msg.add_reaction("3️⃣")
        await msg.add_reaction("4️⃣")

        sent_users.append(message.author.id)  # add this user to the list of sent users

        try:
            def check(reaction, user):
                return user == message.author and str(reaction.emoji) in ["1️⃣", "2️⃣", "3️⃣", "4️⃣"]

            reaction, user = await client.wait_for("reaction_add", timeout=60, check=check)

            if str(reaction.emoji) == "1️⃣":
                embed = discord.Embed(color=0x00FFFF)
                embed.set_author(name=f"Olympia Gaming Modmail System",
                                 icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
                embed.add_field(name='How to Report:',
                                value="Send the ID of the person you are reporting and attach add a screen shot of them breaking a rule (can be ToS or a server rule).")
                embed.set_footer(text="Olympia Gaming | Report a member ")
                await message.author.send(embed=embed)

                message = await client.wait_for("message", timeout=60, check=lambda
                    m: m.channel == message.channel and m.author == message.author)
                embed = discord.Embed(title=f"{message.content}", color=0x00FFFF)
                await modmail_channel.send(embed=embed)


        except asyncio.TimeoutError:
            await message.delete()



def setup(client):
    client.add_cog(modmail(client))

https://cdn.discordapp.com/attachments/926071479706222622/926393218713649172/game_bot_-_Google_Chrome_12_31_2021_2_13_59_PM_LI.jpg

【问题讨论】:

    标签: python discord.py bots


    【解决方案1】:

    您正在尝试向机器人本身发送消息(它不能这样做),这是因为您没有正确地忽略自己:

    # INCORRECT
    if message.author == client.user:
        return
    

    client 此处与您的机器人实例不同,因为它引用了在第 4 行 (client = discord.Client()) 定义的不同实例。相反,您想要做的是通过使用 self 忽略您的实例,因为您正在通过 __init__ 函数传入机器人实例,所以它是:

    if message.author == self.client.user:
        return
    

    您还应该删除第 4 行的 client 实例,因为您可能会对实际的机器人实例 (self.client) 感到困惑

    【讨论】:

      【解决方案2】:

      编辑:这可用于响应同一频道中的消息。

      await message.channel.send(embed=embed)
      

      以下代码仅在message.author 返回User 或Member 时有效。 ClientUser 模型没有 dm_channel 属性或 send() 方法。


      您需要指定要发送消息到用户的DM频道

      https://discordpy.readthedocs.io/en/stable/api.html#discord.User.dm_channel

      await message.author.dm_channel.send(embed=embed)
      

      【讨论】:

      • msg = await message.author.dm_channel.send(embed=embed) AttributeError: 'ClientUser' object has no attribute 'dm_channel' 我收到此错误。同样,当我运行代码时,前半部分运行良好,我收到一条消息,错误发生的部分是在反应部分之后
      • 啊,我看错了。如果你只是想在同一频道回复消息,你可以使用message.channel.send()
      • 我将行 msg= await message.author.send(embed=embed) 更改为 msg= await message.channel.send(embed=embed) 发生的事情是机器人不断向 dm 频道发送垃圾邮件反应消息。所以问题在于它发送反应消息后的某处传输错误发生并且代码停止前进。
      • 如果它向频道发送垃圾邮件,听起来机器人正在响应自己的消息。
      • 如果你看到我发送的第一条消息的图像,它会继续发送它自己。嗯,可能是的,它对自己的消息做出响应,当我添加一个反应时,它再次发送垃圾邮件。
      猜你喜欢
      • 2021-11-04
      • 2021-08-01
      • 1970-01-01
      • 2023-01-23
      • 2021-01-30
      • 2017-12-02
      • 2020-09-15
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多