Page Menu
Home
desp's stash
Search
Configure Global Search
Log In
Files
F373932
UserPurge.java
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
UserPurge.java
View Options
package
me.despawningbone.discordbot.command.admin
;
import
java.util.Arrays
;
import
java.util.EnumSet
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
me.despawningbone.discordbot.DiscordBot
;
import
me.despawningbone.discordbot.command.Command
;
import
me.despawningbone.discordbot.command.CommandResult
;
import
me.despawningbone.discordbot.command.CommandResult.CommandResultType
;
import
net.dv8tion.jda.api.Permission
;
import
net.dv8tion.jda.api.entities.Member
;
import
net.dv8tion.jda.api.entities.Message
;
import
net.dv8tion.jda.api.entities.TextChannel
;
import
net.dv8tion.jda.api.entities.User
;
public
class
UserPurge
extends
Command
{
public
UserPurge
()
{
this
.
alias
=
Arrays
.
asList
(
"upurge"
);
this
.
desc
=
"Clear a specific user's messages"
;
this
.
usage
=
"<usertag/userid> <count>"
;
this
.
remarks
=
Arrays
.
asList
(
"Note: Count must be in between 1 to 100, and it includes the messages in between."
);
this
.
examples
=
Arrays
.
asList
(
DiscordBot
.
OwnerID
);
//this.botUserLevel = -1; //dont give so much power to botmods
this
.
perms
=
EnumSet
.
of
(
Permission
.
MESSAGE_MANAGE
);
}
@Override
public
CommandResult
execute
(
TextChannel
channel
,
User
author
,
Message
msg
,
String
[]
args
)
{
Member
Bot
=
channel
.
getGuild
().
getMemberById
(
DiscordBot
.
BotID
);
if
(
Bot
.
hasPermission
(
Permission
.
MESSAGE_MANAGE
))
{
int
n
=
0
;
try
{
n
=
Integer
.
parseInt
(
args
[
1
]);
}
catch
(
NumberFormatException
e
)
{
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"Please enter a correct number."
);
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"Please enter a number."
);
}
if
(
n
>
100
)
{
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"The number entered is too large."
);
}
String
SID
=
null
;
if
(
args
.
length
<
1
)
{
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"Please tag a user or enter their ID."
);
}
else
{
List
<
User
>
u
=
msg
.
getMentionedUsers
();
if
(!
u
.
isEmpty
())
{
SID
=
u
.
get
(
0
).
getId
();
}
else
{
SID
=
args
[
0
];
Member
m
=
msg
.
getGuild
().
getMemberById
(
SID
);
if
(
m
==
null
)
{
return
new
CommandResult
(
CommandResultType
.
INVALIDARGS
,
"The user ID is not valid."
);
}
}
}
msg
.
delete
().
queue
();
final
String
purgeID
=
SID
;
channel
.
getHistory
().
retrievePast
(
n
).
queue
(
history
->
{
List
<
Message
>
msgs
=
history
.
stream
()
.
filter
(
hmsg
->
hmsg
.
getAuthor
().
getId
().
equals
(
purgeID
)).
collect
(
Collectors
.
toList
());
if
(
msgs
.
size
()
>
1
)
channel
.
deleteMessages
(
msgs
).
queue
();
else
if
(
msgs
.
size
()
==
1
)
channel
.
deleteMessageById
(
msgs
.
get
(
0
).
getId
());
//if no result ignore
});
return
new
CommandResult
(
CommandResultType
.
SUCCESS
);
}
else
{
return
new
CommandResult
(
CommandResultType
.
FAILURE
,
"The Bot does not have enough permissions to perform that."
);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Aug 1, 12:39 PM (14 h, 55 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
15/6a/7e4273cc177ffa62d7239f06b368
Attached To
rDESB despbot
Event Timeline
Log In to Comment