python - 满足条件时通过不和谐机器人发送消息
问题描述
当 a 不等于 b 时,我想通过机器人发送自动不和谐消息。我还有其他命令在工作,但它们都需要用户通过 discord 输入
from discord.ext import commands
import requests
import time
import threading
client = commands.Bot(command_prefix = '!')
TOKEN = ('my bot token')
def rep():
threading.Timer(10.0, rep).start()
rep.a = ("aaa")
rep.b = ("bbb")
if rep.a == rep.b :
print("a does in fact equal b")
#other stuff
else:
print("a does not equal b")
#send a message through the discord bot
#other stuff
rep()
client.run(TOKEN)
解决方案
discord.py 实际上内置了循环
我认为这应该有所帮助:
from discord.ext import tasks
@client.event
async def on_ready():
your_loop_name.start()
a = "aaa"
b = "bbb"
@tasks.loop(seconds=10)
async def your_loop_name():
global a, b
print(a)
print(b)
if a != b:
# You can put everything here, this is just an example
user = client.get_user(PUT_YOUR_USER_ID_HERE)
await user.send('a is not equal to b')
else:
pass
# ----------------
@client.command()
async def change(ctx, a_b="-", *, value=None):
global a, b
if a_b == "a":
if not value:
return
a = value
elif a_b == "b":
if not value:
return
b = value
else:
await ctx.send(f"Please enter a or b")
循环每 10 秒执行一次,并检查 a 是否不等于 b ( != )
如果您执行命令“change b aaa”,Bot 将停止向您发送它不相等的信息,并将打印“aaa”2x!
希望我能帮上忙,如果您有任何问题,请在 discord 上写信给我:V Enc OnPoint.#2641
推荐阅读
- grafana - Centos8 上的 Grafana:无法使用“临时过滤器”
- python-3.x - 在不同的响应中获得许多结果?Discord.py / SQLite
- css - 网格调整 2 个孩子的内容以匹配最大的一个
- http4s - 如何在客户端断开连接的情况下流式传输 fs2.Queue 而不会丢失项目
- python - 在具有多个 Python 版本的 Windows 上安装 Python 模块
- c - C中的服务器到客户端广播
- azure - Azure 函数主机在 URI 路径无效的情况下提供消息:
- java - 如何使用声明式服务正确扩展 OSGi 服务?
- jenkins - 在詹金斯奴隶中找不到maven
- python - Python DDD模型中的递归关系