【问题标题】:I have started making a bot in discord but the bot is not sending dms to the members of the server我已经开始制作一个不和谐的机器人,但该机器人没有向服务器成员发送 dms
【发布时间】:2021-03-21 20:29:38
【问题描述】:

我一直在运行以下代码,经过各种更改后,即使确保已启用从服务器向用户发送消息的权限,机器人仍然不会向用户返回欢迎 dm。

import os
import discord

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')# gets the token of the bot from the .env file

client = discord.Client()# makes and discord client

@client.event #client on the following event:
async def on_ready():
        print(f'{client.user} has connected to Discord!')# tells us if the bot is connected
        
@client.event
async def on_member_join(member):
    await member.create_dm()# creates an dm channel
    await member.dm_channel.send( # sends the string given as argument to the channel created
        f'Hiiiiiiiiiii {member.name}, welcomeeeeeeeee icyyyyyy here'
    )

client.run(TOKEN)# runs client for the bot token in TOKEN 

并且代码成功返回该机器人已连接到不和谐并且也在服务器中上线。

【问题讨论】:

    标签: python-3.x discord bots discord.py


    【解决方案1】:

    你需要启用dmintents(有很多所以我建议你启用默认intents)和intents.members

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

    不要忘记在developer portal 中启用特权成员意图

    如果没有创建dm_channel,也可以是None,这是一个更好的解决方案

    await member.send('whatever')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2021-01-28
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多