Inviter of undefined - discord.js

It says no inviter as an error, and when I can't find the inviter, I want it to send a message saying he logged in with the Url or unknown. What should I do?Normal users do not have a problem when they come with an invitation, but they give such an error when they log in with the Url.
Error: (node:244) UnhandledPromiseRejectionWarning: TypeError: Cannot
read property 'in viter' of undefined
at C:\Users\Administrator\Desktop\1951 Moderation + Registrar + Stats\bot.js :276:63
const invites = {};
const wait = require("util").promisify(setTimeout);
client.on("ready", () => {
wait(1000);
client.guilds.cache.forEach(g => {
g.fetchInvites().then(guildInvites => {
invites[g.id] = guildInvites;
});
});
});
client.on("guildMemberAdd", member => {
member.setNickname("• İsim ' Yaş");
member.setNickname("• İsim ' Yaş");
member.roles.add(ayarlar.kayıtsızRol)
member.roles.add(ayarlar.kayıtsızRol)
if (member.user.bot) return;
member.guild.fetchInvites().then(async guildInvites => {
const ei = invites[member.guild.id];
invites[member.guild.id] = guildInvites;
const invite = await guildInvites.find(
i => (ei.get(i.code) == null ? i.uses - 1 : ei.get(i.code).uses) < i.uses
);
const daveteden = member.guild.members.cache.get(invite.inviter.id);
db.add(`davet_${invite.inviter.id}_${member.guild.id}`, +1);
db.set(`bunudavet_${member.id}`, invite.inviter.id);
let davetsayiv2 = await db.fetch(
`davet_${invite.inviter.id}_${member.guild.id}`
);
let davetsayi;
if (!davetsayiv2) davetsayi = 0;
else
davetsayi = await db.fetch(
`davet_${invite.inviter.id}_${member.guild.id}`
);
let date = moment(member.user.createdAt)
const startedAt = Date.parse(date);
var msecs = Math.abs(new Date() - startedAt);
const years = Math.floor(msecs / (1000 * 60 * 60 * 24 * 365));
msecs -= years * 1000 * 60 * 60 * 24 * 365;
const months = Math.floor(msecs / (1000 * 60 * 60 * 24 * 30));
msecs -= months * 1000 * 60 * 60 * 24 * 30;
const weeks = Math.floor(msecs / (1000 * 60 * 60 * 24 * 7));
msecs -= weeks * 1000 * 60 * 60 * 24 * 7;
const days = Math.floor(msecs / (1000 * 60 * 60 * 24));
msecs -= days * 1000 * 60 * 60 * 24;
const hours = Math.floor(msecs / (1000 * 60 * 60));
msecs -= hours * 1000 * 60 * 60;
const mins = Math.floor((msecs / (1000 * 60)));
msecs -= mins * 1000 * 60;
const secs = Math.floor(msecs / 1000);
msecs -= secs * 1000;
var string = "";
if (years > 0) string += `${years} yıl ${months} ay`
else if (months > 0) string += `${months} ay ${weeks > 0 ? weeks+" hafta" : ""}`
else if (weeks > 0) string += `${weeks} hafta ${days > 0 ? days+" gün" : ""}`
else if (days > 0) string += `${days} gün ${hours > 0 ? hours+" saat" : ""}`
else if (hours > 0) string += `${hours} saat ${mins > 0 ? mins+" dakika" : ""}`
else if (mins > 0) string += `${mins} dakika ${secs > 0 ? secs+" saniye" : ""}`
else if (secs > 0) string += `${secs} saniye`
string = string.trim();
let guild = member.client.guilds.cache.get(ayarlar.SunucuID)
let log = guild.channels.cache.get(ayarlar.hosgeldinKanal);
let endAt = member.user.createdAt
let gün = moment(new Date(endAt).toISOString()).format('DD')
let ay = moment(new Date(endAt).toISOString()).format('MM').replace("01", "Ocak").replace("02", "Şubat").replace("03", "Mart").replace("04", "Nisan").replace("05", "Mayıs").replace("06", "Haziran").replace("07", "Temmuz").replace("08", "Ağustos").replace("09", "Eylül").replace("10", "Ekim").replace("11", "Kasım").replace("12", "Aralık")
let yıl = moment(new Date(endAt).toISOString()).format('YYYY')
let saat = moment(new Date(endAt).toISOString()).format('HH:mm')
let kuruluş = `${gün} ${ay} ${yıl} ${saat}`;
log.send(`
> :tada: **Adalet #1951** Sunucumuza hoş geldin ${member} seninle birlikte **${member.guild.memberCount}** üyeye ulaştık!
> ** **
> Hesabın **${kuruluş} (${string})** önce oluşturulmuş.
> ** **
> Sunucu kurallarımız <#896409238274965525> kanalında yazıyor sunucu kurallarına uymazsan cezalandırılırsın!
> ** **
> **Davet eden:** ${daveteden} \`${davetsayi}.\` davetini gerçekleştirdi.`)
})});
client.on("guildMemberRemove", async member => {
let davetçi = await db.fetch(`bunudavet_${member.id}`);
const daveteden = member.guild.members.cache.get(davetçi);
db.add(`davet_${davetçi}_${member.guild.id}`, -1);
})

