Skip to content

Commit

Permalink
Merge pull request #8 from nanoeray/feature/voiceStatistics
Browse files Browse the repository at this point in the history
Feature/voice statistics
  • Loading branch information
barbarbar338 authored Jan 26, 2024
2 parents 3af0246 + 65cebad commit d2696e8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 22 deletions.
28 changes: 14 additions & 14 deletions src/commands/rank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Embed, EmbedBuilder, SlashCommandBuilder,AttachmentBuilder } from "discord.js";
import { SlashCommandBuilder,AttachmentBuilder } from "discord.js";
import { CONFIG } from "../config";
import { GuildMemberModel } from "../models/GuildMemberModel";
import { UserModel } from "../models/UserModel";
import { createCanvas, loadImage, registerFont } from "canvas";
import { registerFont } from "canvas";
import { GuildMemberVoiceModel } from "../models/GuildMemberVoiceModel";

registerFont(__dirname + '/../assets/fonts/Poppins-Bold.ttf', { family: 'Poppins Bold' });
registerFont(__dirname + '/../assets/fonts/Poppins-Light.ttf', { family: 'Poppins Light' });
Expand Down Expand Up @@ -45,16 +46,6 @@ const RankCommand: SlashLevel.ICommand = {
"The user you specified has no level data. How would you like to start a chat with him/her?",
});

const ranks = await GuildMemberModel.find({
guildID: interaction.guild!.id,
}).sort({
xp: -1,
});

const index = ranks.findIndex(
(guildMemberData) => guildMemberData.userID === member.id,
);

let userModel = await UserModel.findOne({
userID: guildMemberModel.userID,
});
Expand All @@ -67,10 +58,19 @@ const RankCommand: SlashLevel.ICommand = {
const { xp, level } = guildMemberModel;
const requiredXP = client.utils.calculateRequiredExp(level + 1);


const dateObj = new Date();
const month = dateObj.getUTCMonth() + 1;
const year = dateObj.getUTCFullYear();
const newDate = `${year}/${month}`;
let voiceModel = await GuildMemberVoiceModel.findOne({
guildID: member.guild.id,
userID: member.id,
month: newDate
});
const userTime = !voiceModel ? 0 : voiceModel.time;
const img = await client.utils.createImage(member.user.tag,level,xp,requiredXP,member.displayAvatarURL({
extension: "png",
}))
}),userTime)


const file = new AttachmentBuilder(img);
Expand Down
5 changes: 3 additions & 2 deletions src/events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ const MessageEvent: SlashLevel.IEvent = {
guildID: message.guild.id,
userID: message.author.id,
xp: 0,
messages: 0,
level: 0,
});

guildMemberModel.xp =
guildMemberModel.xp + client.utils.randomInt(5, 25);
guildMemberModel.xp = guildMemberModel.xp + client.utils.randomInt(5, 25);
guildMemberModel.messages = guildMemberModel.messages + 1;
const { level, xp } = guildMemberModel;
let leveled = false;
const currentLevel = Math.floor(0.15 * Math.sqrt(xp + 1));
Expand Down
44 changes: 44 additions & 0 deletions src/events/voiceStateUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { GuildMemberVoiceModel } from "../models/GuildMemberVoiceModel";
import { VoiceState } from "discord.js";

const VoiceStateUpdateEvent: SlashLevel.IEvent = {
name: "voiceStateUpdate",
execute: async (client,oldState:VoiceState, newState:VoiceState) => {
const member = oldState.member || newState.member;
if (!member) return;

if (!newState.channel && oldState.channel) {
const joinedTimestamp = client.voiceUsers.get(member.id);
if (!joinedTimestamp) return;
const totalTime = new Date().getTime() - joinedTimestamp;
const dateObj = new Date();
const month = dateObj.getUTCMonth() + 1;
const year = dateObj.getUTCFullYear();
const newDate = `${year}/${month}`;
let voiceModel = await GuildMemberVoiceModel.findOne({
guildID: member.guild.id,
userID: member.id,
month: newDate
});
if (!voiceModel) {
voiceModel = await GuildMemberVoiceModel.create({
guildID: member.guild.id,
userID: member.user.id,
time: totalTime,
month: newDate
});
} else {
voiceModel.time = voiceModel.time + totalTime;
}
await voiceModel.save();

} else if (!oldState.channel && newState.channel) {

const time = new Date().getTime();
client.voiceUsers.set(member.id, time);
} else {
}
}
};

