前提
Python、Discord Botともに初心者です。
tweepy(3.10.0)とDiscord.py(1.7.3)で、tweepyである特定のアカウントのツイートをリアルタイムで取得してDiscord.pyを用いてDiscordサーバーに送信するというものを作ろうとしています。
リアルタイムでのツイートの取得まではうまく行ったのですが、Discordに送信するところが動きません…
実現したいこと
Tweepyでツイートが取得できるたびにDiscordで送る
該当のソースコード
python
1import tweepy 2import discord 3from discord.ext import tasks 4 5TOKEN = '*******************' # TOKENを貼り付け 6CHANNELID = ******************* # チャンネルIDを貼り付け 7 8client = discord.Client() 9 10consumer_key = "*******************" 11consumer_secret = "*******************" 12access_key = "*******************" 13access_secret = "*******************" 14 15auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 16auth.set_access_token(access_key, access_secret) 17api = tweepy.API(auth) 18 19@client.event 20async def on_ready(): 21 print('起動しました') 22 23#override tweepy.StreamListener to add logic to on_status 24class MyStreamListener(tweepy.StreamListener): 25 26 27 def on_status(self, status): 28 print(status.text) 29 tt = status.text 30 async def sendtext(): 31 await channel_sent.send(tt) 32 channel_sent = client.get_channel(CHANNELID) 33 34 35myStreamListener = MyStreamListener() 36myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener) 37 38myStream.filter(follow=["*******************"]) 39 40client.run(TOKEN)
試したこと
下記のようにして10秒ごとにDiscordにTweepyで取得したものを送信するようにして、どこかで変化するからOK!みたいにしてみたのですが、10秒間隔でずっと同じものを送信するだけのスパムになってしまいました。
python
1import tweepy 2import discord 3from discord.ext import tasks 4 5TOKEN = '*******************' # TOKENを貼り付け 6CHANNELID = ******************* # チャンネルIDを貼り付け 7 8client = discord.Client() 9 10consumer_key = "*******************" 11consumer_secret = "*******************" 12access_key = "*******************" 13access_secret = "*******************" 14 15auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 16auth.set_access_token(access_key, access_secret) 17api = tweepy.API(auth) 18 19@client.event 20async def on_ready(): 21 print('起動しました') 22 23#override tweepy.StreamListener to add logic to on_status 24class MyStreamListener(tweepy.StreamListener): 25 26 def on_status(self, status): 27 print(status.text) 28 tt = status.text 29 @tasks.loop(seconds=10) 30 async def send_message_every_10sec(): 31 await channel_sent.send(tt) 32 @client.event 33 async def on_ready(): 34 global channel_sent 35 channel_sent = client.get_channel(CHANNELID) 36 send_message_every_10sec.start() #定期実行するメソッドの後ろに.start()をつける 37 client.run(TOKEN) 38 39myStreamListener = MyStreamListener() 40myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener) 41 42myStream.filter(follow=["*********************"])
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/22 09:03
2022/08/24 13:24
2022/08/24 14:32
2022/08/24 15:56