Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何新建或者获取一个bot对象 #4

Closed
wakabaa opened this issue Aug 5, 2021 · 7 comments
Closed

请问如何新建或者获取一个bot对象 #4

wakabaa opened this issue Aug 5, 2021 · 7 comments

Comments

@wakabaa
Copy link

wakabaa commented Aug 5, 2021

如题,现在正在尝试做一个定时发送群消息到指定群的功能,不知道如何实现。

@MisakaTAT
Copy link

MisakaTAT commented Aug 5, 2021

如题,现在正在尝试做一个定时发送群消息到指定群的功能,不知道如何实现。

启动类添加 @EnableScheduling 注解
@Resource
private BotContainer botContainer;

@Scheduled(cron = "0 0 00 * * ?", zone = "Asia/Shanghai")
private void sendMsg()  {
    val bot = botContainer.getBots().get(botId);
    if (bot != null) {
        bot.sendGroupMsg(groupId, "hi", false);
   }
}

@wakabaa
Copy link
Author

wakabaa commented Aug 5, 2021

非常非常感谢 定时发送已经明白了 !! 后续功能的实装请允许我参考https://github.com/MisakaTAT/YuriBot 谢谢!

@wakabaa wakabaa closed this as completed Aug 5, 2021
@wakabaa
Copy link
Author

wakabaa commented Aug 6, 2021

不好意思,还需要再问一下.....
1.【Bot bot = botContainer.getBots().get(Global.bot_selfId);】在执行这段代码的时候,botContainer.getBots()没有取到map,你的代码里是有给他赋值的处理吗?
2.【Global.bot_selfId】这个好像是读取配置文件的作为bot的信息,selfId设为0是因为已经把这个配置文件添加进botContainer了吗?
基础比较差...读代码没有读懂这两个地方...求赐教_(:з」∠)_

@wakabaa wakabaa reopened this Aug 6, 2021
@MisakaTAT
Copy link

不好意思,还需要再问一下.....
1.【Bot bot = botContainer.getBots().get(Global.bot_selfId);】在执行这段代码的时候,botContainer.getBots()没有取到map,你的代码里是有给他赋值的处理吗?
2.【Global.bot_selfId】这个好像是读取配置文件的作为bot的信息,selfId设为0是因为已经把这个配置文件添加进botContainer了吗?
基础比较差...读代码没有读懂这两个地方...求赐教_(:з」∠)_

Global.bot_selfId请替换为你自己机器人的QQ号, long类型

@wakabaa
Copy link
Author

wakabaa commented Aug 6, 2021

这里换成机器人的QQ号就好了 谢谢

@wakabaa wakabaa closed this as completed Aug 6, 2021
@lz1998
Copy link
Collaborator

lz1998 commented Aug 6, 2021

如题,现在正在尝试做一个定时发送群消息到指定群的功能,不知道如何实现。

新建机器人

Go-Mirai-Client 创建一个新的账号,支持多账号登录。

获取已有的机器人

@Component
public class A {
    @Autowired
    BotContainer botContainer;

    @Scheduled(fixedRate = 3000)
    public void test() {
        long botId = 123L;

        Map<Long, Bot> allBots = botContainer.getBots();

        Bot bot = allBots.get(botId);
        if (bot != null) {
            bot.sendPrivateMsg();
        }
    }
}

定时任务

参考 Spring Boot 定时任务

首先,在项目启动类上添加 @EnableScheduling 注解,开启对定时任务的支持

@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {

	public static void main(String[] args) {
		SpringApplication.run(ScheduledApplication.class, args);
	}

}

其次,编写定时任务类和方法,定时任务类通过 Spring IOC 加载,使用 @component 注解,定时方法使用 @scheduled 注解。

@Component
public class ScheduledTask {

    @Scheduled(fixedRate = 3000)
    public void scheduledTask() {
        System.out.println("任务执行时间:" + LocalDateTime.now());
    }

}

@wakabaa
Copy link
Author

wakabaa commented Aug 9, 2021

如题,现在正在尝试做一个定时发送群消息到指定群的功能,不知道如何实现。

新建机器人

Go-Mirai-Client 创建一个新的账号,支持多账号登录。

获取已有的机器人

@Component
public class A {
    @Autowired
    BotContainer botContainer;

    @Scheduled(fixedRate = 3000)
    public void test() {
        long botId = 123L;

        Map<Long, Bot> allBots = botContainer.getBots();

        Bot bot = allBots.get(botId);
        if (bot != null) {
            bot.sendPrivateMsg();
        }
    }
}

定时任务

参考 Spring Boot 定时任务

首先,在项目启动类上添加 @EnableScheduling 注解,开启对定时任务的支持

@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {

	public static void main(String[] args) {
		SpringApplication.run(ScheduledApplication.class, args);
	}

}

其次,编写定时任务类和方法,定时任务类通过 Spring IOC 加载,使用 @component 注解,定时方法使用 @scheduled 注解。

@Component
public class ScheduledTask {

    @Scheduled(fixedRate = 3000)
    public void scheduledTask() {
        System.out.println("任务执行时间:" + LocalDateTime.now());
    }

}

感谢你详细的回答,现在定时任务已经成功跑起来了,谢谢。

@lz1998 lz1998 pinned this issue Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants