满足条件时通过Discord机器人发送消息

时间:2019-08-22 00:42:13

标签: python discord discord.py

当a不等于b时,我想通过漫游器发送自动的不和谐消息。我还有其他命令可以使用,但是它们都需要用户通过不和谐输入

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)

1 个答案:

答案 0 :(得分:0)

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(!=)

如果执行命令 “更改b aaa” 机器人会停止向您发送不相等的信息,并会打印“ aaa” 2x!

希望我能帮上忙, 如有疑问,请以不和谐的方式写信给我:V Enc OnPoint。#2641