0

我只想通过 python 代码向我的朋友发送 DM。

这是我的代码,但它不起作用。

代码:

import discord

client = discord.Client(token="MY_TOKEN")

async def sendDm():
    user = client.get_user("USER_ID")
    await user.send("Hello there!")
4

2 回答 2

0
  1. 您的机器人现在可能在其缓存中拥有该用户。然后使用fetch_user 代替get_userfetch请求 Discord API 而不是其内部缓存):
async def sendDm():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")
  1. 您可以使用on_readyevent运行它:
@client.event
async def on_ready():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")

复制和粘贴:

import discord

client = discord.Client()

@client.event
async def on_ready():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")

client.run("MY_TOKEN")
于 2021-11-11T16:13:08.030 回答
0

那么您是否尝试使用命令执行此操作?如果是这样的话

@bot.command()
async def dm(ctx, user: discord.User = None, *, value = None):
  if user == ctx.message.author:
    await ctx.send("You can't DM yourself goofy")
  else:
    await ctx.message.delete()
    if user == None:
      await ctx.send(f'**{ctx.message.author},** Please mention somebody to DM.')
    else:
      if value == None:
        await ctx.send(f'**{ctx.message.author},** Please send a message to DM.')
      else:

     `   await user.send(value)

于 2021-11-12T05:11:24.993 回答