首页 > 解决方案 > 满足条件时通过不和谐机器人发送消息

问题描述

当 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)

标签: pythondiscorddiscord.py

解决方案


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


推荐阅读