Praw & Discord.py:机器人一直在发送相同的模因。我希望机器人在被询问时发送不同的模因
Praw & Discord.py: The bot keep sending the same meme. I want the bot to send different meme whenever it is asked
我正在尝试创建一个 listener
模因。当其中包含 "meme"
时,机器人将响应消息。它必须发送模因。为此,我正在使用 Praw。
这是我写的代码:
@nd.listen()
async def on_message(message):
if message.author == nd.user:
return
def findCoindences(w):
return re.compile(r'\b({0})\b'.format(w)).search
content = message.content
reaction = ""
subreddit = reddit.subreddit("memes")
for submission in subreddit.random():
submission_link = submission.url
if findCoindences('meme')(content.lower()):
await message.reply(submission_link)
await message.add_reaction(f"<{reaction}>")
注意:我已经在一些行上面定义了reddit
。我猜这不是原因。
问题:
每当我在聊天中发送 meme
时,它每次都会发送相同的模因。我希望它有所不同,但不知道我在哪里遇到这个问题。
这是它的样子:
您无需迭代任何内容。 Praw 提供了一种方法,允许您从 subreddit 中随机获取提交内容。
文档:
random()
代码:
@nd.listen()
async def on_message(message):
if message.author == nd.user:
return
def findCoindences(w):
return re.compile(r'\b({0})\b'.format(w)).search
content = message.content
reaction = ""
if findCoindences('meme')(content.lower()):
random_submission = reddit.subreddit('memes').random()
await message.reply(random_submission.url)
await message.add_reaction(f"<{reaction}>")
Note: I have move the random_submission inside the If-statement
because there is no need for you to look up a submission if the message doesn't say "meme". It would just waste rate limit.
我正在尝试创建一个 listener
模因。当其中包含 "meme"
时,机器人将响应消息。它必须发送模因。为此,我正在使用 Praw。
这是我写的代码:
@nd.listen()
async def on_message(message):
if message.author == nd.user:
return
def findCoindences(w):
return re.compile(r'\b({0})\b'.format(w)).search
content = message.content
reaction = ""
subreddit = reddit.subreddit("memes")
for submission in subreddit.random():
submission_link = submission.url
if findCoindences('meme')(content.lower()):
await message.reply(submission_link)
await message.add_reaction(f"<{reaction}>")
注意:我已经在一些行上面定义了reddit
。我猜这不是原因。
问题:
每当我在聊天中发送 meme
时,它每次都会发送相同的模因。我希望它有所不同,但不知道我在哪里遇到这个问题。
这是它的样子:
您无需迭代任何内容。 Praw 提供了一种方法,允许您从 subreddit 中随机获取提交内容。
文档: random()
代码:
@nd.listen()
async def on_message(message):
if message.author == nd.user:
return
def findCoindences(w):
return re.compile(r'\b({0})\b'.format(w)).search
content = message.content
reaction = ""
if findCoindences('meme')(content.lower()):
random_submission = reddit.subreddit('memes').random()
await message.reply(random_submission.url)
await message.add_reaction(f"<{reaction}>")
Note: I have move the random_submission inside the
If-statement
because there is no need for you to look up a submission if the message doesn't say "meme". It would just waste rate limit.