Skip to content

主动发送消息 #74

Answered by ForteScarlet
MUYI-XIAN asked this question in Q&A
Feb 13, 2023 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

不需要 @Listener 注解,并在类中注入 Application 使用

@Autowired
private Application application;

@SneakyThrows
@Scheduled(cron="0 0 7  * * ? ")
public void goodMorning(){
    BotManagers botManagers = application.getBotManagers();
   // 如果只有一种bot,也可以考虑使用 botManagers.get(0).all().get(0); 
   // 这样的话注意处理异常情况

   // 下面是当存在多个botManager、多个bot的情况时
   for (BotManager<*> manager in botManagers) {
      // 寻找你所需要的botManager,例如MiraiBotManager
     if (manager instanceof MiraiBotManager miraiBotManager) {
          MiraiBot bot = miraiBotManager.getBot(Identifies.ID(BOT的ID));
          // 拿到bot,怎么操作看你心情,比如往某个群发消息
          Group group = bot.getGroup(...); // warn: nullable
          group.send(...);
         break

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@q1285514609
Comment options

@q1285514609
Comment options

@ForteScarlet
Comment options

Answer selected by MUYI-XIAN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
组件:mirai 与mirai组件有关的内容
3 participants