export default VoiceStateUpdateEvent;
6 changes: 6 additions & 0 deletions src/models/GuildMemberModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IGuildMemberModel extends Document {
userID: string;
level: number;
xp: number;
messages: number;
}

export const GuildMemberSchema = new Schema<IGuildMemberModel>({
Expand All @@ -21,6 +22,11 @@ export const GuildMemberSchema = new Schema<IGuildMemberModel>({
required: true,
default: 0,
},
messages: {
type: Number,
required: true,
default: 0,
},
xp: {
type: Number,
required: true,
Expand Down
35 changes: 35 additions & 0 deletions src/models/GuildMemberVoiceModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Schema, model, Document} from "mongoose";

export interface IGuildMemberVoiceModel extends Document {
guildID: string;
userID: string;
time: number;
month: string;
}

export const GuildMemberVoiceSchema = new Schema<IGuildMemberVoiceModel>({
guildID: {
type: String,
required: true,
},
userID: {
type: String,
required: true,
},
time: {
type: Number,
required: true,
default: 0,
},
month: {
type: String,
required: true,
default: '0',
},
});

export const GuildMemberVoiceModel = model<IGuildMemberVoiceModel>(
"guildMemberVoiceModel",
GuildMemberVoiceSchema,
"GUILD_MEMBER_VOICE_COLLECTION",
);
2 changes: 2 additions & 0 deletions src/struct/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Core extends Client {
public logger = new Logger("[Core]:");
public rest = new REST({ version: "10" }).setToken(CONFIG.TOKEN);
public utils = utils;
public voiceUsers:Map<string, number> = new Map()

constructor() {
super({
Expand All @@ -35,6 +36,7 @@ export class Core extends Client {
"GuildMessages",
"Guilds",
"MessageContent",
"GuildVoiceStates"
],
});
}
Expand Down
16 changes: 10 additions & 6 deletions src/struct/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Canvas, Image, createCanvas, loadImage } from "canvas";
import { createCanvas, loadImage } from "canvas";

export class Utils {

Expand Down Expand Up @@ -87,13 +87,14 @@ export class Utils {
return longNumber < 999 ? `${longNumber}` : (outputNum + short).trim();
};

public createImage = async (tag: any, level: any, xp: any, requiredXP: any, avatar: any) => {
public createImage = async (tag: any, level: any, xp: any, requiredXP: any, avatar: any, userTime: number) => {
const post = {
nickname: tag,
rank: level,
xp: xp,
toLevel: requiredXP,
avatar: avatar
avatar: avatar,
time: userTime
};

const width = 840;
Expand All @@ -120,6 +121,11 @@ export class Utils {
context.textAlign = "left";
context.fillStyle = "#fff";
context.fillText(this.formatNumber(post.xp) + "/" + this.formatNumber(post.toLevel), 680, 145);

context.font = "13pt 'Poppins Light'";
context.textAlign = "left";
context.fillStyle = "#fff";
context.fillText("Voice Chat Time: " + String(Math.ceil(post.time / 1000 / 60) + "mins."), 524, 125);
/** Texts **/

/** Background PINS **/
Expand Down Expand Up @@ -163,8 +169,7 @@ export class Utils {
ctx.closePath();
}

const percentage = (post.xp / post.toLevel) * 506;
const rectWidth = percentage;
const rectWidth = (post.xp / post.toLevel) * 506;
const rectHeight = 28;
const cornerRadius = 15;

Expand Down Expand Up @@ -196,7 +201,6 @@ export class Utils {


const buffer: any = canvas.toBuffer("image/png");
const img = Buffer.from(buffer, 'base64');

return buffer;
}
Expand Down

0 comments on commit d2696e8

Please sign in to comment.