久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <i id='0JdWj'><tr id='0JdWj'><dt id='0JdWj'><q id='0JdWj'><span id='0JdWj'><b id='0JdWj'><form id='0JdWj'><ins id='0JdWj'></ins><ul id='0JdWj'></ul><sub id='0JdWj'></sub></form><legend id='0JdWj'></legend><bdo id='0JdWj'><pre id='0JdWj'><center id='0JdWj'></center></pre></bdo></b><th id='0JdWj'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0JdWj'><tfoot id='0JdWj'></tfoot><dl id='0JdWj'><fieldset id='0JdWj'></fieldset></dl></div>

    <small id='0JdWj'></small><noframes id='0JdWj'>

        <bdo id='0JdWj'></bdo><ul id='0JdWj'></ul>
    1. <legend id='0JdWj'><style id='0JdWj'><dir id='0JdWj'><q id='0JdWj'></q></dir></style></legend>
    2. <tfoot id='0JdWj'></tfoot>

      使用 tweepy 和 discord.py 將推文發(fā)布到特定頻道

      Using tweepy with discord.py to post tweets to a specific channel(使用 tweepy 和 discord.py 將推文發(fā)布到特定頻道)
        <i id='X5E8X'><tr id='X5E8X'><dt id='X5E8X'><q id='X5E8X'><span id='X5E8X'><b id='X5E8X'><form id='X5E8X'><ins id='X5E8X'></ins><ul id='X5E8X'></ul><sub id='X5E8X'></sub></form><legend id='X5E8X'></legend><bdo id='X5E8X'><pre id='X5E8X'><center id='X5E8X'></center></pre></bdo></b><th id='X5E8X'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='X5E8X'><tfoot id='X5E8X'></tfoot><dl id='X5E8X'><fieldset id='X5E8X'></fieldset></dl></div>

        <small id='X5E8X'></small><noframes id='X5E8X'>

              <tbody id='X5E8X'></tbody>

              • <bdo id='X5E8X'></bdo><ul id='X5E8X'></ul>

                <legend id='X5E8X'><style id='X5E8X'><dir id='X5E8X'><q id='X5E8X'></q></dir></style></legend>
                <tfoot id='X5E8X'></tfoot>
              • 本文介紹了使用 tweepy 和 discord.py 將推文發(fā)布到特定頻道的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                因此,我正在嘗試結(jié)合使用教程、文檔和在線示例來自學(xué)一點 Python,以構(gòu)建一個 Discord 機器人,該機器人實現(xiàn)了一個或多個現(xiàn)有機器人的某些功能.其中一個功能是將一組 Twitter 帳戶中的(新)推文發(fā)布到我的 Discord 服務(wù)器上的特定頻道.我發(fā)現(xiàn)了幾段代碼,我拼湊在一起從 Twitter 流中讀取并調(diào)整"了這些代碼.在這里和那里做一些事情來嘗試實現(xiàn)這一點.

                So, I'm trying to teach myself a bit of Python using a combination of tutorials, documentation, and online examples to build a Discord bot that implements some features of one or more existing bots. One of those features is posting (new) tweets from a certain set of Twitter accounts to a specific channel on my Discord server. I've found several bits and pieces of code that I've cobbled together to read from a Twitter stream and "tweaked" a few things here and there to try to accomplish this.

                但是,我一直遇到一個問題,實際上是讓 on_status 代碼正確執(zhí)行.我嘗試了多種方法來使其正常工作,但我嘗試的所有方法都會導(dǎo)致某種錯誤.以下是我一直在測試的最新迭代中的相關(guān)代碼(已編輯):

                However, I keep running into an issue with actually getting the on_status code to execute properly. I've tried a variety of ways to get it working, but everything I've tried results in some sort of error. Below is the pertinent code (redacted) from the most recent iteration that I've been testing:

                import discord
                import tweepy
                
                from discord.ext import commands
                from tweepy import Stream
                from tweepy.streaming import StreamListener
                
                class TweetListener(StreamListener):
                    def on_status(self, status):
                        if status.in_reply_to_status_id is None:
                            TweetText = status.text
                
                            for DGuild in MyBot.guilds:
                                for DChannel in DGuild.text_channels:
                                    if DChannel.name == 'testing':
                                        TwitterEmbed = discord.Embed(title='New Tweet', description='New Tweet from my timeline.', color=0xFF0000)
                                        TwitterEmbed.set_author(name='@TwitterHandle', icon_url=bot.user.default_avatar_url)
                                        DChannel.send(TweetText, embed = TwitterEmbed)
                
                DISCORD_TOKEN = 'dtoken'
                TWITTER_CONSUMER_KEY = 'ckey'
                TWITTER_CONSUMER_SECRET = 'csecret'
                TWITTER_ACCESS_TOKEN = 'atoken'
                TWITTER_ACCESS_SECRET = 'asecret'
                
                MyBot = commands.Bot(command_prefix='!', description='This is a testing bot')
                TwitterAuth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
                TwitterAuth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET)
                TweetAPI = tweepy.API(TwitterAuth)
                NewListener = TweetListener()
                NewStream = tweepy.Stream(auth=TweetAPI.auth, listener=NewListener)
                NewStream.filter(follow=['USER IDS HERE'], is_async=True)
                
                @bot.event
                async def on_ready():
                    print(MyBot.user.name,'has successfully logged in ('+str(MyBot.user.id)+')')
                    print('Ready to respond')
                
                MyBot.run(DISCORD_TOKEN)
                

                當我將推文發(fā)布到我的測試帳戶的時間線時,channel.send() 方法會生成以下消息:

                The channel.send() method generates the following message when I post a tweet to my testing account's timeline:

                RuntimeWarning: coroutine 'Messageable.send' was never awaited
                

                我理解消息 - channel.send() 方法是異步方法,但 on_status() 事件處理程序是同步方法,我不能await channel.send() 里面 on_status() - 但我不知道如何使它工作.我試圖使 on_status() 方法成為異步方法:

                I understand the message - the channel.send() method is an asynchronous method, but the on_status() event handler is a synchronous method and I can't await channel.send() inside on_status() - but I can't figure out how to make it work. I tried to make the on_status() method an asynchronous method:

                    async def on_status(self, status):
                        <same code for checking the tweet and finding the channel>
                        await DChannel.send(TweetText, embed = TwitterEmbed)
                

                但這總是會導(dǎo)致類似的警告:

                but this always resulted in a similar warning:

                RuntimeWarning: coroutine 'TweetListener.on_status' was never awaited
                if self.on_status(status) is False:
                

                我發(fā)現(xiàn)了問題,How do I async on_status from tweepy? 并按照那里有鏈接,但我沒有看到任何讓我知道我的編碼錯誤的東西.

                I found the question, How do I async on_status from tweepy? and followed the links there, but I didn't see anything that clued me in to whatever my coding error is.

                通過額外的研究,我還嘗試使用 asyncio 庫中的一些調(diào)用來進行調(diào)用:

                Through additional research, I also tried using some calls from the asyncio library to make the call:

                    #channel.send(message, embed = TwitterEmbed)
                    #---ASYNCIO TEST---
                    loop = asyncio.get_event_loop()
                    loop.run_until_complete(asyncio.gather(DChannel.send(embed=TwitterEmbed)))
                    loop.close()
                

                不幸的是,當它嘗試執(zhí)行 loop=asyncio.get_event_loop() 時,這會導(dǎo)致未處理的異常,表明 線程 'Thread-6' 中沒有當前事件循環(huán).code>(所以它甚至不會使用 run_until_complete() 方法來查看 that 是否會工作,盡管我目前并不完全樂觀).

                Unfortunately, this results in an unhandled exception when it attempts to execute loop=asyncio.get_event_loop() indicating There is no current event loop in thread 'Thread-6'. (so it won't even get to the run_until_complete() method to see if that will work, although I'm not exactly optimistic at this point).

                我意識到現(xiàn)有的 Discord 機器人(例如 MEE6)已經(jīng)可以完成我在這里嘗試做的事情,但我希望能夠讓這個機器人成為我自己的"機器人.同時自學(xué)一些關(guān)于 Python 的知識.我可能在這里忽略了一些簡單的事情,但是我無法在 API 文檔中找到任何似乎指向我的 tweepydiscord.py在正確的方向上,我的 Google-fu 顯然不是那么強大.到目前為止,我能夠找到的所有示例似乎都已過時,因為它們似乎引用了已棄用的方法和其中一個或兩個庫的舊版本.還有其他事情我仍然需要弄清楚如何去做(例如,讓 @TwitterHandle 正確填充嵌入),但我必須讓它能夠閱讀并推送推文,然后我才能擔(dān)心到達那一點.

                I realize there are existing Discord bots like MEE6 that can already do what I'm trying to do here, but I'd like to be able to make this bot "my own" while teaching myself a little about Python. I'm probably overlooking something simple here, but I've not been able to find anything in the API documentation for either tweepy or discord.py that seems to be pointing me in the correct direction, and my Google-fu is apparently not that strong. All of the examples I've been able to find so far appear to be out-of-date as they seem to refer to deprecated methods and older versions of either or both libraries. There are other things that I still have to figure out how to do as well (e.g., get the @TwitterHandle to correctly populate the embed), but I've got to get it able to read and push the tweet before I can worry about getting to that point.

                Visual Studio 2017 CE

                Visual Studio 2017 CE

                Python 3.6

                discord.py 1.3.3

                discord.py 1.3.3

                tweepy 3.8.0

                tweepy 3.8.0

                所以,為了向自己證明我的 TweetListener 類確實有效,我繼續(xù)在 on_status 方法中注釋掉所有與 Discord 的交互(for Guild in MyBot.guilds 到方法末尾的所有內(nèi)容),然后添加了一個簡單的 print(TweetText).我再次運行代碼,登錄到我為這個項目擁有的測試 Twitter 帳戶,并發(fā)布了更新.檢查控制臺,它正確地輸出了我的推文的 status.text,正如我所期望的那樣.正如我所想,我在這里遇到的唯一問題是嘗試將該文本 send 到 Discord.

                So, just to prove to myself that my TweetListener class is actually working, I went ahead and commented out all of the interaction with Discord in the on_status method (everything from for Guild in MyBot.guilds to the end of the method), then added a simple print(TweetText). I ran the code again, logged in to the testing Twitter account I have for this project, and posted an update. Checking the console, it correctly output the status.text of my tweet, as I expected it to. As I thought, the only issue I'm having here is trying to send that text to Discord.

                我還嘗試在同步模式而不是異步模式下初始化 tweepy.Stream.filter - NewStream.filter(follow=['USER IDS']).這只會導(dǎo)致我的應(yīng)用程序基本上掛起".在啟動時.我意識到這是由于它在代碼中的位置,所以我將 TweetListener 初始化的所有三行都移到了 on_ready 事件方法中.我將 Discord 代碼注釋掉并進行了測試,它再次工作"了.因為它將另一條測試推文的 status.text 打印到控制臺.但是,機器人不會響應(yīng)任何其他內(nèi)容(我假設(shè)仍在等待"流).

                I also tried initializing the tweepy.Stream.filter in synchronous mode instead of asynchronous - NewStream.filter(follow=['USER IDS']). This only resulted in my application basically "hanging" on startup. I realized this was due to its position in the code, so I moved all three lines of the TweetListener initialization to the on_ready event method. I left the Discord code commented out and tested, and it again "worked" in that it printed the status.text of another test tweet to the console. However, the bot won't respond to anything else (still "waiting" on the stream, I assume).

                即便如此,只是為了看看它是否會有所作為,我繼續(xù)取消了 Discord 交互代碼的注釋并再次嘗試.結(jié)果與我最初的結(jié)果相同,只是這次 Discord 機器人不會響應(yīng)頻道中的活動(在其他地方有幾個超級簡單的 @bot.command 聲明代碼),這又一次讓我回到原來的代碼,遇到同樣的問題.

                Even so, just to see if it would make any difference, I went ahead and uncommented the Discord interaction code and tried again. The result was the same as I had initially, only this time the Discord bot won't respond to activity in the channel (there are a couple of super simple @bot.command declarations elsewhere in the code), which once again basically leads me back to my original code with the same issue.

                我確信有更好的方法來編寫整個代碼.正如我上面所說,我現(xiàn)在才開始玩 Python 并且還在學(xué)習(xí)語法規(guī)則等.即便如此,我仍然不確定自己做錯了什么和/或忽略了這使我無法完成我認為相當簡單的任務(wù).

                I'm certain there are better ways to code this entire thing. As I said above, I'm just now starting to play around with Python and still learning syntax rules and such. Even so, I'm still not sure what I'm doing wrong and/or overlooking that's preventing me from achieving what I had thought would be a fairly simple task.

                我回去又找到了一些 asyncio 示例,并在 on_status 中的 Discord 交互中嘗試了另一個迭代:

                I went back and found some more asyncio examples and tried another iteration in the Discord interaction in on_status:

                    loop = asyncio.new_event_loop()
                    asyncio.set_event_loop(loop)
                    result=loop.run_until_complete(DChannel.send(embed=TwitterEmbed))
                

                這一次我得到了一個 不同 未處理的異常,而不是我以前得到的.run_until_complete 行在獲取新推文時發(fā)生錯誤:

                This time I got a different unhandled exception than I've gotten before. The error occurs on the run_until_complete line when it picks up a new tweet:

                Timeout context manager should be used inside a task
                

                我考慮了這個進展,但我顯然還沒有完全到那里,所以回到谷歌,我找到了關(guān)于 asgiref 庫及其 sync_to_async 的信息>async_to_sync 方法,所以我想我會嘗試一下.

                I consider this progress, but I'm obviously still not quite there, so back to Google and I found information on the asgiref library with its sync_to_async and async_to_sync methods, so I figured I'd give that a try.

                    asgiref.sync.async_to_sync(DChannel.send)(embed=TwitterEmbed)
                

                不幸的是,我得到了與 asyncio 版本相同的未處理異常.我覺得我正處于弄清楚這一點的邊緣,但它根本沒有點擊".還沒有.

                Unfortunately, I get the same unhandled exception as with the asyncio version. I feel like I'm right on the edge of figuring this out, but it simply hasn't "clicked" yet.

                好的,所以在尋找有關(guān) Timeout context manager 異常的信息后,我偶然發(fā)現(xiàn)了另一個 SO 問題,這給了我一線希望.RuntimeError: Timeout context manager should be used inside a task 上的這個答案讓我再次回到使用 asyncio 并在使用 asyncio.run_coroutine_threadsafe 方法提供有用的建議之前,對 OP 問題背后的原因進行簡要但描述性的解釋.查看推薦的代碼,這可以幫助我有效地啟動 sync->async 方法通信是有道理的.我實現(xiàn)了建議的更改(為運行機器人的 Thread 對象創(chuàng)建了一個全局變量,添加了一個在該循環(huán)中生成機器人的啟動"方法,然后在 on_status 將它們整合在一起.

                Okay, so after looking for information about the Timeout context manager exception I was getting above, I stumbled across another SO question that has given me a tiny glimmer of hope. This answer on RuntimeError: Timeout context manager should be used inside a task once again takes me back to using asyncio and gives a brief but descriptive explanation of the reason behind the OP's issue before providing a useful suggestion using the asyncio.run_coroutine_threadsafe method. Looking at the recommended code, it made sense that this could help me initiate the sync->async method communication effectively. I implemented the suggested changes (created a global variable for a Thread object on which to run the bot, added a "startup" method that generated the bot in that loop, then changed the Discord interaction in on_status to bring it all together.

                好消息"是我在發(fā)布推文時不再收到 any 錯誤.此外,測試 bot 命令似乎也可以正常工作.壞消息是它仍然沒有將消息發(fā)送到頻道.因為它沒有產(chǎn)生任何錯誤,所以不知道消息在哪里結(jié)束.

                The "good news" is that I'm no longer getting any errors when I post a tweet. Also, testing the bot command seems to work just fine as well. The bad news is that it still didn't send the message to the channel. Since it didn't produce any errors, there's no telling where the message ended up.

                推薦答案

                我遇到了和你一樣的問題,并從谷歌獲得了確切的鏈接并嘗試了所有這些鏈接,但正如你所提到的,沒有任何效果.

                I went through the same problem as you have, and got the exact links from google and tried all of them but as you have mentioned nothing worked.

                所以,經(jīng)過大量嘗試和修改后,我意識到,我可以將主循環(huán)傳遞給 tweepy 偵聽器并使用 run_coroutine_threadsafe 執(zhí)行異步函數(shù).

                So, after lots of trying and tinkering I realised, I could pass the main loop to the tweepy listener and execute async functions with run_coroutine_threadsafe.

                這是我的代碼的要點:

                聽者:

                class EpicListener(tweepy.StreamListener):
                    def __init__(self, discord, loop, *args, **kwargs):
                        super().__init__(*args, **kwargs)
                        self.discord = discord # this is just a function which sends a message to a channel
                        self.loop = loop # this is the loop of discord client
                
                    def on_status(self, status):
                        self.send_message(status._json)
                
                    def send_message(self, msg):
                        # Submit the coroutine to a given loop
                        future = asyncio.run_coroutine_threadsafe(self.discord(msg), self.loop)
                        # Wait for the result with an optional timeout argument
                        future.result()
                

                不和諧的客戶:

                class MyClient(discord.Client):
                    def __init__(self, *args, **kwargs):
                        super().__init__(*args, **kwargs)
                
                    async def on_ready(self):
                        myStream = tweepy.Stream(
                                    auth=api.auth, listener=EpicListener(discord=self.sendtwitter, loop=asyncio.get_event_loop())
                                )
                        myStream.filter(follow=['mohitwr'], is_async=True)
                        print(myStream)
                

                希望這會有所幫助.

                這篇關(guān)于使用 tweepy 和 discord.py 將推文發(fā)布到特定頻道的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機器人?)
                Discord bot isn#39;t responding to commands(Discord 機器人沒有響應(yīng)命令)
                Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關(guān)于我嗎?Discord 機器人的功能?(不和諧.py))
                message.channel.id Discord PY(message.channel.id Discord PY)
                How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機器人?)
                discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)
                <i id='jhUzS'><tr id='jhUzS'><dt id='jhUzS'><q id='jhUzS'><span id='jhUzS'><b id='jhUzS'><form id='jhUzS'><ins id='jhUzS'></ins><ul id='jhUzS'></ul><sub id='jhUzS'></sub></form><legend id='jhUzS'></legend><bdo id='jhUzS'><pre id='jhUzS'><center id='jhUzS'></center></pre></bdo></b><th id='jhUzS'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='jhUzS'><tfoot id='jhUzS'></tfoot><dl id='jhUzS'><fieldset id='jhUzS'></fieldset></dl></div>

                  <small id='jhUzS'></small><noframes id='jhUzS'>

                    <tfoot id='jhUzS'></tfoot>
                      <tbody id='jhUzS'></tbody>
                    • <bdo id='jhUzS'></bdo><ul id='jhUzS'></ul>
                        <legend id='jhUzS'><style id='jhUzS'><dir id='jhUzS'><q id='jhUzS'></q></dir></style></legend>
                          主站蜘蛛池模板: 亚洲精品日韩一区二区电影 | 91伊人| 国产我和子的乱视频网站 | 鸳鸯谱在线观看高清 | 草久久久 | www.久久精品| 视频在线一区 | 亚洲电影一区二区三区 | 麻豆国产一区二区三区四区 | 欧美一区二区视频 | 精品粉嫩超白一线天av | 国内精品视频免费观看 | 国产精品区一区二区三区 | 97人澡人人添人人爽欧美 | 第四色影音先锋 | 欧美在线一区二区三区 | 自拍偷拍精品 | 亚洲一区精品在线 | 亚洲精品无 | 亚洲成人精品一区二区 | 欧美成人a| 天天躁日日躁狠狠的躁天龙影院 | 亚洲国产成人久久综合一区,久久久国产99 | www.99精品 | 国产精品s色| 日韩在线免费视频 | 亚洲精品一区二区 | 国产a级毛片 | 国产欧美日韩一区二区三区在线 | 日本一二三区高清 | 91偷拍精品一区二区三区 | 国产在线永久免费 | 国产精品久久久久久久久久久久久 | av网站免费看 | 久久久久久久一区二区三区 | 欧美大片久久久 | av中文字幕在线 | 亚洲情综合五月天 | 超碰免费在线观看 | 成人激情免费视频 | 成人福利在线视频 |