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, emitEvents: false,
emitChatEvents: true emitChatEvents: true
}); });
bot.on("error", error => {
console.error("bot error", error);
});
await bot.login({ await bot.login({
identifier: process.env.IDENTIFIER, identifier: process.env.IDENTIFIER,
password: process.env.PASSWORD password: process.env.PASSWORD
@ -22,7 +26,11 @@ bot.on("message", async message => {
if (message.text.startsWith('/')) { if (message.text.startsWith('/')) {
let respond = text => conversation.sendMessage({text}); let respond = text => conversation.sendMessage({text});
let cmd = message.text.split(' ')[0].slice(1).toLowerCase(); let cmd = message.text.split(' ')[0].slice(1).toLowerCase();
let commandList = `/list, /leave, /join, /ping`;
switch(cmd) { switch(cmd) {
case "ping":
await respond("pong");
return;
case "list": case "list":
let {conversations} = await bot.listConversations(); 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(', ')}`); 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(); await conversation.leave();
return; return;
case "join": case "join":
await respond(`Welcome to the group chat!`); await respond(`You joined the group chat`);
return;
case "ping":
await respond("pong");
return; return;
case "help": case "help":
await respond(`Commands: /list, /leave, /join, /ping`); await respond(`Commands: ${commandList}`);
return;
default:
await respond(`Only these commands are accepted: ${commandList}`);
return; return;
} }
} }