Refactor examples into a better form factor

This commit is contained in:
Casper Warden
2020-11-23 15:43:27 +00:00
parent 1f3677905b
commit 8ba2cf231c
14 changed files with 672 additions and 460 deletions

96
examples/Groups/Group.ts Normal file
View File

@@ -0,0 +1,96 @@
import { ExampleBot } from '../ExampleBot';
import { UUID } from '../../lib/classes/UUID';
import { GroupNoticeEvent } from '../../lib/events/GroupNoticeEvent';
class Group extends ExampleBot
{
async onConnected()
{
this.bot.clientEvents.onGroupNotice.subscribe(this.onGroupNotice.bind(this));
// Group invite example
// Just omit the role parameter for "everyone" role
//
// bot.clientCommands.group.sendGroupInvite("c6424e05-6e2c-fb03-220b-ca7904d11e04", "d1cd5b71-6209-4595-9bf0-771bf689ce00");
// Advanced group invite example
//
const userToInvite = new UUID('d1cd5b71-6209-4595-9bf0-771bf689ce00');
const groupID = new UUID('4b35083d-b51a-a148-c400-6f1038a5589e');
// Retrieve group roles
const roles = await this.bot.clientCommands.group.getGroupRoles(groupID);
for (const role of roles)
{
if (role.Name === 'Officers')
{
// IMPORTANT: IN PRODUCTION, IT IS HIGHLY RECOMMENDED TO CACHE THIS LIST.
//
try
{
const members = await this.bot.clientCommands.group.getMemberList(groupID);
let found = true;
for (const member of members)
{
if (member.AgentID.toString() === userToInvite.toString())
{
found = true;
}
}
if (found)
{
console.log('User already in group, skipping invite');
}
else
{
this.bot.clientCommands.group.sendGroupInvite(groupID, userToInvite, role.RoleID).then(() =>
{
});
}
}
catch (error)
{
console.error('Error retrieving member list for group invite');
}
}
}
// Get group member list
try
{
const memberList = await this.bot.clientCommands.group.getMemberList(groupID);
console.log(memberList.length + ' members in member list');
}
catch (error)
{
// Probably access denied
console.error(error);
}
// Get group ban list
try
{
const banList = await this.bot.clientCommands.group.getBanList(groupID);
console.log(banList.length + ' members in ban list');
}
catch (error)
{
// Probably access denied
console.error(error);
}
}
async onGroupNotice(event: GroupNoticeEvent)
{
// Get group name
const groupProfile = await this.bot.clientCommands.group.getGroupProfile(event.groupID);
console.log('Group notice from ' + event.fromName + ' (' + event.from + '), from group ' + groupProfile.Name + ' (' + event.groupID + ')');
console.log('Subject: ' + event.subject);
console.log('Message: ' + event.message);
}
}
new Group().run().then(() => {}).catch((err: Error) => { console.error(err) });

View File

@@ -0,0 +1,80 @@
import { ExampleBot } from '../ExampleBot';
import { UUID } from '../../lib/classes/UUID';
import { GroupChatEvent } from '../../lib/events/GroupChatEvent';
class GroupChat extends ExampleBot
{
private pings: {[key: string]: number} = {};
async onConnected()
{
const groupID = new UUID('4b35083d-b51a-a148-c400-6f1038a5589e');
this.bot.clientEvents.onGroupChat.subscribe(this.onGroupChat.bind(this));
// Start a group chat session - equivalent to opening a group chat but not sending a message
await this.bot.clientCommands.comms.startGroupChatSession(groupID, '');
// Send a group message
await this.bot.clientCommands.comms.sendGroupMessage(groupID, 'Test');
const badGuyID = new UUID('1481561a-9113-46f8-9c02-9ac1bf005de7');
await this.bot.clientCommands.comms.moderateGroupChat(groupID, badGuyID, true, true);
// Now, the group mute stuff is often pretty useless because an avatar can just leave the session and re-join.
// Let's enforce it a little better.
const groupChatSubscriber = this.bot.clientEvents.onGroupChatAgentListUpdate.subscribe((event) =>
{
if (event.groupID.equals(groupID) && event.agentID.equals(badGuyID) && event.entered)
{
this.bot.clientCommands.comms.moderateGroupChat(groupID, badGuyID, true, true).then(() =>
{
console.log('Re-enforced mute on ' + badGuyID.toString());
}).catch((err) =>
{
console.error(err);
});
}
});
// Actually, maybe we want to ban the chump.
await this.bot.clientCommands.group.banMembers(groupID, [badGuyID]);
await this.bot.clientCommands.group.ejectFromGroup(groupID, badGuyID);
}
async onGroupChat(event: GroupChatEvent)
{
console.log('Group chat: ' + event.fromName + ': ' + event.message);
if (event.message === '!ping')
{
const ping = UUID.random().toString();
this.pings[ping] = Math.floor(new Date().getTime());
try
{
const memberCount = await this.bot.clientCommands.comms.sendGroupMessage(event.groupID, 'ping ' + ping);
console.log('Group message sent to ' + memberCount + ' members');
}
catch (error)
{
console.error('Failed to send group message:');
console.error(error);
}
}
else if (event.from.toString() === this.bot.agentID().toString())
{
if (event.message.substr(0, 5) === 'ping ')
{
const pingID = event.message.substr(5);
if (this.pings[pingID])
{
const time = (new Date().getTime()) - this.pings[pingID];
delete this.pings[pingID];
await this.bot.clientCommands.comms.sendGroupMessage(event.groupID, 'Chat lag: ' + time + 'ms');
}
}
}
}
}
new GroupChat().run().then(() => {}).catch((err: Error) => { console.error(err) });