python - Discord py 向 channel 发送消息

标签 python bots discord

我正在尝试使用discord.py库将消息从一个 channel 发送到另一个 channel 。想法 - channel _1 用户无权读取和发送 channel _2 中的消息。我尝试编写应该发送这些消息的机器人 - 例如,用户编写!发送“channel2”“hello”,机器人将此消息发送到 channel 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 个

最佳答案

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

@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)

关于python - Discord py 向 channel 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333157/

相关文章:

python - 显示来自 2 个独立 JSON 文件的 JSON 数据 Discord PY

discord - 删除特定 channel 中的某些文件类型附件?

javascript - 语法错误: Unexpected Identifier when using discord-webhooks/javascript

python - 在 python 正则表达式中匹配 unicode 表情符号

python - sqlite3.操作错误: unrecognized token: "{"

Python distutils,如何获得将要使用的编译器?

api - getUpdates Telegram bot API 中忽略的偏移量参数

python - 如何修复用于在 HTML 页面中调用变量的 Python 模板标签,例如 {{ my_name }}

javascript - 无法读取未定义和未处理的 promise 拒绝的属性 'catch'

ruby - heroku 上的 API 轮询机器人