【问题标题】:How to send messages that are sniped to another channel? (discord.py)如何将被狙击的消息发送到另一个频道? (discord.py)
【发布时间】:2021-07-31 11:38:06
【问题描述】:

每当删除一条消息时,我都希望将该消息发送到另一个通道,类似于日志。基本上,每当在某个频道中删除消息时,我都希望将被剪切/删除的消息发送到另一个频道。

示例:如果一条消息在频道 X 中被删除,我希望消息内容转到频道 Y。

代码

import discord
from discord.ext import commands
from tokens import token, CHANNEL_ID

client = commands.Bot(command_prefix='!')
client.sniped_message = None

@client.event
async def on_ready():
    print("Your bot is ready.")

@client.event
async def on_message(message):
    
    if message.channel.id == CHANNEL_ID and message.author != client.user:
        print(f'Fetched message: {message}')
        client.sniped_message = message

@client.command()
async def snipe(ctx):
    
    if ctx.channel.id != CHANNEL_ID:
        return

    if client.sniped_message is None:
        await ctx.channel.send("Couldn't find a message to fetch!")
        return

    message = client.sniped_message

    embed = discord.Embed(
        description=message.content,
        color=discord.Color.purple(),
        timestamp=message.created_at
    )
    embed.set_author(
        name=f"{message.author.name}#{message.author.discriminator}",
        icon_url=message.author.avatar_url
    )
    embed.set_footer(text=f"Message sent in: #{message.channel.name}")

    await ctx.channel.send(embed=embed)
    # assume I would be sending the embed to another channel but not sure how to tackle that

client.run(token)

非常感谢您的帮助! PS:我对 Python 很陌生,和 JS 很不一样……

【问题讨论】:

    标签: python discord message channels


    【解决方案1】:

    先像这样获取 Channel Y:

    channely = client.get_channel(channel_id)
    

    那你就可以了

    await channely.send(client.sniped_message)
    

    【讨论】:

    • 欣赏!这也将落在此处 `await ctx.channel.send(embed=embed)`。
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2018-11-24
    • 2021-02-06
    相关资源
    最近更新 更多