如果他们有特定角色,我正在尝试向不和谐频道中的每个人发送私人消息

时间:2021-01-13 19:38:17

标签: python discord discord.py

你好,我找到了这个:

import discord
from discord.ext import commands
from discord.utils import get

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.command()
async def ping(ctx):
        await ctx.send("Pong")

@bot.command()
async def message_role(ctx, role: discord.Role, *, message):
    for member in ctx.message.guild.members:
        if role in member.roles:
            await bot.send_message(member, message)



# Eventsd
@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Game(name='I am Nice'))
    print('Der bot {0.user} ist Gestartet'.format(bot))

如果我输入 .message_role NewTest Hi 什么都没有发生
如果我输入 !message_role NewTest Hi 我得到命令未找到错误

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 您定义了客户端和机器人,您只使用一个
  2. 您没有启用 intents.members
intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents

还要确保在 developer portalhere's how

中启用特权意图

无论您在哪里使用 client 将其更改为 bot

@bot.command()
async def message_role(ctx, role: discord.Role, *, message):
    for member in ctx.message.guild.members:
        if role in member.roles:
            await bot.send_message(member, message)

不是bot.send_message而是member.send

await member.send(message)

还有一个更好的编写代码的方法

@bot.command()
async def message_role(ctx, role: discord.Role, *, message):
    for member in role.members:
        await member.send(message)