【问题标题】:How to make my bot forward DMs sent to it to a channel如何让我的机器人将发送给它的 DM 转发到频道
【发布时间】:2021-03-11 17:34:26
【问题描述】:

所以,我有一个机器人可以通过一个非常简单的命令向用户发送 DM。我所做的只是“!DM @user message”,它会向他们发送消息。然而,人们有时会尝试用他们的问题来回复 DM 中的机器人,但机器人对此无能为力,我看不到他们的回复。有没有办法让我这样做,如果用户向机器人发送 DM,它会将他们的消息转发到我服务器中的某个频道(频道 ID:756830076544483339)。

如果可能,请有人告诉我,我必须使用什么代码才能在发送到我的频道 756830076544483339 的所有 DM 上转发它。

这是我当前的代码:

import discord
from discord.ext import commands

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


@bot.event
async def on_ready():
    activity = discord.Game(name="DM's being sent", type=3)
    await bot.change_presence(
        activity=discord.Activity(
            type=discord.ActivityType.watching,
            name="Pokemon Go Discord Server!"))
    print("Bot is ready!")


@bot.command()
async def DM(ctx, user: discord.User, *, message=None):
    message = message or "This Message is sent via DM"
    await user.send(message)
    await ctx.send('DM Sent Succesfully.')

谢谢!

注意:我对 discord.py 编码还是很陌生,所以请你写出我需要添加到脚本中的代码,而不是仅仅告诉我一些代码,因为我经常对此感到困惑那。谢谢!

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

您正在寻找的是on_message 事件

@bot.event
async def on_message(message):
    # Checking if its a dm channel
    if isinstance(message.channel, discord.DMChannel):
        # Getting the channel
        channel = bot.get_channel(some_id)
        await channel.send(f"{message.author} sent:\n```{message.content}```")
    # Processing the message so commands will work
    await bot.process_commands(message)

Reference

【讨论】:

  • 成功了,非常感谢!我的机器人系统现在已经完成了。再次感谢您!
猜你喜欢
  • 2020-06-30
  • 1970-01-01
  • 2021-06-13
  • 2020-03-05
  • 2023-01-17
  • 2020-05-17
  • 2019-01-07
  • 2021-12-23
  • 2019-03-27
相关资源
最近更新 更多