首页 > 解决方案 > Discord py send message to channel

问题描述

I am trying to use discord.py library to send message from one channel to another. Idea - channel_1 user has no rights to read and send messages in channel_2. I tried to write bot which should send these messages - for example, user writes !send "channel2" "hello" and bot send this message to channel 2. But I have got an error tryong to do this

    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)

Error I get - TypeError: send() takes from 1 to 2 positional arguments but 3 were given

标签: pythonbotsdiscord

解决方案


这不是 discord.py-rewrite,对吧?所以只需使用bot.get_channel()并发送消息bot.send_message()链接到文档
(顺便说一句,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)

推荐阅读