当我尝试运行该命令时,我总是收到此错误消息:Discord.py 'NoneType' object has no attribute 'send'
import os
import json
import discord
from discord.ext import commands
def get_channel(client, message):
with open('./data/welcome.json', 'r') as f:
tchann = json.load(f)
return tchann[str(message.guild.id)]
class welcome(commands.Cog):
def __init__(self, client):
self.client = client
# Events
@commands.Cog.listener()
async def on_ready(self):
print('Welcome script loading.')
@commands.Cog.listener()
async def on_member_join(self, member):
embed=discord.Embed(title="Yoshino",
url="http://animeex.nhely.hu/",
description=f"asd1.",
color=0x29FF94)
channel = self.client.get_channel(id=get_channel)
await channel.send(embed=embed)
@commands.command()
async def welcome(self, ctx, channel):
with open('./data/welcome.json', 'r') as f:
tchann = json.load(f)
tchann[str(ctx.guild.id)] = channel
with open('./data/welcome.json', 'w') as f:
json.dump(tchann, f, indent=4)
await ctx.send(f'Channel set: {channel}')
def setup(client):
client.add_cog(welcome(client))
错误代码:
<块引用>文件 "C:\Users\NexaHn\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", 第 343 行,在 _run_event 中 await coro(*args, **kwargs) File "K:\Discord BOT\PythonX\cogs\welcome.py", line 30, in on_member_join 等待 channel.send(embed=embed) AttributeError: 'NoneType' 对象没有属性 'send'
答案 0 :(得分:1)
def get_channel(client, message):
with open('./data/welcome.json', 'r') as f:
tchann = json.load(f)
return tchann[str(message.guild.id)]
channel = self.client.get_channel(id=get_channel)
get_channel
是一种方法。它期待一个 int
。见:
https://discordpy.readthedocs.io/en/stable/api.html?highlight=wait_for#discord.Client.get_channel
channel = self.client.get_channel(id=123456789)
本来是有效的(假设 ID 123456789
存在)。
答案 1 :(得分:0)
错误意味着变量 channel
是 NoneType 或 None
。这可能意味着没有 ID 为 get_channel
的频道。您可能希望运行函数 get_channel
而不是将其作为 ID。