discord.py - 如何在任务中使用 guild.id
问题描述
我正在尝试创建一个循环来检查服务器何时擦除的命令,但我发现你不能ctx.guild.id
在任务中使用无论如何都可以创建命令循环但仍然可以使用公会:
@tasks.loop(seconds=120)
async def pop_status():
for x in collection.find():
if x["_id"] == client.get_guild(id):
wipe = x["pop"]
response = requests.get('https://api.battlemetrics.com/servers/' + wipe)
pass_times = response.json()
Server_wipe = pass_times['data']['attributes']['details']['rust_last_wipe']
print(Server_wipe)
解决方案
有几个选项,您可以guild
在启动任务时在任务中传递参数,也可以在任务中获取它:
@tasks.loop(seconds=120)
async def pop_status(guild):
# you can use the guild argument
# You have to pass it when starting it, example in a command
@bot.command()
async def start(ctx):
pop_status.start(ctx.guild)
让公会进入循环本身
@tasks.loop(seconds=120)
async def pop_status():
guild = bot.get_guild(ID_HERE)
PS:您不应该真正使用该requests
库,它是阻塞的,您应该aiohttp
改用
推荐阅读
- reactjs - Should we install react and react-dom in --save or --save-dev mode?
- fortran - 在 Fortran 中存储具有多维索引的变量
- python - Complex Django permissions
- qt - qmake 语法:+= 与 *=(加与星号)
- extjs - ExtJs 远程(列表)过滤器重置
- c# - Closing the StreamReader
- php - Creating a PHP table generator with input fields
- html - 向弹性项目添加滚动
- php - Wordpress 正在从放置循环的 HTML 标记中删除一些帖子。为什么?
- javascript - NativeScript Angular:Android主线程上发生未捕获的异常