python - Python Discord Bot - 在调用函数时向特定频道发布消息
问题描述
我正在尝试定义一个函数,该函数将在调用该函数时将消息发布到特定的不和谐频道。我不知道该怎么做。这就是我到目前为止所拥有的。有人可以帮忙吗?
编辑:无论其他事件如何,只要调用 output() 函数,我都需要它运行。
import discord
import asyncio
token = 'BOT TOKEN'
channelid = 'CHANNEL ID'
client = discord.Client()
def respond(output):
#this should make a discord bot send a message with a value of output to the channel of channelid
client.run(token)
respond('hello world')
解决方案
您可以使用 bot.get_channel(channelid)。
这只是一个示例,对于我在堆栈溢出中没有格式化的错误格式感到抱歉。:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
channel = bot.get_channel(yourchannelid) #a specific channel
await channel.send('Bot is ready')
推荐阅读
- java - 面临主要活动困难
- c# - Encoding.UTF8.GetBytes with Span - GetByteCount 或 try/catch 哪个对性能更好
- tfs - tfs 的 .tfignore 和 *.vssscc 之间的区别
- javascript - Angular 6 - 画布旋转后触摸不正确
- datepicker - 如何使用引导日期选择器显示月份
- c++ - 具有两个类似 catch 部分的代码的异常处理行为
- c# - 解析具有自定义属性的方法的类并添加到委托列表
- c# - 如何修复与 int[] numArray 相关的此错误?
- sql - 即使当月没有销售,也要在查询中列出状态销售
- spring - 如何在html页面中显示来自外部文件夹的图像