Related

countdown timer doesn't countdown

<script>
var countDownDate = new Date("Jan 1, 2019 00:00:00").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("launch").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("launch").innerHTML = "EXPIRED"
}
}, 1000);
</script>

Angularjs split last hour in 10 minutes slices

Is there a way I can take the time between now and last hour and split it in 10 minutes slices in AngularJS? Something like below
10:00:00
09:50:00
09:40:00
09:30:00
09:20:00
09:10:00
09:00:00
The start time code is
//time now -1 hour
$scope.startTime = new Date( (new Date) * 1 - 1000 * 3600 * 1 );
And for end time I am using
//time now
$scope.endTime = new Date();
To make it a genuine date I am using this function
Date.prototype.yyyymmddhhmm = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
var hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours();
var min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes();
var sec = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds();
return "".concat(yyyy + '-').concat(mm + '-').concat(dd + ' ').concat(hh + ':').concat(min + ':').concat(sec);
};
What I need the 10 minutes slices is for a Line Chart labels.
Thanks
I have modified the code and as below,
js code as below
var myApp = angular.module('myApp',[])
.controller('MyCtrl', function($scope){
$scope.timeList = [];
$scope.seconds = 0;
var time = formatAMPM(new Date( (new Date) * 1 + 1000 * 0 * 1 ));
$scope.timeList.push(time);
for(i=0;i<=5;i++)
{
$scope.seconds = $scope.seconds + 600;
var time = formatAMPM(new Date( (new Date) * 1 + 1000 * $scope.seconds * 1 ));
$scope.timeList.push(time);
}
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
});
Html Code as below,
<div ng-controller="MyCtrl">
<div ng-repeat="time in timeList">
{{time}}
</div>
</div>
Check this JSFiddle Link: Demo
OutPut is as below:
3:44 pm
3:54 pm
4:04 pm
4:14 pm
4:24 pm
4:34 pm
4:44 pm
Hope this helps you
Thanks
var endTime = new Date();
var startTime = new Date( endTime.getTime() - 1000 * 3600 );
var time = startTime;
while (time < endTime) {
time = new Date(time.getTime() + 1000 * 60 * 10);
// do something with the generated time
}
This doesn't use anything you didn't already use, I'm confused as to what you got stuck on.

Converting Milliseconds to Minutes and Seconds in momentjs

1.from: 2380 //time in milliseconds to minutes and seconds
example:
2380 is 0.2 sec convert to 00:02 (min:sec)
var fromDuration = moment.duration(from, 'milliseconds');
var fromMinutes = Math.ceil(fromDuration.asMinutes());
var fromSeconds = Math.ceil(fromDuration.asSeconds());
My solution is:
const millisecondsToMinutesSeconds = (ms) => {
let duration = moment.duration(ms, 'milliseconds');
let fromMinutes = Math.floor(duration.asMinutes());
let fromSeconds = Math.floor(duration.asSeconds() - fromMinutes * 60);
return Math.floor(duration.asSeconds()) >= 60 ? (fromMinutes<= 9 ? '0'+fromMinutes : fromMinutes) +':'+ (fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds)
: '00:'+(fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds);
};

Fill assoc array with date

