Compare commits

...

2 Commits

Author SHA1 Message Date
bf88195901 catch invalid command 2024-11-02 16:14:02 -07:00
c9d312d78f handle bot error 2024-11-02 16:06:37 -07:00

View File

@ -6,6 +6,10 @@ var bot = new Bot({
emitEvents: false,
emitChatEvents: true
});
bot.on("error", error => {
console.error("bot error", error);
});
await bot.login({
identifier: process.env.IDENTIFIER,
password: process.env.PASSWORD
@ -22,7 +26,11 @@ bot.on("message", async message => {
if (message.text.startsWith('/')) {
let respond = text => conversation.sendMessage({text});
let cmd = message.text.split(' ')[0].slice(1).toLowerCase();
let commandList = `/list, /leave, /join, /ping`;
switch(cmd) {
case "ping":
await respond("pong");
return;
case "list":
let {conversations} = await bot.listConversations();
await respond(`${conversations.length} members in group chat: ${conversations.map(c => '@' + c.members.find(m => m.did != bot.profile.did).handle).join(', ')}`);
@ -32,13 +40,13 @@ bot.on("message", async message => {
await conversation.leave();
return;
case "join":
await respond(`Welcome to the group chat!`);
return;
case "ping":
await respond("pong");
await respond(`You joined the group chat`);
return;
case "help":
await respond(`Commands: /list, /leave, /join, /ping`);
await respond(`Commands: ${commandList}`);
return;
default:
await respond(`Only these commands are accepted: ${commandList}`);
return;
}
}