【问题标题】:Discord.py 'NoneType' object has no attribute 'send'Discord.py 'NoneType' 对象没有属性 'send'
【发布时间】:2021-07-27 04:53:03
【问题描述】:

当我尝试运行命令时,我总是收到以下错误消息: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 等待 coro(*args, **kwargs) 文件“K:\Discord BOT\PythonX\cogs\welcome.py”,第 30 行,在 on_member_join await channel.send(embed=embed) AttributeError: 'NoneType' object has no attribute 'send'

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    错误意味着变量channel 是NoneType 或None。这可能意味着没有 ID 为get_channel 的频道。您可能想运行函数 get_channel 而不是将其作为 id。

    【讨论】:

      【解决方案2】:
      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 存在)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-15
        • 2021-02-17
        • 2023-01-23
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 2021-05-06
        • 2021-04-13
        相关资源
        最近更新 更多