【问题标题】:Python Discord Bot - Post a message to a specific channel when a function is calledPython Discord Bot - 在调用函数时向特定频道发布消息
【发布时间】:2021-03-27 16:28:54
【问题描述】:

我正在尝试定义一个函数,该函数将在调用该函数时将消息发布到特定的不和谐频道。我不知道该怎么做。这就是我到目前为止所拥有的。有人可以帮忙吗?

编辑:无论其他事件如何,只要调用 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')

【问题讨论】:

    标签: python python-3.x discord


    【解决方案1】:

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

    【讨论】:

    • 希望对您有所帮助。
    • 我知道 bot.get_channel,问题是它只会在机器人初始化事件上运行。每次调用 respond() 函数时,我都需要它运行。
    猜你喜欢
    • 2023-03-27
    • 2021-01-10
    • 2020-10-01
    • 2021-08-18
    • 2020-10-01
    • 2022-01-05
    • 2018-09-22
    • 2019-01-31
    • 2021-09-30
    相关资源
    最近更新 更多