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

问题描述 投票:0回答:1

当 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)
python discord discord.py
1个回答
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!

© www.soinside.com 2019 - 2024. All rights reserved.