主动发送消息 #74
主动发送消息
#74
-
涉及的编程语言Java 涉及的组件库No response 疑问描述请问我该如何在一个定时任务里主动向群里发送消息,以下是我代码
|
Beta Was this translation helpful? Give feedback.
Answered by
ForteScarlet
Feb 14, 2023
Replies: 1 comment 3 replies
-
不需要 @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;
}
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
MUYI-XIAN
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不需要
@Listener
注解,并在类中注入Application
使用