透過 Discord BOT 來發推 0w<
author: takodayounknown
python
2 years ago
2.7 kB
4
Indexable
Never
# tweePy and discord bot II.py API_key ="*************************" APT_secret_key = "*************************" Access_token = "*************************" Access_token_secret = "*************************" # tweepy,發推與顯示推文 import tweepy, time auth = tweepy.OAuthHandler(API_key, APT_secret_key) auth.set_access_token(Access_token, Access_token_secret) api = tweepy.API(auth) def tweet(think = "*************************"): api.update_status(think) print("\nsend it!\n") # tweet() def show_my_last_tweet_link(): self_account = "*************************" self_tweets = api.user_timeline(self_account, count=1) for self_tweet in self_tweets: return f"https://twitter.com/{self_account}/status/{self_tweet.id}" # print(self_tweet.text) # print("\n") # print(f"https://twitter.com/twitter/statuses/{self_tweet.id}") # print(self_tweet.created_at) # print("_____" * 8) # print(show_my_last_tweet_link()) # discord.py,從 discord BOT 收發訊息 # 來源:https://hackmd.io/@kangjw/Discordpy機器人從0到1超詳細教學 token = "*************************" #導入Discord.py import discord #client是我們與Discord連結的橋樑 client = discord.Client() #調用event函式庫 @client.event #當機器人完成啟動時 async def on_ready(): print('目前登入身份:',client.user) @client.event #當有訊息時 async def on_message(message): #排除自己的訊息,避免陷入無限循環 if message.author == client.user: return # #如果以「https://twitter.com」開頭 # if message.content.startswith('https://twitter.com'): # #分割訊息成兩份 # tmp = message.content.split("?t=",2) # #如果分割後串列長度只有1 # if len(tmp) == 1: # await message.channel.send("Unexcept error") # else: # await message.channel.send(tmp[0]) # 發推指令 if message.content.startswith('tweet'): tmp = message.content.split(' ', 1) tweet(tmp[1]) link = show_my_last_tweet_link() # await message.channel.send(link) # print(type(link)) # print(link) # print(show_my_last_tweet_link()) print(f'{tmp[1]}, send!') await message.channel.send(f"{link}") if message.content.startswith('new tweet'): await message.channel.send('news') print(show_my_last_tweet_link()) await message.channel.send(f"{show_my_last_tweet_link()}") client.run(token) #TOKEN在剛剛Discord Developer那邊「BOT」頁面裡面