Search code examples
pythondiscordbotschatbot

try make discord bot, but why client.send() error?


import discord
import requests
import asyncio
from json import loads

twitch_Client_ID = 'id'
twitch_Client_secret = 'secret'
discord_Token = 'token'
discord_channelID = 'id'
discord_bot_state = 'TEST'
twitchID = 'tid'
msg = 'LIVE! https://www.twitch.tv/' + twitchID

client = discord.Client()


@client.event
async def on_ready():
    print(client.user.id)
    print("ready")

    game = discord.Game(discord_bot_state)
    await client.change_presence(status=discord.Status.online, activity=game)

    channel = client.get_channel(int(discord_channelID))

    oauth_key = requests.post("https://id.twitch.tv/oauth2/token?client_id=" + twitch_Client_ID +
                              "&client_secret=" + twitch_Client_secret + "&grant_type=client_credentials")
    access_token = loads(oauth_key.text)["access_token"]
    token_type = 'Bearer '
    authorization = token_type + access_token
    print(authorization)

    check = False

    while True:
        print("ready on Notification")

        headers = {'client-id': twitch_Client_ID,
                   'Authorization': authorization}
        response_channel = requests.get(
            'https://api.twitch.tv/helix/streams?user_login=' + twitchID, headers=headers)
        print(response_channel.text)

        # try:
        if loads(response_channel.text)['data'][0]['type'] == 'live' and check == False:
            await client.wait_until_ready()
            await channel.send(msg)
            print("Online")
            check = True
        # except:
            # print("Offline")
            #check = False

        await asyncio.sleep(10)

client.run(discord_Token)

here my all code.. and i saw this error

AttributeError: 'NoneType' object has no attribute 'send'

        # try:
        if loads(response_channel.text)['data'][0]['type'] == 'live' and check == False:
            await client.wait_until_ready()
            await channel.send(msg)

I think the problematic code is here. what's wrong here? why always error.....

i try this: https://www.reddit.com/r/discordapp/comments/6ylp7t/python_bot_channel_object_has_no_attribute_send/ and this: AttributeError: 'NoneType' object has no attribute 'send' Discord.py rewrite

but do not work for me !


Solution

  • Looks like your channel is None. According to the docs get_channel(id: int) might return None when channel has not been found. Double check your discord_channelID is correct and that it is an int (not str as in your example).