简体   繁体   中英

Sending message in discord using bot

I was working on discord.py and I got issue with error AttributeError: 'NoneType' object has no attribute 'send'

Here is the code

import discord
from discord.ext import commands

pybot=commands.Bot(command_prefix="#", description="I love it",case_insensitive=True)

log_channel_id=674175630916583445

@pybot.event

async def on_ready():
    print(f"Logged in as{pybot.user}")
    channel = pybot.get_user(log_channel_id)
    await channel.send('👀')

pybot.run(TOKEN, bot=True, reconnect=True)

You want to get a channel but you're using the get_user function. Since the bot can't find a user with the channel's ID, it returns None . Replace

channel = pybot.get_user(log_channel_id)

with

channel = pybot.get_channel(log_channel_id)
@pybot.event
async def on_ready():
    print(f"Logged in as{pybot.user}")
    channel = pybot.get_channel(674175630916583445)
    await channel.send('👀')

You can have more information about get_channel in the official Discord.py docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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