client.get_通道(id)在存在的通道上返回“无”

2024-05-13 15:09:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为Discord创建一个bot,每次我尝试使用以下代码将消息发送到指定的频道时,它不会给我任何错误,但会打印“无”,这是我在频道不存在时所期望的。我现在已经用多个公会/服务器和多个文本频道,以及运行相同代码的多台计算机尝试了这一点。在下面的代码中,我将channelID替换为int,int是channelID,将token替换为我的token(字符串)

import discord
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix = 'bday ', intents = intents)
channel = client.get_channel(channelID)

print(channel)

client.run("token")

bot确实具有管理权限,以及两个意向网关


Tags: 代码importclienttoken消息bot错误channel
2条回答

似乎您正在尝试在实际运行bot之前调用bot的功能

尝试在on_ready()回调中添加代码,以确保仅在初始化bot本身之后才尝试获取通道

import discord
from discord.ext import commands

intents = discord.Intents.all()
client = commands.Bot(command_prefix = 'bday ', intents = intents)

@client.event
async def on_ready():
    channel = client.get_channel(channelID)
    print(channel)

client.run("token")

您需要修改

client.get_channel()

client.get_guild(your server's id).get_channel(channel id)

相关问题 更多 >