我正在尝试使用discord.py库将消息从一个通道发送到另一个通道。IDEA-Channel_1用户无权读取和发送Channel_2中的消息。我尝试编写应该发送这些消息的bot-例如,用户写入!发送“Channel2”“Hello”,bot将此消息发送到Channel2。但我试着这么做时出错了
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)
错误I Get-TypeError:send()采用1到2个位置参数,但给出了3个
它不是不和谐的。py-rewrite,对吧?所以只需使用bot.get_channel()
并通过bot.send_message()
发送消息。链接到文档
(Btw,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的版本-重写)
@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)