Banning/kicking members

This commit is contained in:
MathMan05 2024-08-21 12:06:37 -05:00
parent dbee2f3628
commit 3ccb7e63e1
5 changed files with 175 additions and 3 deletions

View file

@ -1,6 +1,7 @@
import { User } from "./user.js";
import { Role } from "./role.js";
import { SnowFlake } from "./snowflake.js";
import { Dialog } from "./dialog.js";
class Member {
static already = {};
owner;
@ -161,5 +162,61 @@ class Member {
profileclick(html) {
//to be implemented
}
get name() {
return this.nick || this.user.username;
}
kick() {
let reason = "";
const menu = new Dialog(["vdiv",
["title", "Kick " + this.name + " from " + this.guild.properties.name],
["textbox", "Reason:", "", function (e) {
reason = e.target.value;
}],
["button", "", "submit", () => {
this.kickAPI(reason);
menu.hide();
}]]);
menu.show();
}
kickAPI(reason) {
const headers = structuredClone(this.guild.headers);
headers["x-audit-log-reason"] = reason;
fetch(`${this.info.api}/guilds/${this.guild.id}/members/${this.id}`, {
method: "DELETE",
headers,
});
}
ban() {
let reason = "";
const menu = new Dialog(["vdiv",
["title", "Ban " + this.name + " from " + this.guild.properties.name],
["textbox", "Reason:", "", function (e) {
reason = e.target.value;
}],
["button", "", "submit", () => {
this.banAPI(reason);
menu.hide();
}]]);
menu.show();
}
banAPI(reason) {
const headers = structuredClone(this.guild.headers);
headers["x-audit-log-reason"] = reason;
fetch(`${this.info.api}/guilds/${this.guild.id}/bans/${this.id}`, {
method: "PUT",
headers
});
}
hasPermission(name) {
if (this.isAdmin()) {
return true;
}
for (const thing of this.roles) {
if (thing.permissions.getPermission(name)) {
return true;
}
}
return false;
}
}
export { Member };

View file

@ -96,6 +96,34 @@ class User {
})
});
});
this.contextmenu.addbutton("Kick member", function (member) {
member.kick();
}, null, function (member) {
if (!member)
return false;
const us = member.guild.member;
if (member.id === us.id) {
return false;
}
if (member.id === member.guild.properties.owner_id) {
return false;
}
return (us.hasPermission("KICK_MEMBERS")) || false;
});
this.contextmenu.addbutton("Ban member", function (member) {
member.ban();
}, null, function (member) {
if (!member)
return false;
const us = member.guild.member;
if (member.id === us.id) {
return false;
}
if (member.id === member.guild.properties.owner_id) {
return false;
}
return (us.hasPermission("BAN_MEMBERS")) || false;
});
}
static clear() {
this.userids = {};

View file

@ -1,7 +1,7 @@
class Contextmenu<x,y>{
static currentmenu;
name:string;
buttons:[string,(e:MouseEvent)=>void,string|null,(this:x,arg:y)=>boolean,(this:x,arg:y)=>boolean,string][];
buttons:[string,(this:x,arg:y,e:MouseEvent)=>void,string|null,(this:x,arg:y)=>boolean,(this:x,arg:y)=>boolean,string][];
div:HTMLDivElement;
static setup(){
Contextmenu.currentmenu="";
@ -19,11 +19,11 @@ class Contextmenu<x,y>{
this.name=name;
this.buttons=[]
}
addbutton(text:string,onclick:(e:MouseEvent)=>void,img:null|string=null,shown:(this:x,arg:y)=>boolean=_=>true,enabled:(this:x,arg:y)=>boolean=_=>true){
addbutton(text:string,onclick:(this:x,arg:y,e:MouseEvent)=>void,img:null|string=null,shown:(this:x,arg:y)=>boolean=_=>true,enabled:(this:x,arg:y)=>boolean=_=>true){
this.buttons.push([text,onclick,img,shown,enabled,"button"]);
return {};
}
addsubmenu(text:string,onclick:(e:MouseEvent)=>void,img=null,shown:(this:x,arg:y)=>boolean=_=>true,enabled:(this:x,arg:y)=>boolean=_=>true){
addsubmenu(text:string,onclick:(this:x,arg:y,e:MouseEvent)=>void,img=null,shown:(this:x,arg:y)=>boolean=_=>true,enabled:(this:x,arg:y)=>boolean=_=>true){
this.buttons.push([text,onclick,img,shown,enabled,"submenu"])
return {};
}

View file

@ -3,6 +3,7 @@ import {Role} from "./role.js";
import {Guild} from "./guild.js";
import { SnowFlake } from "./snowflake.js";
import { memberjson, presencejson, userjson } from "./jsontypes.js";
import { Dialog } from "./dialog.js";
class Member{
static already={};
@ -152,5 +153,65 @@ class Member{
profileclick(html:HTMLElement){
//to be implemented
}
get name(){
return this.nick||this.user.username;
}
kick(){
let reason=""
const menu=new Dialog(["vdiv",
["title","Kick "+this.name+" from "+this.guild.properties.name],
["textbox","Reason:","",function(e:Event){
reason=(e.target as HTMLInputElement).value;
}],
["button","","submit",()=>{
this.kickAPI(reason);
menu.hide();
}]
]);
menu.show();
}
kickAPI(reason:string){
const headers=structuredClone(this.guild.headers);
headers["x-audit-log-reason"]=reason
fetch(`${this.info.api}/guilds/${this.guild.id}/members/${this.id}`,{
method:"DELETE",
headers,
})
}
ban(){
let reason=""
const menu=new Dialog(["vdiv",
["title","Ban "+this.name+" from "+this.guild.properties.name],
["textbox","Reason:","",function(e:Event){
reason=(e.target as HTMLInputElement).value;
}],
["button","","submit",()=>{
this.banAPI(reason);
menu.hide();
}]
]);
menu.show();
}
banAPI(reason:string){
const headers=structuredClone(this.guild.headers);
headers["x-audit-log-reason"]=reason
fetch(`${this.info.api}/guilds/${this.guild.id}/bans/${this.id}`,{
method:"PUT",
headers
})
}
hasPermission(name:string):boolean{
if(this.isAdmin()){
return true;
}
for(const thing of this.roles){
if(thing.permissions.getPermission(name)){
return true;
}
}
return false;
}
}
export {Member};

View file

@ -100,6 +100,32 @@ class User{
})
})
});
this.contextmenu.addbutton("Kick member",function(this:User,member:Member){
member.kick();
},null,function(member){
if(!member) return false;
const us=member.guild.member;
if(member.id===us.id){
return false;
}
if(member.id===member.guild.properties.owner_id){
return false;
}
return (us.hasPermission("KICK_MEMBERS"))||false;
});
this.contextmenu.addbutton("Ban member",function(this:User,member:Member){
member.ban();
},null,function(member){
if(!member) return false;
const us=member.guild.member;
if(member.id===us.id){
return false;
}
if(member.id===member.guild.properties.owner_id){
return false;
}
return (us.hasPermission("BAN_MEMBERS"))||false;
});
}
static clear(){
this.userids={};