python-3.x 如何让discord.py机器人DM特定用户

zvms9eto  于 2023-05-30  发布在  Python
关注(0)|答案(4)|浏览(171)

我找了很多遍这个答案,但我没有找到。我想使用一个建议命令,每当有人使用它来建议一个想法时,它会DM我,而且只有我。

pw136qt2

pw136qt21#

您必须使用send_message方法。在此之前,你必须找到哪个User对应于你自己。

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    # can be cached...
    me = await client.get_user_info('MY_SNOWFLAKE_ID')
    await client.send_message(me, "Hello!")
rpppsulh

rpppsulh2#

@client.event
async def on_message(message):
    if message.content.startswith("#whatever you want it to be")
        await client.send_message(message.author, "#The message")

用你想要的单词替换那些带标签的东西。例如:“无论你想成为什么,它都可能是”!#消息可以是“命令是...”。

lyr7nygr

lyr7nygr3#

对于任何新的:

格式:

dmuser = await bot.fetch_user(user id as int)
await dmuser.send("This is a test!")

on_message:

@bot.event
async def on_message(message):
    dmuser = await bot.fetch_user(message.author.id)
    await dmuser.send("This is a test!")

命令示例1:

@client.command()
async def test(ctx):
    dmuser = await bot.fetch_user(ctx.author.id)
    await dmuser.send("This is a test!")

命令示例二:

@client.command()
async def test(ctx, user:discord.Member):
    dmuser = await bot.fetch_user(user.id)
    await dmuser.send("This is a test!")
xmd2e60i

xmd2e60i4#

discord.py v1.0.0+

从v1.0.0开始,不再使用client.send_message发送消息,而是使用abc.Messageablesend(),它实现了以下内容:

  • discord.TextChannel
  • discord.DMChannel
  • discord.GroupChannel
  • discord.User
  • discord.Member
  • commands.Context
示例

使用bot.command()(推荐):

from discord.ext import commands

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

@bot.command()
async def suggest(ctx, *, text: str):
    me = bot.get_user(YOUR_ID)
    await me.send(text)  # or whatever you want to send here

on_message

from discord.ext import commands

bot = commands.Bot()

@bot.event
async def on_message(message: discord.Message):

    if message.content.startswith("!suggest"):
        text = message.content.replace("!suggest", "")
        me = bot.get_user(YOUR_ID)
        await me.send(text)  # or whatever you want to send here

相关问题