首页 > 解决方案 > 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')

标签: pythonpython-3.xdiscord

解决方案


您可以使用 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')

推荐阅读