discord.py wait_for 无法在方法中工作
discord.py wait_for not working in a method
在这种情况下我有 2 个单独的文件,其中 1 个用于主文件,另一个是包含函数的文件(不在 Cog 中)。我想让用户响应机器人输出的消息,然后机器人会将该消息发回聊天室。
这里的问题是程序没有注册一个人输入的消息,我尝试使用 self.bot
或 client
而不是仅用于 wait_for
功能的机器人,但这不起作用。
这是我的 main.py
文件的代码
import discord
import asyncio
from discord.ext import commands
from anime import *
from embed import *
bot = commands.Bot(command_prefix="m.")
TOKEN = "INSERT TOKEN HERE"
@bot.event
async def on_ready():
print("Connected to discord")
@bot.command(name="searchanime", help="Seaches through the MAL Database to find an anime. Requires a Title to search")
async def search(ctx, *, query):
await search_anime(ctx, query)
bot.run(TOKEN)
这是我的 anime.py
代码,它是一个单独的文件
import discord
import requests
headers = {
'x-rapidapi-key': "2ed26cb89emsh2b7cb079ec42fd9p15290cjsne485ef05078a",
'x-rapidapi-host': "jikan1.p.rapidapi.com"
}
async def search_anime(ctx, query):
url = "https://jikan1.p.rapidapi.com/search/anime"
querystring = {"q": query, "format": "json"}
response = requests.request("GET", url, headers=headers, params=querystring).json()
response_list = []
for i in range(4):
response_list.append(response["results"][i])
await ctx.send("Message 'm.(desired number)' without the parenthesis to select the anime you are looking for from this list")
for i in range(len(response_list)):
await ctx.send(f"{i+1}) {response_list[i]['title']}")
try:
msg = await bot.wait_for("message")
if msg:
await ctx.send(msg.content)
except asyncio.TimeoutError:
await ctx.send("Cancelling due to timeout")
bot 根本没有在您的 anime.py
中定义,您可以将其作为参数传递给函数。
await search_anime(bot, ctx, query)
async def search_anime(bot, ctx, query)
#other stuff
在这种情况下我有 2 个单独的文件,其中 1 个用于主文件,另一个是包含函数的文件(不在 Cog 中)。我想让用户响应机器人输出的消息,然后机器人会将该消息发回聊天室。
这里的问题是程序没有注册一个人输入的消息,我尝试使用 self.bot
或 client
而不是仅用于 wait_for
功能的机器人,但这不起作用。
这是我的 main.py
文件的代码
import discord
import asyncio
from discord.ext import commands
from anime import *
from embed import *
bot = commands.Bot(command_prefix="m.")
TOKEN = "INSERT TOKEN HERE"
@bot.event
async def on_ready():
print("Connected to discord")
@bot.command(name="searchanime", help="Seaches through the MAL Database to find an anime. Requires a Title to search")
async def search(ctx, *, query):
await search_anime(ctx, query)
bot.run(TOKEN)
这是我的 anime.py
代码,它是一个单独的文件
import discord
import requests
headers = {
'x-rapidapi-key': "2ed26cb89emsh2b7cb079ec42fd9p15290cjsne485ef05078a",
'x-rapidapi-host': "jikan1.p.rapidapi.com"
}
async def search_anime(ctx, query):
url = "https://jikan1.p.rapidapi.com/search/anime"
querystring = {"q": query, "format": "json"}
response = requests.request("GET", url, headers=headers, params=querystring).json()
response_list = []
for i in range(4):
response_list.append(response["results"][i])
await ctx.send("Message 'm.(desired number)' without the parenthesis to select the anime you are looking for from this list")
for i in range(len(response_list)):
await ctx.send(f"{i+1}) {response_list[i]['title']}")
try:
msg = await bot.wait_for("message")
if msg:
await ctx.send(msg.content)
except asyncio.TimeoutError:
await ctx.send("Cancelling due to timeout")
bot 根本没有在您的 anime.py
中定义,您可以将其作为参数传递给函数。
await search_anime(bot, ctx, query)
async def search_anime(bot, ctx, query)
#other stuff