Improved !oops command (#3726).

This commit is contained in:
Christian P. MOMON 2021-04-13 20:23:48 +02:00
parent 03e346554f
commit b5fd23533f
5 changed files with 131 additions and 21 deletions

View File

@ -18,12 +18,13 @@
*/
package org.april.hebdobot.bot.hooks;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.bot.Hebdobot;
import org.april.hebdobot.bot.review.Topic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringsUtils;
/**
* The Class CancelPreviousInputHook.
*/
@ -40,9 +41,9 @@ public class CancelPreviousInputHook extends Hook
{
boolean result;
if (StringUtils.equalsIgnoreCase(message, "!cancelprevious"))
if (StringsUtils.containsAnyIgnoreCase(message, "!cancelprevious", "!oops", "!oups"))
{
logger.info("!cancelprevious caught.");
logger.info("!cancelprevious/!oups caught.");
// Missing.
if (bot.getReview() == null)
@ -58,14 +59,14 @@ public class CancelPreviousInputHook extends Hook
}
else
{
if (bot.getReview().getParticipants().contains(sender))
String previousMessage = topic.cancelPreviousMessage(sender);
if (previousMessage == null)
{
topic.cancelPreviousMessage(sender);
bot.sendMessage("Fait.");
bot.sendMessage("Vous n'avez pas d'entrée en cours.");
}
else
{
bot.sendMessage("Vous n'avez pas d'entrée en cours.");
bot.sendMessage(sender + ", suppressiond de votre dernière entrée : " + previousMessage);
}
}
}

View File

@ -1,5 +1,5 @@
/**
* Copyright (C) 2017-2019 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2017-2021 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
*
* This file is part of (April) Hebdobot.
@ -56,13 +56,23 @@ public class CollectiveTopic extends Topic
* @see org.april.hebdobot.bot.review.Topic#cancelPrevious(java.lang.String)
*/
@Override
public void cancelPreviousMessage(final String author)
public String cancelPreviousMessage(final String author)
{
String result;
Message previousMessage = this.messages.getByAuthor(author).getLast();
if (previousMessage != null)
if (previousMessage == null)
{
result = null;
}
else
{
this.messages.remove(previousMessage);
result = previousMessage.getContent();
}
//
return result;
}
/**
@ -90,4 +100,23 @@ public class CollectiveTopic extends Topic
//
return result;
}
/**
* Checks for message.
*
* @param author
* the author
* @return true, if successful
*/
@Override
public boolean hasMessage(final String author)
{
boolean result;
Messages authorMessage = this.messages.getByAuthor(author);
result = authorMessage.isEmpty();
//
return result;
}
}

View File

@ -1,5 +1,5 @@
/**
* Copyright (C) 2017-2019 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2017-2021 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
*
* This file is part of (April) Hebdobot.
@ -19,9 +19,6 @@
*/
package org.april.hebdobot.bot.review;
import java.util.HashMap;
import java.util.Map;
import fr.devinsy.strings.StringSet;
/**
@ -29,7 +26,7 @@ import fr.devinsy.strings.StringSet;
*/
public class IndividualTopic extends Topic
{
private final Map<String, Messages> messages;
private final MessageMap messages;
/**
* Instantiates a new individual topic.
@ -41,7 +38,7 @@ public class IndividualTopic extends Topic
{
super(title);
this.messages = new HashMap<>();
this.messages = new MessageMap();
}
/* (non-Javadoc)
@ -62,13 +59,23 @@ public class IndividualTopic extends Topic
* @see org.april.hebdobot.bot.review.Topic#cancelPrevious(java.lang.String)
*/
@Override
public void cancelPreviousMessage(final String participant)
public String cancelPreviousMessage(final String participant)
{
String result;
Messages authorMessages = this.messages.get(participant);
if (authorMessages != null)
if (authorMessages == null)
{
authorMessages.removeLast();
result = null;
}
else
{
Message removed = authorMessages.removeLast();
result = removed.getContent();
}
//
return result;
}
/**
@ -102,6 +109,33 @@ public class IndividualTopic extends Topic
return result;
}
/**
* Checks for message.
*
* @param participant
* the participant
* @return true, if successful
*/
@Override
public boolean hasMessage(final String participant)
{
boolean result;
Messages messages = this.messages.get(participant);
if ((messages == null) || (messages.isEmpty()))
{
result = false;
}
else
{
result = true;
}
//
return result;
}
/**
* Checks for participant.
*

View File

@ -0,0 +1,37 @@
/**
* Copyright (C) 2021 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.bot.review;
import java.util.HashMap;
/**
* The Class MessageMap.
*/
public class MessageMap extends HashMap<String, Messages>
{
private static final long serialVersionUID = 2324236890989710448L;
/**
* Instantiates a new message map.
*/
public MessageMap()
{
super();
}
}

View File

@ -1,5 +1,5 @@
/**
* Copyright (C) 2017-2019 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2017-2021 Christian Pierre MOMON <cmomon@april.org>
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
*
* This file is part of (April) Hebdobot.
@ -53,7 +53,7 @@ public abstract class Topic
* @param participant
* the participant
*/
public abstract void cancelPreviousMessage(final String participant);
public abstract String cancelPreviousMessage(final String participant);
/**
* Gets the participants.
@ -71,4 +71,13 @@ public abstract class Topic
{
return this.title;
}
/**
* Checks for message from.
*
* @param author
* the author
* @return true, if successful
*/
public abstract boolean hasMessage(String author);
}