Last active
March 5, 2022 05:02
-
-
Save zaneneuschuler/a12d0f6e799d473cfe61a2cac783691a to your computer and use it in GitHub Desktop.
Twitter to discord crossposting bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import peony | |
import asyncio | |
import discord | |
import os | |
def cls(): | |
os.system('cls' if os.name=='nt' else 'clear') | |
twitter_client = peony.PeonyClient( | |
consumer_key=YOUR_KEY_HERE, | |
consumer_secret=YOUR_CONSUMER_KEY_HERE, | |
access_token=YOUR_ACCESS_TOKEN_HERE, | |
access_token_secret=YOUR_ACCESS_SECRET_HERE | |
) | |
channels = [YOUR_CHANNEL_IDS_HERE] | |
class MyClient(discord.Client): | |
async def on_ready(self): | |
print('Connected to discord as {0.name} ({0.id})'.format(self.user)) | |
@twitter_client.event_stream | |
class EventStream(peony.EventStream): | |
def stream_request(self): | |
return self.stream.statuses.filter.post(follow=[YOUR_ACCOUNT_ID_HERE]) | |
@peony.events.on_connect.handler | |
async def on_connect(self, data): | |
user = await twitter_client.user | |
print(f'Connected to twitter as @{user.screen_name}') | |
@peony.events.on_tweet.handler | |
async def on_tweet(self, data): | |
out = "" | |
if hasattr(data, 'retweeted_status'): # if there's a retweet, do this | |
out = f'https://twitter.com/{data.retweeted_status.user.screen_name}/status/{data.retweeted_status.id}' | |
else: # if there's no retweet, we can't use that | |
out = f'https://twitter.com/{data.user.screen_name}/status/{data.id}' | |
# await discord_client.send_message(...) | |
for channel in channels: | |
#set a flag because i'm bad at optimizing | |
sentFlag = False | |
sendChannel = client.get_channel(channel) | |
async for message in sendChannel.history(limit=50): | |
#if the link exists, why repost it | |
if out in message.system_content: | |
sentFlag = True | |
if sentFlag == True: | |
print(f'This was already seen in {sendChannel.name}, skipping...') | |
else: | |
await sendChannel.send(out) | |
print(f'Messaged {out} in channel {sendChannel.name}') | |
client = MyClient() | |
async def main(): | |
cls() | |
await asyncio.gather( | |
twitter_client.arun(), | |
client.start(YOUR_DISCORD_CLIENT_KEY_HERE, bot=False ) | |
) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
peony-twitter[all] | |
git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a small little server (I guess?) that I created to facilitate crossposting links from your twitter account to discord channels you're in, that you'd like.
There are ways that I could optimize it but honestly this works fine so I'm not too worried.
requirements.txt is right there for your easy
pip install -r requirements.txt
needs, even though it's just adding like, two things, although it does require discord.py's rewrite.Also requires Python 3.5+ probably I dunno I'm pretty sure I'm just using the most updated python.