繁体   English   中英

Discord.py 发送消息

[英]Discord.py send message

我在下面有一个示例代码块,我试图运行它以向特定频道发送消息。 当我运行它时,我收到此错误:AttributeError: 'NoneType' object has no attribute 'send'。 我知道频道 ID 很好。 这很简单,但我知道 discord.py 所以我可能会忽略一些东西

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents(guild_messages=True)
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    await client.get_channel(811074101505425418).send("bot is online")

client.run(TOKEN)

你可以试试这个

Channel=‘#channelID’
Await channel(‘#your message’)

你可以试试这个:

@client.event
async def on_ready():
    # get channel by ID and save it in the variable "channel"
    channel = await client.get_channel(811074101505425418)
    # send your message to the channel
    await channel.send('bot is ready')

我认为您的问题是您在使用通道时定义了通道。 这应该有效:

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents(guild_messages=True)
client = discord.Client(intents=intents)

@client.event
async def on_ready():
#make a channel variable first
    channel = client.get_channel(811074101505425418)
#if you want to use the message later eg. to delete it you can make a variable too
    message = await channel.send('Bot is online!')#only channel.send because the channel 
#is a variable

client.run(TOKEN)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM