将DM发送给特定的Discord用户

时间:2020-08-17 10:29:18

标签: python discord bots discord.py

我是菜鸟,正在尝试创建将DM发送给特定用户的漫游器,但它返回并显示以下错误:TypeError: event() missing 1 required positional argument: 'coro'

import discord
import asyncio
import os
from users import user_list

key = open("ID.txt","r").readline()

bot = discord.Client()

@bot.event()
async def DM(ctx, user: user_list, *, message=None):
    message = "Come show some love and support to our local streamer by giving him a follow here https://www.twitch.tv/streamer"
    await user.send(message)

bot.run(key.strip())

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

给出代码和其他答案,我觉得有必要在这里使用commands framework

给出完整的示例
import discord
from discord.ext import commands

with open("ID.txt", 'r') as f:
    key = f.readline()

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

@bot.command()
async def dm(ctx, user: discord.User, *, message=None):
    message = message or "Come show some love and support to our local streamer by giving him a follow here https://www.twitch.tv/streamer"
    await user.send(message)

bot.run(key.strip())

可以使用!dm <user> [message]将其发送给您提及的任何人dm

答案 1 :(得分:0)

  @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.bot:
            return
        if isinstance(message.channel, discord.DMChannel):    #CHECKS IF MESSAGE IS IN DM
            owner = self.bot.get_user(110877011584503808)
            print(
                f"{message.author.name}#{message.author.discriminator} <{message.author.id}>: {message.content}"
            )
            await owner.send(
                f"{message.author.name}#{message.author.discriminator} <{message.author.id}>: {message.content}"
            )   #SENDS THE MESSAGE SENT TO THE BOT, TO THE OWNER

这是我的代码,用于当某人向我的机器人发送dm时。它将在dm中将该人的姓名和消息发送给我

owner = self.bot.get_user(110877011584503808)#定义要将dm发送到的用户。 **使用您要用来分级用户ID的任何方法,将其更改为变量-如果它不是像我的那样定义的用户ID

然后等待所有者。发送将其发送给您将user.id设置为该人的人。

@bot.command()
async def dm(ctx, user: discord.Member):
    await user.send("Come show some love and support to our local streamer by giving him a follow here https://www.twitch.tv/streamer")