【问题标题】:Discord py send message to channelDiscord py 向频道发送消息
【发布时间】:2020-04-07 12:45:09
【问题描述】:

我正在尝试使用 discord.py 库将消息从一个频道发送到另一个频道。 Idea - channel_1 用户无权阅读和发送 channel_2 中的消息。我尝试编写应该发送这些消息的机器人 - 例如,用户编写!发送“channel2”“hello”并将此消息发送到通道 2。但我尝试执行此操作时出错

    import os
import random

import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
token = os.getenv('DISCORD_TOKEN')

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

@bot.command(pass_context=True)
async def xsend(ctx, *, message):
    await bot.delete_message(ctx.message)
    await ctx.send(discord.Object(id='652024045339934731'), message)

bot.run(token)

我得到的错误 - TypeError: send() 接受 1 到 2 个位置参数,但给出了 3 个

【问题讨论】:

    标签: python bots discord


    【解决方案1】:

    这不是 discord.py-rewrite,对吧?所以只需使用bot.get_channel() 并通过bot.send_message() 发送消息。 Link to documentation
    (顺便说一句,ctx.send() 将向调用的频道发送消息,如果我知道的话)

    @bot.command(pass_context=True)
    async def xsend(ctx, *, message: str):
        await bot.delete_message(ctx.message)
        channel = bot.get_channel('652024045339934731')
        if channel:
            await bot.send_message(channel, message)
    


    (discord.py-rewrite 的版本)

    @bot.command(pass_context=True)
    async def xsend(ctx, *, message: str):
        await ctx.message.delete()
        channel = bot.get_channel(652024045339934731)
        if channel:
            await channel.send(message)
    

    【讨论】:

    • 谢谢。有用。决定更改一些功能 - 用户向频道发送消息(带有附件和链接)。之后运行机器人并将最后一条消息作为嵌入消息转发到另一个频道,但机器人只发送文本,而不是图片或链接...尝试使用 embed.add_field: pastebin.com/p8TB4D7i 但得到一个错误 - 'list' 对象没有属性“附件”
    • 语法有问题。不是附件e而是附件。