本文整理汇总了TypeScript中discord.js.Client类的典型用法代码示例。如果您正苦于以下问题:TypeScript js.Client类的具体用法?TypeScript js.Client怎么用?TypeScript js.Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了js.Client类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getClient
public async getClient(userId: string | null = null): Promise<DiscordClient> {
if (userId === null) {
return this.botClient;
}
if (this.clients.has(userId)) {
log.verbose("Returning cached user client for", userId);
return this.clients.get(userId) as DiscordClient;
}
const discordIds = await this.store.get_user_discord_ids(userId);
if (discordIds.length === 0) {
return Promise.resolve(this.botClient);
}
// TODO: Select a profile based on preference, not the first one.
const token = await this.store.get_token(discordIds[0]);
const client = new DiscordClient({
fetchAllMembers: true,
messageCacheLifetime: 5,
sync: true,
});
const jsLog = new Log("discord.js-ppt");
client.on("debug", (msg) => { jsLog.verbose(msg); });
client.on("error", (msg) => { jsLog.error(msg); });
client.on("warn", (msg) => { jsLog.warn(msg); });
try {
await client.login(token);
log.verbose("Logged in. Storing ", userId);
this.clients.set(userId, client);
return client;
} catch (err) {
log.warn(`Could not log ${userId} in. Returning bot user for now.`, err);
return this.botClient;
}
}
示例2: createConnection
createConnection(connectionOptions).then(conn => {
try {
console.log("Typeorm connected to database.");
var bot = new Discord.Client();
bot.on('ready', () => readyEvent(bot));
bot.on('presenceUpdate', presenceEvent);
bot.on('message', messageEvent(bot));
bot.login(Configuration.DISCORD_TOKEN);
} catch (ex) {
console.error(ex);
}
}).catch(err => {
示例3: makeDiscordDriver
export function makeDiscordDriver (token: string): Function {
let client = new Client()
client.loginWithToken(token)
function discordDriver (request: Observable<any>, runSA: StreamAdapter): DiscordSource {
let response = request.share()
response.subscribe(x => console.log('fugg reply'), err => console.log(err))
let discordSource = new MainDiscordSource(client, response, runSA)
return discordSource
}
(discordDriver as any).streamAdapter = rxjsAdapter
return discordDriver
}
示例4: constructor
constructor(config){
this.token = config.token;
this.prefix = config.prefix;
this.presence = config.presence;
this.database = new DB();
this.client = new Client(); // init the discord client
this.client.login(this.token); // login to the discord API
this.commandHandler = new CommandHandler();
this.guilds = [];
this.loadCommands();
}
示例5: getDiscordId
public async getDiscordId(token: string): Promise<string> {
const client = new DiscordClient({
fetchAllMembers: false,
messageCacheLifetime: 5,
sync: false,
});
return new Bluebird<string>((resolve, reject) => {
client.on("ready", async () => {
const id = client.user.id;
await client.destroy();
resolve(id);
});
client.login(token).catch(reject);
}).timeout(READY_TIMEOUT).catch((err: Error) => {
log.warn("Could not login as a normal user.", err.message);
throw Error("Could not retrieve ID");
});
}
示例6: rawReactionEmitter
export async function rawReactionEmitter(rawEvent: any, client: Client) {
if(!reactionEvents.hasOwnProperty(rawEvent.t)) {
return
}
const
{ d: data } = rawEvent,
user: User = client.users.get(data.user_id),
channel: TextChannel = client.channels.get(data.channel_id) as TextChannel
if(channel.messages.has(data.message_id)) {
return
}
const
message: Message = await channel.fetchMessage(data.message_id),
emojiKey: string = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name,
reaction = message.reactions.get(emojiKey)
client.emit(reactionEvents[rawEvent.t], reaction, user)
}
示例7: async
return new Bluebird<string>((resolve, reject) => {
client.on("ready", async () => {
const id = client.user.id;
await client.destroy();
resolve(id);
});
client.login(token).catch(reject);
}).timeout(READY_TIMEOUT).catch((err: Error) => {
示例8: start
public start(): void{
// start the bot
this.client.on("ready", () => {
// initialize the guild objects for the bot instance
this.initGuilds();
// set the 'playing' status
this.client.user.setPresence({game: {name: this.presence, type: 0}});
// print startup time
console.log("Bot started @ " + this.getTime());
});
// message in view
this.client.on("message", (msg) => {
// get description
if(msg.content.startsWith(this.prefix + "desc")){
let command = msg.content.substr(1).split(" ").slice(1);
let response = this.commandHandler.getDescription(command);
if(response != undefined) { msg.channel.send(response); }
// get command
} else if(msg.content.startsWith(this.prefix)){
let command = msg.content.substr(1).split(" ");
let args = command.slice(1);
args.db = this.database;
// find the guild object where the ID matches.
let guild = this.guilds.find(g => g.id == msg.guild.id);
if(guild != undefined && guild.checkCooldown(command, 1000)){
let response = this.commandHandler.execCommand(command[0], args);
// make sure response is valid.
if(response != undefined) { msg.channel.send(response); }
}
}
});
// join new guild
this.client.on("guildCreate", (guild) => {
this.database.addGuild(guild);
});
// removed from guild
this.client.on("guildDelete", (guild) => {
this.database.deleteGuild(guild);
});
}
示例9:
client.on('ready', () => {
client.userAgent.url = module.exports.homepage;
client.userAgent.version = module.exports.version;
client.setPlayingGame(config.get('settings.discordStatus') + '');
console.dir(client.servers.map(s => s.name));
const logServer = client.servers.get(config.get('settings.logServer'));
if (logServer) init(logServer);
else console.error('Not admitted to log server');
});
示例10: reevaluateVoiceChannel
/**
* Finds the most populous voice channel and joins it
*/
function reevaluateVoiceChannel(server:Discord.Server):Promise<any> {
if (server.channels.length === 0) return Promise.resolve();
let [biggest, bigCount]:[Discord.VoiceChannel, number] = [null, -1];
for (const test of server.channels.getAll('type', 'voice')) {
const testCount = test.members.length -
(test.members.get(client.user.id) ? 1 : 0);
if (testCount < bigCount || testCount < 1) continue;
[biggest, bigCount] = [test, testCount];
}
// Join biggest, maybe leaving another
if (biggest) return biggest.join();
// Just leave
const conn = client.voiceConnections.get('server', server);
if (conn) return client.leaveVoiceChannel(conn.voiceChannel);
return Promise.resolve();
}