【问题标题】:How do I send DM to all users in list with discord.py with defined function如何使用具有定义功能的 discord.py 将 DM 发送给列表中的所有用户
【发布时间】:2023-01-17 17:21:42
【问题描述】:

一段时间以来,我一直在为我所在的社区构建一个 discord.py 机器人。目前我正在处理各种“邮件列表”,如果我运行特定命令,机器人会向(当前)硬编码列表中的每个人发送一条消息。

这是我的代码。

#imports
import discord
import json

#takes token form config
with open("./config.json") as config:
  configData = json.load(config)
token = configData["Token"]

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

client = discord.Client(intents=intents)

#login
@client.event
async def on_ready():
    print(f'Logged in as {client.user}')

list = ["12345678910","111213141516171819"]

#defining dm function
def dm_function():
  for user in list:
    user.send("test")


#makes and sends an embed to the channel command was used in and send the dm:s
@client.event
async def on_message(message):
    if message.content.startswith('!testing'):
      #embed stuff
      

#logins to bot with config.json
client.run(token)

dm:s 保持为空,而不是在 dm 中发送任何内容。 (机器人只发送嵌入。)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您需要遍历 ID 列表,获取用户对象并使用 send 方法。

    user_list = ["12345678910","111213141516171819"]
    
    @client.event
    async def on_message(message):
        if message.content.startswith('!testing'):
            for user_id in user_list:
                user = await client.fetch_user(user_id)
                try:
                    await user.send("Here's a DM")
                except discord.Forbidden:
                    # don't have permissions to send them a DM
                    continue
    

    我还重命名了您的list 变量。在现有的 python 类型之后命名变量是不好的做法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2020-10-10
      • 2021-12-24
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多