首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python DM all命令

Python DM all命令
EN

Stack Overflow用户
提问于 2020-10-01 18:54:22
回答 1查看 742关注 0票数 0

所以,我在为我的服务器制作一个自定义机器人时遇到了一个问题,下面是我的代码:

代码语言:javascript
复制
@bot.command()
@commands.has_role('| Owner')
async def dmall(ctx,desc):
    title = f'message from {ctx.message.author}'
    await ctx.send('Sending messages!')
    for members in bot.get_all_members():
        embed = discord.Embed(title=title, description=desc)
        await members.send(embed=embed)
        print('Sent a message!')
        time.sleep(3)

我得到的错误是:

代码语言:javascript
复制
Ignoring exception in command dmall:
Traceback (most recent call last):
  File "/home/container/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/container/main.py", line 37, in dmall
    await members.send(embed=embed)
  File "/home/container/discord/abc.py", line 864, in send
    channel = await self._get_channel()
  File "/home/container/discord/member.py", line 250, in _get_channel
    ch = await self.create_dm()
  File "/home/container/discord/member.py", line 110, in general
    return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'

我一直在尝试解决这个问题,问题是我没有在我的代码中使用create_dm

EN

回答 1

Stack Overflow用户

发布于 2020-10-01 19:16:20

这个错误可能是由bot.get_all_members()引起的。您可以使用ctx.guild members获取成员。所以你可以这样做:

代码语言:javascript
复制
@bot.command()
@commands.has_role('| Owner')
async def dmall(ctx,desc):
    title = f'message from {ctx.message.author}'
    await ctx.send('Sending messages!')
    for member in ctx.guild.members:
        embed = discord.Embed(title=title, description=desc)
        await member.send(embed=embed)
        print('Sent a message!')
        await asyncio.sleep(3)

我还添加了asyncio.sleep(),因为据我所知,time.sleep()会阻塞所有代码,所以不要忘了导入它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64153901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档