Python _message()上的Discord.py,但仅适用于私人邮件

Python _message()上的Discord.py,但仅适用于私人邮件,python,bots,discord,discord.py,Python,Bots,Discord,Discord.py,所以,我在研究一个不和谐机器人。我正在使用on_message()事件,它可以在私人消息和服务器上工作。我想这只工作在私人信息,我不知道如何去做这件事。 如果有人能帮助我,那就是拉阿德 import os import discord from discord.ext import commands TOKEN = '' quotedUsers = [] client = commands.Bot(command_prefix = '/') @client.event async def

所以,我在研究一个不和谐机器人。我正在使用on_message()事件,它可以在私人消息和服务器上工作。我想这只工作在私人信息,我不知道如何去做这件事。 如果有人能帮助我,那就是拉阿德

import os
import discord
from discord.ext import commands


TOKEN = ''
quotedUsers = []

client = commands.Bot(command_prefix = '/')

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    await client.change_presence(activity=discord.Game(name='with myself.'))
    
@client.event
async def on_message(message):
    await message.author.send("Recieved: " + message.content)

@client.command()
async def search(ctx, *, question):
    await ctx.author.send("Searching: " + question)
    

client.run(TOKEN)```

当收到DM时,它不会有公会,因此您可以使用如下逻辑:

@client.event
异步def on_消息(消息):
#您需要这个,因为您还使用cmd decorators
等待客户端处理命令(消息)
如果不是message.guild:
等待message.author.send(f“Received:{message.content}”)

参考文献:

  • -它提到“如果适用”,这意味着如果它不在公会,它将返回
    None
    ,而不是DM
  • -Python 3.6+

    • 分组DM中也不存在消息公会,因此您必须检查您发送消息的频道是否为DM。您可以使用用户的
      dm_频道
      属性:

      @client.event
      异步def on_消息(消息):
      如果message.channel.id==message.author.dm_channel.id:#仅dm
      #在这里做事#
      elif not message.guild:#仅限分组dm
      #在这里做事#
      其他:#服务器文本频道
      #在这里做事#
      
      您刚刚发布了您的机器人令牌。你需要尽快重新生成它。@JoshuaNixon Discord已经开始自动收集泄漏的代币,并自动为你重新生成它-你还收到来自Moof的DM是的,我太傻了。现在修好了O