解決了!下面回答。
編輯:清理這篇文章以保持一切清晰。
這是一個簡單的 GIF 機器人的代碼。重要的是:這個機器人離線作業,當我發送一個!命令時會給出一個 gif。這個機器人與Heroku一起上線,所以這也很好。當機器人在線時,發送命令以接收 GIF 是行不通的。所以代碼有效,但我無法使用在線模式下的命令使其處于作業狀態。
代碼
import random
import discord
import giphy_client
from discord.ext.commands import Bot
from giphy_client.rest import ApiException
discord_token = *hidden for stackoverflow*
giphy_token = *hidden for stackoverflow*
api_instance = giphy_client.DefaultApi()
def search_gifs(query):
try:
return api_instance.gifs_search_get(giphy_token, query, limit=100, rating = 'PG13')
except ApiException as e:
return "Exception when calling DefaultApi->gifs_search_get: %s\n" % e
def gif_response(emotion):
gifs = search_gifs(emotion)
lst = list(gifs.data)
gif = random.choices(lst)
return gif[0].url
class DiscordClient(discord.Client):
async def on_ready(self):
print("Login as")
print(self.user)
print("-------")
async def on_message(self, message):
if message.author != self.user:
if message.content == '!vibe':
await message.channel.send(gif_response('vibing'))
if message.content == '!dragony':
await message.channel.send(gif_response('dragon'))
if message.content == '!gm':
await message.channel.send(gif_response('gm'))
intents = discord.Intents(messages=True, guilds=True, guild_messages=True)
client = DiscordClient(intents=intents)
# client = DiscordClient()
client.run(discord_token)
需求檔案 (requirements.txt)
git https://github.com/Rapptz/discord.py
PyNaCl==1.3.0
dnspython==1.16.0
async-timeout==3.0.1
pandas
giphy_client==1.0.0
檔案
worker: python gif_bot.py
部署時的日志
2022-04-21T11:46:39.000000 00:00 app[api]: Build started by user
2022-04-21T11:47:14.667290 00:00 app[api]: Deploy x by user
2022-04-21T11:47:14.667290 00:00 app[api]: Release v11 created by user
2022-04-21T11:47:15.800842 00:00 heroku[worker.1]: State changed from crashed to starting
2022-04-21T11:47:21.019690 00:00 heroku[worker.1]: Starting process with command `python gif_bot.py`
2022-04-21T11:47:21.640978 00:00 heroku[worker.1]: State changed from starting to up
2022-04-21T11:47:25.392719 00:00 app[worker.1]: Login as
2022-04-21T11:47:25.392731 00:00 app[worker.1]: Gif Bot#8552
2022-04-21T11:47:25.392732 00:00 app[worker.1]: -------
2022-04-21T11:47:29.000000 00:00 app[api]: Build succeeded
uj5u.com熱心網友回復:
@smerkd 幫我解答了!
在意圖中添加“message_content = True”使其作業。
代碼
intents = discord.Intents(messages=True, message_content=True, guilds=True, guild_messages=True)
uj5u.com熱心網友回復:
使用較新版本的不和諧,您需要傳遞您的意圖。本質上是告訴不和諧你將要使用什么事件和一些快取的東西。一種簡單的思考方式就像您打算做什么或權限一樣。
對于您的示例,請嘗試以下操作
intents = discord.Intents.default()
intents.message_content = True
然后修復您的客戶端以包含指定的意圖
client = DiscordClient(intents=intents)
在此處閱讀有關它們的更多資訊:https ://discordpy.readthedocs.io/en/stable/intents.html
注意:如果你打算做更復雜的事情,你將不得不添加更多的意圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/467618.html
上一篇:如何解決discord.py中的以下錯誤:“TypeError__init__()缺少1個必需的僅關鍵字引數:'intents'”
