本文整理汇总了TypeScript中@bhmb/bot.MessageBot类的典型用法代码示例。如果您正苦于以下问题:TypeScript MessageBot类的具体用法?TypeScript MessageBot怎么用?TypeScript MessageBot使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MessageBot类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
MessageBot.registerExtension('bibliofile/analytics', async (ex, world) => {
const ui = ex.bot.getExports('ui') as UIExtensionExports | undefined
if (!ui) return // Not useful in node bots
ex.remove = () => ui.removeTabGroup(TAB_GROUP)
ui.addTabGroup('Analytics', TAB_GROUP)
const statsTab = ui.addTab('Statistics', TAB_GROUP)
const playerTab = ui.addTab('Players', TAB_GROUP)
statsTab.textContent = playerTab.textContent = 'Loading...'
const logs = await world.getLogs(true)
if (!logs.length) {
ui.notify('No world logs, world not online?')
return
}
const results = analyzeLogs(logs)
const totalJoins = getTotalJoins(results.players)
const bounceRate = calcBounceRate(results.players)
const firstEntry = logs[0]
const totalAccounts = Object.keys(results.players).length
const dupAccounts = calcNumberDuplicateAccounts(results)
// Stats tab
statsTab.innerHTML = `<div class="container is-widescreen">
<h3 class="subtitle">Statistics</h3>
<p>
Note: These statistics will only be accurate since
${niceDate(firstEntry.timestamp)}
</p>
<ul>
<li>This server has been joined ${totalJoins} times.
<li>${totalAccounts} accounts have joined.
<li>Bounce rate (lower is better): ${bounceRate}
<li>${dupAccounts} (${(dupAccounts / totalAccounts * 100).toFixed(2)} %)
of players have more than one account.
<li>The server last had 16 players at: ${niceDate(results.last16Time)}
</ul>
<div style="max-width: 90%;"></div><!--Canvas container -->
</div>`
const canvas = statsTab.querySelector('div')!.appendChild(document.createElement('canvas'))
function sortIntoHours(logs: LogEntry[]): number[] {
const hours = Array(24).fill(0)
logs.forEach(({timestamp}) => hours[timestamp.getHours()]++)
return hours
}
const lastWeekLogs = logs.filter(e => e.timestamp.getTime() > Date.now() - 1000 * 604800)
const lastDayLogs = lastWeekLogs.filter(e => e.timestamp.getTime() > Date.now() - 1000 * 86400)
graph(
canvas.getContext('2d')!,
sortIntoHours(logs),
sortIntoHours(lastWeekLogs),
sortIntoHours(lastDayLogs)
)
// Players tab
const playerList: Array<PlayerInfo & { name: string }> = Object.keys(results.players)
.map(name => ({...results.players[name], name}))
.sort((a, b) => b.lastJoin.getTime() - a.lastJoin.getTime())
playerTab.innerHTML = searchHtml
const tbody = playerTab.querySelector('tbody')!
const template = playerTab.querySelector('template')!
const insertPlayer = (player: PlayerInfo & { name: string}) => {
ui.buildTemplate(template, tbody, [
{ selector: '[data-for=name]', text: player.name },
{ selector: '[data-for=last-ip]', text: world.getPlayer(player.name).ip },
{ selector: '[data-for=last-join]', text: niceDate(player.lastJoin) },
{ selector: 'button', 'data-name': player.name },
])
}
playerList.slice(0, Math.min(100, playerList.length))
.forEach(insertPlayer)
playerTab.addEventListener('click', event => {
const target = event.target as HTMLElement
if (target.tagName !== 'BUTTON') return
const name = target.getAttribute('data-name')!
const safeName = stripHTML(name)
const player = world.getPlayer(name)
const minutesOnline = Math.floor(results.players[name].timeOnline / 1000 / 60)
const timeOnline = minutesOnline < 60 ?
`${minutesOnline.toFixed(0)} minutes` :
`${(minutesOnline / 60).toFixed(2)} hours`
const makeList = (arr: string[]): string => '<ul><li>' + arr.map(stripHTML).join('</li><li>') + '</li></ul>'
ui.alert(`
<div class="content">
<strong>Name</strong>: ${safeName}<br>
//.........这里部分代码省略.........
示例2: function
MessageBot.registerExtension('bibliofile/banking', function(ex, world) {
const storage = ex.storage;
// Helpers
const getCurrencyName = () => storage.get(currency_id, 'Server Coin');
// Commands
const listeners = new Map<string, (player: Player, args: string) => void>();
const accounts = new AccountManager(storage, world);
const messages = new MessageManager(storage);
const permissions = new PermissionManager(storage);
const bankers = new BankerManager(storage);
function permissionCheck(player: Player, perm: PermissionValues): boolean {
switch (perm) {
case 'All':
return true;
case 'Owner':
return player.isOwner;
case 'AdminBanker':
return player.isAdmin || bankers.isBanker(player.name);
case 'Banker':
return bankers.isBanker(player.name);
case 'Admin':
return player.isAdmin;
default:
return false;
}
}
listeners.set('check', (player, args) => {
let check = player.name;
if (args && permissionCheck(player, permissions.getPerm('check'))) {
check = args.toLocaleUpperCase();
}
try {
ex.bot.send(messages.getMessage('check'), {
name: check,
amount: accounts.getBalance(check) + '',
currency: getCurrencyName()
});
} catch {
ex.bot.send(messages.getMessage('error_no_account'), {
name: check,
command: 'check'
});
}
});
listeners.set('transfer', (player, args) => {
const parts = args.match(/([1-9]\d*) (.+)/);
if (!parts) {
return;
}
const amount = +parts[1];
const to = parts[2];
const from = player.name;
// Does the player exist?
if (!accounts.accountExists(to)) {
ex.bot.send(messages.getMessage('error_no_account'), {
name: to,
command: 'transfer'
});
return;
}
try {
accounts.transfer(from, to, amount);
} catch (error) {
const message = error.message.includes('max') ? 'error_limit_reached' : 'error_funds';
ex.bot.send(messages.getMessage(message), {
name: from,
currency: getCurrencyName()
});
return;
}
// Let players know it worked
ex.bot.send(messages.getMessage('transfer'), {
FROM: from,
From: from[0] + from.substr(1).toLocaleLowerCase(),
from: from.toLocaleLowerCase(),
TO: to,
To: to[0] + to.substr(1).toLocaleLowerCase(),
to: to.toLocaleLowerCase(),
amount: amount + '',
currency: getCurrencyName(),
});
});
listeners.set('add', (player, args) => {
const parts = args.match(/([1-9]\d*) (.+)/);
if (!parts) return;
if (!permissionCheck(player, permissions.getPerm('add'))) return;
//.........这里部分代码省略.........
示例3: resolve
.filter(input => input.checked)
.map(input => input.value)
if (ids.length < 1) {
ui.notify('Please select at least one world')
return
}
const settings: PushSettings = {
mode: (tab.querySelector('[name=mode]:checked') as HTMLInputElement).value as 'overwrite' | 'append',
}
resolve({ worlds: worlds.filter(({ id }) => ids.includes(id)), settings })
})
})
}
MessageBot.registerExtension('bibliofile/lists', function(ex) {
const ui = ex.bot.getExports('ui') as UIExtensionExports | undefined
if (!ui) return
const listId = 'bibliofile/lists'
ui.addTabGroup('Lists', listId)
ex.remove = () => ui.removeTabGroup(listId)
createUI('adminlist', ui.addTab('Adminlist', listId), ui)
createUI('modlist', ui.addTab('Modlist', listId), ui)
createUI('whitelist', ui.addTab('Whitelist', listId), ui)
createUI('blacklist', ui.addTab('Blacklist', listId), ui)
})
示例4: function
MessageBot.registerExtension('bibliofile/name_triggers', function (ex, world) {
const getMessages = () => ex.storage.get<Message[]>('messages', [])
const getPreferences = () => ex.storage.get<Preferences>('preferences', { exactMatch: false })
function listener(player: Player) {
const { exactMatch } = getPreferences()
for (const { trigger, message } of getMessages()) {
const target = trigger.toLocaleUpperCase();
if (exactMatch && player.name == target) {
ex.bot.send(message, { name: player.name, ip: player.ip });
}
if (!exactMatch && player.name.includes(target)) {
ex.bot.send(message, { name: player.name, ip: player.ip });
}
}
}
world.onJoin.sub(listener);
ex.remove = () => {
world.onJoin.unsub(listener)
}
// Browser only
const ui = ex.bot.getExports('ui') as UIExtensionExports | undefined
if (!ui) return;
const tab = ui.addTab('Name Triggers', 'messages');
tab.innerHTML = html
const exactBox = tab.querySelector('input[type=checkbox]') as HTMLInputElement
const container = tab.querySelector('.messages-container') as HTMLDivElement
if (getPreferences().exactMatch) {
exactBox.checked = true
}
const addMessage = ({ trigger, message }: Message) => {
ui.buildTemplate(tab.querySelector('template')!, container, [
{ selector: '[data-target=trigger]', value: trigger },
{ selector: '[data-target=message]', value: message }
]);
}
// Deleting messages
tab.addEventListener('click', event => {
const target = event.target as HTMLElement
if (!target.matches('.is-danger')) return
findParentWithClass(target, 'box').remove()
save()
})
// Adding messages
tab.querySelector('.is-adding-message')!.addEventListener('click', () => {
addMessage({ trigger: '', message: '' })
})
const save = () => {
const messages: Message[] = [];
Array.from(container.children).forEach(child => {
messages.push({
message: (child.querySelector('[data-target=message]') as HTMLInputElement).value,
trigger: (child.querySelector('[data-target=trigger]') as HTMLInputElement).value
})
})
ex.storage.set('messages', messages)
ex.storage.set('preferences', {
exactMatch: exactBox.checked
})
}
tab.addEventListener('change', save)
ex.remove = (orig => () => {
orig()
ui.removeTab(tab)
})(ex.remove)
getMessages().forEach(addMessage);
});