0

だから私は praw フレームワークを使って書いた簡単な reddit ボットをセットアップしました。コードは次のとおりです。

import praw
import time
import numpy
import pickle

r = praw.Reddit(user_agent = "Gets the Daily General Thread from subreddit.")
print("Logging in...")
r.login()

words_to_match = ['sdfghm']

cache = []

def run_bot():
    print("Grabbing subreddit...")
    subreddit = r.get_subreddit("test")
    print("Grabbing thread titles...")
    threads = subreddit.get_hot(limit=10)
    for submission in threads:
        thread_title = submission.title.lower()
        isMatch = any(string in thread_title for string in words_to_match)
        if submission.id not in cache and isMatch:
            print("Match found! Thread ID is " + submission.id)
            r.send_message('FlameDraBot', 'DGT has been posted!', 'You are awesome!')
            print("Message sent!")
            cache.append(submission.id)
    print("Comment loop finished. Restarting...")


# Run the script
while True:
    run_bot()
    time.sleep(20)

ユーザーが照会するさまざまな情報のフィールドを変更できるファイル (テキスト ファイルまたは xml、またはその他のもの) を作成したいと考えています。たとえば、次のような行を含むファイルが必要です。

Words to Search for = sdfghm
Subreddit to Search in = text
Send message to = FlameDraBot

情報をフィールドから入力して、行全体ではなく Words to Search for = の後に値を取得するようにします。情報がファイルに入力され、保存された後。スクリプトでファイルから情報を取得し、それを変数に格納して、その変数を次のような適切な関数で使用するようにします。

words_to_match = ['sdfghm']
subreddit = r.get_subreddit("test")
r.send_message('FlameDraBot'....

つまり、基本的にはスクリプトの構成ファイルのようなものです。スクリプトが .txt または別の適切なファイルから入力を受け取り、それをコードに実装できるようにするにはどうすればよいですか?

4

1 に答える 1