python - 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
解决方案
这不是 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)
推荐阅读
- gitolite - 如何在 gitolite 中应用存储库权限的更改?
- java - 如何使 STS 或 Maven 停止在项目目录中生成 JAR?
- sql - 将 24 小时时间更改为 12 小时时间的最佳方法?
- .htaccess - 子域重定向到 subdomain.domain.com/subdomain
- c - 除了第一个之外,所有压缩字段的 C 结构与压缩结构不同吗?
- python - 熊猫:用缺少分隔符分隔两列
- kubernetes - 塞尔登IO | sklearn_iris 和 sklearn_spacy_text | 不能在 k8s 中工作
- excel - 如何复制最后一行数据的范围并粘贴到它下面的行中
- java - Spring security:拒绝访问处理程序不起作用(xml配置+控制器方法上的预授权注释)
- c# - 无法使用锁在多线程上下文中实现数据解析