I need to fill a assoc array $pay_day with the key = date and a value of 0, from a start date $d1 till an end date $d2.
$d1 = strtotime("October 12 2014");
$d2 = strtotime("February 28 2015");
$startday = date('F j, Y', $d1 + (24 * 60 * 60));
echo "<h2>Startday is " . $startday . "</h2>";
$endday = date('F j, Y', $d2 + (24 * 60 * 60));
echo "<h2>Endday is " . $endday . "</h2>";
$n=0;
$d3=$d1;
while ($d3 <= $d2) {
$d3 = date('F j, Y', ($d1 + ++$n*(24 * 60 * 60)));
$pay_day[$d3] = 0;
}
$d1 = 'August 12 2014';
$d2 = 'February 28 2015';
$period = new \DatePeriod(
\DateTime::createFromFormat('F d Y', $d1),
new \DateInterval('P1D'),
\DateTime::createFromFormat('F d Y', $d2)->modify('+1 day'));
foreach($period as $date) {
$d3 = $date->format('F j, Y');
$pay_day[$d3] = 0;
}
print_r($pay_day);

Optimize matrices operations

I'm optimizing via Genetic Algorithm a machine simulator (of a MultiheadWeigher machine) in order to solve the well known "Setup problem". I based all the code on matricies but with the multiproduct case I think there is still some inefficiency...
Code
function [f] = MHW(position_matrix, HA, HB, HC)
global W_best_tot
global Function
global n_b_global
nComb = size(position_matrix,1);
dim = size(position_matrix,2);
WL = 241;
%SIMULATIONS VARIABLES
alpha = 0.123;
nSim = 3000;
CellUncertainty = 0.5 * 10^(-6);
%// Define Product Hopper allocation
WT_A = 130; WL_A = 129;
WT_B = 30; WL_B = 29;
%HC = 2;
WT_C = 90; WL_C = 89;
%// COMBINATION MATRIX
CombinantionMatrix_A = combn([0 1], HA);
CombinantionMatrix_B = combn([0 1], HB);
CombinantionMatrix_C = combn([0 1], HC);
if HA == 1
CombinantionMatrix_A = CombinantionMatrix_A.';
end
if HB == 1
CombinantionMatrix_B = CombinantionMatrix_B.';
end
if HC == 1
CombinantionMatrix_C = CombinantionMatrix_C.';
end
CombinantionMatrix_A_Transp = CombinantionMatrix_A.';
CombinantionMatrix_B_Transp = CombinantionMatrix_B.';
CombinantionMatrix_C_Transp = CombinantionMatrix_C.';
% OBJECTIVE FUNCTION COST COEFFICIENTS
Cu_A = 0.03; c_p = 0.6; c_f = 734; c_l = 3.2;
Cu_B = 0.09;
Cu_C = 0.04;
[HopperWeight UncertainWeight] = Weight(nComb, dim, alpha, ...
position_matrix, CellUncertainty);
HopperWeight_A = HopperWeight(:,1:HA);
HopperWeight_B = HopperWeight(:,HA+1:HA+HB);
HopperWeight_C = HopperWeight(:,HA+HB+1:HA+HB+HC);
UncertainWeight_A = UncertainWeight(:,1:HA);
UncertainWeight_B = UncertainWeight(:,HA+1:HA+HB);
UncertainWeight_C = UncertainWeight(:,HA+HB+1:HA+HB+HC);
W_best_tot = zeros(nComb, nSim);
W_best_ABC = zeros(nComb, 3, nSim);
for ii=1:nSim
W_A = HopperWeight_A * CombinantionMatrix_A_Transp;
W_B = HopperWeight_B * CombinantionMatrix_B_Transp;
W_C = HopperWeight_C * CombinantionMatrix_C_Transp;
W_Unc_A = UncertainWeight_A * CombinantionMatrix_A_Transp;
W_Unc_B = UncertainWeight_B * CombinantionMatrix_B_Transp;
W_Unc_C = UncertainWeight_C * CombinantionMatrix_C_Transp;
[~,comb_A] = min(abs(W_Unc_A - WT_A),[],2);
[~,comb_B] = min(abs(W_Unc_B - WT_B),[],2);
[~,comb_C] = min(abs(W_Unc_C - WT_C),[],2);
W_best_A = W_A(sub2ind_mod(size(W_A),1:size(W_A,1),comb_A.')).';
W_best_B = W_B(sub2ind_mod(size(W_B),1:size(W_B,1),comb_B.')).';
W_best_C = W_C(sub2ind_mod(size(W_C),1:size(W_C,1),comb_C.')).';
W_best_ABC(:,:,ii) = [W_best_A W_best_B W_best_C];
W_best_tot(:,ii) = sum(W_best_ABC(:,:,ii),2);
[HopperWeight_2_A UncertainWeight_2_A] = Weight(nComb, HA, alpha, ...
position_matrix(:,1:HA), CellUncertainty);
[HopperWeight_2_B UncertainWeight_2_B] = Weight(nComb, HB, alpha, ...
position_matrix(:,HA+1:HA+HB), CellUncertainty);
[HopperWeight_2_C UncertainWeight_2_C] = Weight(nComb, HC, alpha, ...
position_matrix(:,HA+HB+1:HA+HB+HC), CellUncertainty);
idx = CombinantionMatrix_A(comb_A,:)~=0;
HopperWeight_A(idx) = HopperWeight_2_A(idx);
UncertainWeight_A(idx) = UncertainWeight_2_A(idx);
idx = CombinantionMatrix_B(comb_B,:)~=0;
HopperWeight_B(idx) = HopperWeight_2_B(idx);
UncertainWeight_B(idx) = UncertainWeight_2_B(idx);
idx = CombinantionMatrix_C(comb_C,:)~=0;
HopperWeight_C(idx) = HopperWeight_2_C(idx);
UncertainWeight_C(idx) = UncertainWeight_2_C(idx);
clear HopperWeight_2_A HopperWeight_2_B HopperWeight_2_C ...
UncertainWeight_2_A UncertainWeight_2_B UncertainWeight_2_C;
end
n_b = logical(W_best_tot >= WL);
n_bA = logical(reshape(W_best_ABC(:,1,:), nComb, nSim) >= WL_A);
n_bB = logical(reshape(W_best_ABC(:,2,:), nComb, nSim) >= WL_B);
n_bC = logical(reshape(W_best_ABC(:,3,:), nComb, nSim) >= WL_C);
n_b_global = sum(n_b .* n_bA .* n_bB .* n_bC, 2);
GAvg_A = sum(reshape(W_best_ABC(:,1,:), nComb, nSim).*(reshape(...
W_best_ABC(:,1,:), nComb, nSim) > WT_A),2)./sum(reshape(...
W_best_ABC(:,1,:), nComb, nSim) > WT_A,2);
GAvg_B = sum(reshape(W_best_ABC(:,2,:), nComb, nSim).*(reshape(...
W_best_ABC(:,2,:), nComb, nSim) > WT_B),2)./sum(reshape(...
W_best_ABC(:,2,:), nComb, nSim) > WT_B,2);
GAvg_C = sum(reshape(W_best_ABC(:,3,:), nComb, nSim).*(reshape(...
W_best_ABC(:,3,:), nComb, nSim) > WT_C),2)./sum(reshape(...
W_best_ABC(:,3,:), nComb, nSim) > WT_C,2);
f = Cu_A .* GAvg_A + Cu_B .* GAvg_B + Cu_C .* GAvg_C + (nSim./...
n_b_global) .* c_p + (c_f./n_b_global) + ((nSim - n_b_global)./...
n_b_global) .* c_l;
Function = f;
Profiler
This is the Main Profiler and these are: the MHW function and the Weight function. The main problems are in the Weight since it's called 3000times for each GA generation...Above the Weight function code:
Weight Function Code
function [ HopperWeight UncertainWeight ] = Weight( nComb, H, alpha, AllComb, CellUncertainty )
HopperWeight = randn(nComb,H) * alpha;
HopperWeight = (1+HopperWeight) .* AllComb;
idx = HopperWeight<0;
random = randn(sum(idx(:)), 1);
HopperWeight(idx) = random .* ((1+alpha) .* AllComb(idx));
%ADD ERROR
RandomizeUncertainty = randn(nComb,H) * CellUncertainty;
UncertainWeight = abs((1+RandomizeUncertainty) .* HopperWeight);
end
Is there something that I am missing in order to optimize better the Weight function and in general the time consuming part in the MHW function? (If you need the GA launcher in order to try the MHW function use THIS code)
TIA

Resources