你好,我找到了这个:
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 我得到命令未找到错误
答案 0 :(得分:0)
两件事:
intents.members
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents
还要确保在 developer portal、here'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)