ユーザーの操作やコマンド入力なしで、X秒ごと(たとえば3秒ごと)にメッセージを自動的に送信するDiscordボットを作成しようとしています。
これは私が持っているコードです:
import discord
from discord.ext import tasks, commands
bot = commands.Bot(command_prefix="!")
@bot.event
async def on_ready():
await bot.wait_until_ready()
print("Logged in as:")
print(bot.user.name)
print("------")
channel = bot.get_channel(IDasInteger)
print("Channel is:")
print(channel) #Prints None
get_price.start()
@tasks.loop(seconds=3)
async def get_price():
await bot.wait_until_ready()
channel = bot.get_channel(IDasInteger)
print("Channel is:")
print(channel) #Prints None
await channel.send('Test')
@get_price.before_loop
async def before ():
print("Before done.")
bot.run('MyTokenHere')
問題は、このコードを実行すると、次のエラーが発生することです。
AttributeError: 'NoneType' object has no attribute 'send'
チャネル変数を出力しようとすると、Noneが返されます。チャンネルIDは正しいです-値を変更せずにDiscordアプリから直接コピーしました。
何かアイデアはありますか?
ありがとうございました
あなたが得てNone
いる理由IDasInteger
は、定義されていないことです(少なくともあなたが提供したコードでは。それがあなたのコードで定義されている場合、それはチャネルを見つけることができず、戻りますNone
。None
属性sendがなく、エラーが発生します。
チャネルIDをサーバー内の有効なチャネルのIDに変更します。
functions channel = bot.get_channel(790279425633758682)
または、IDasInteger
が有効なチャネルIDを返していることを確認してください
注:サーバーへの有効なチャネルIDを使用してコードをテストしたところ、意図したとおりに機能しました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加