我试图向不和谐频道中的每个具有特定角色的人发送私人消息。
我设法找到了这个
from discord.ext import commands
import discord
TOKEN = 'xxx'
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def message_role(ctx, role: discord.Role, *, message):
for member in ctx.message.server.members:
if role in member.roles:
await bot.send_message(member, message)
bot.run(TOKEN)
我使用Client.x,但我不了解Bot。是,当我运行此命令时,收到以下错误消息:
PS D:\discord bots> py .\DMeveryone.py
Ignoring exception in command message_role:
Traceback (most recent call last):
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\bot.py", line 898, in invoke
await ctx.command.invoke(ctx)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 608, in invoke
await self.prepare(ctx)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 573, in prepare
await self._parse_arguments(ctx)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 491, in _parse_arguments
transformed = await self.transform(ctx, param)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 353, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 308, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\core.py", line 254, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ext\commands\converter.py", line 320, in convert
raise NoPrivateMessage()
discord.ext.commands.errors.NoPrivateMessage
这是消息::message_role成员大家好!
如果有人可以向我解释那真棒的问题,非常感谢。
答案 0 :(得分:1)
discord.ext.commands.Bot是命令扩展discord.py的一部分和子类discord.Client。
根据discord.ext.commands.NoPrivateMessage的文档,它是
当操作在私人消息上下文中不起作用时引发。
这是因为你正在试图使用使用的命令的RoleConverter通过DMS的,在没有作用。
获得self.attrib
异常的原因是因为您尝试使用Message.server属性,但是使用的是discord.py的重写分支,其中{{3} }。
Message.server是discord.py(Server is now Guild)的异步分支上Message.channel和Channel.server的快捷属性。相应地,@Patrick Haugh作为属性存在于重写分支上。
您还使用一种方法来发送仅存在于异步分支上的消息。 参见Message.guild。