【发布时间】:2021-06-13 04:23:27
【问题描述】:
我的 discord bot 有问题,每当我使用 apraw 运行下面的代码以获取 subreddit 上最近提交的标题时,bot 不再在线显示,但仍返回 CMD 中的标题:
- 当我执行此操作时,Bot 不在线,但仍然要求 subreddit 名称并在 CMD 中打印 subreddit 的新帖子的标题:
import asyncio
import apraw
from discord.ext import commands
bot = commands.Bot(command_prefix = '?')
@bot.event
async def on_ready():
print('Bot is ready')
await bot.change_presence(status=discord.Status.online, activity=discord.Game('?'))
@bot.command()
async def online (ctx):
await ctx.send('Bot is online !')
reddit = apraw.Reddit(client_id = "CLIENT_ID",
client_secret = "CLIENT_SECRET",
password = "PASSWORD",
user_agent = "pythonpraw",
username = "LittleBigOwl")
@bot.event
async def scan_posts():
xsub = str(input('Enter subreddit name : '))
subreddit = await reddit.subreddit(xsub)
async for submission in subreddit.new.stream():
print(submission.title)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(scan_posts())
bot.run('TOKEN')
- 但当我执行此操作时在线但显然不要求子名称...:
import asyncio
import apraw
from discord.ext import commands
bot = commands.Bot(command_prefix = '?')
@bot.event
async def on_ready():
print('Bot is ready')
await bot.change_presence(status=discord.Status.online, activity=discord.Game('?'))
@bot.command()
async def online (ctx):
await ctx.send('Bot is online !')
bot.run('TOKEN')
所以 reddit 是这里的问题。但是,为了让我的机器人在线出现,同时仍然能够在给定的 subreddit 上检索新提交的标题,我应该改变什么?代码没有返回任何错误:/
【问题讨论】:
-
你究竟是如何触发
scan_posts的,因为那不是不和谐事件 -
@Ceres idk,scan_posts 自行触发,当我尝试删除
@bot.event时,我仍然遇到同样的问题 -
这不应该发生,当你启动机器人时它会打印
Enter subreddit name :吗?@bot.event装饰器用于修改脚本在发生不和谐事件(例如on_message和on_ready)时所做的事情。你到底想让scan_posts做什么?你为什么用@bot.events装饰它。
标签: discord.py offline reddit praw