首页 > 解决方案 > 我正在尝试使用不和谐机器人使用 tweepy 自动发布推文

问题描述

我知道这可能是一个简单的修复,但我遇到了一个问题,即机器人发布消息的方法在自定义客户端类中,而接收推文的方法在 MSL 类中。我非常肯定这是一个简单的解决方案,我只是愚蠢,但我无法让机器人发布推文。

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api = tweepy.API(auth)

load_dotenv('botAttributes.env')
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

client = discord.Client()

class MSL(tweepy.StreamListener):
    def on_status(self, status):
        
        print(status.text)
        
    def on_error(self, status_code):
        if status_code == 420:
            return False
        else:
            print(status_code)

class CustomClient(discord.Client):
    async def on_ready(self):
        print(f'{self.user} has connected to Discord!')
        
        print("the program should start showing tweets")
        StreamListener = MSL()
        MS = tweepy.Stream(auth = api.auth, listener = StreamListener)
        MS.filter(track=['#python'],is_async=True)

client = CustomClient()
client.run(TOKEN)

标签: pythontwitterdiscordtweepy

解决方案


推荐阅读