Added error handling for getting reputation

main
David 2023-01-19 15:26:50 +01:00
parent d3469fb9fb
commit 69e317487d
2 changed files with 88 additions and 69 deletions

View File

@ -9,7 +9,7 @@ The bot is orientated towards admins of large Matrix rooms that are public and r
## How to run?
**Using Docker:**
A minimal Docker image containing the compiled binary is published on [Docker Hub](https://hub.docker.com/r/davidlocalhost/matrix-modbot). Note that this image is automatically pushed using a CI/CD Pipeline for every commit made to the main branch. Expect there to be bugs and breaking changes.
A minimal Docker image containing the compiled binary is published on [Docker Hub](https://hub.docker.com/r/davidlocalhost/matrix-modbot). Note that this image is automatically pushed using a CI/CD Pipeline for every commit made to the main branch. Expect there to be bugs and breaking changes. If you want to use a stable release, go to Deployments -> Releases and download the source code and build a Docker image containing a release binary.
`sudo docker run -v /local/path/to/config.ini:/config.ini -t matrix-modbot:latest`
Without root:
@ -21,22 +21,39 @@ cargo build --release
cargo run /path/to/config.ini
```
Note that this bot will NOT run in an encrypted room. It also can only join 1 room at a time and isn't integrated with Element's Spaces feature. You must specify the room to join in the config file and invite the bot. You will also have to create an account for the bot and put the credentials into the config file.
Note that this bot will NOT run in an encrypted room. It also can only join 1 room at a time and isn't integrated with Element's Spaces feature. You must specify the room to join in the config file and invite the bot. You will also have to create an account for the bot and put the credentials into the config file. It is possible to use this bot with Matrix-Discord bridge, as long as the bridge bot is set to have moderator privileges.
### Configuration
For configuration, see `example_config.ini`. The options should be self-explanatory.
See example below
```
[credentials]
user_id = @bot:matrix.org
homeserver = matrix.org
password = insert-password-here
[room]
room = !insert-room-id:matrix.org
[swear_list]
url = https://raw.githubusercontent.com/chucknorris-io/swear-words/master/en
[rules]
allow_swear = false
```
Note that the configuration file should be specified if the file name is not `config.ini`.
## Features
- Easy to configure
- Configurable swear detection
- Spam detection (WIP)
- Configurable swear detection based on list of blocked words
- Spam detection
- Anti-Caps
- Anti-ASCII Art Spam
- Reputation system
- All members of a room have reputation points, they are deducted when spam/swear/caps are detected
- Automatically kicks a member if reputation is below -15
- Members can award each other with maximum 1 reputation point every 24hr
- Users with power level >= 50 are not affected (Mods and Admins)
- Users with power level >= 50 are not affected by the limit (Mods and Admins)
- Mods can warn specific members by deducting reputation points
### Commands
For awarding someone reputation:
@ -47,3 +64,8 @@ For deducting someone's reputation (moderators only):
To get own reputation:
- "!modbot reputation"
### Credits
Many thanks to:
- [brokenbyte](https://gitlab.com/brokenbyte) for all the help and support along the way
- [hebbot](https://github.com/haecker-felix/hebbot), as an example I could follow during the development of this bot

View File

@ -291,68 +291,65 @@ pub mod bot {
}
async fn detect_spam(&mut self, event: &OriginalSyncRoomMessageEvent) {
let author = self
.joined_room
.get_member(&event.sender)
.await
.unwrap()
.unwrap();
let author_name = author.user_id().as_str().to_string();
let curr_utc = Utc::now().timestamp();
let expire_time: i64 = curr_utc - 5;
let mut expired_msgs: Vec<i64> = vec![];
if let Some(author) = self.joined_room.get_member(&event.sender).await.unwrap() {
let author_name = author.user_id().as_str().to_string();
let curr_utc = Utc::now().timestamp();
let expire_time: i64 = curr_utc - 5;
let mut expired_msgs: Vec<i64> = vec![];
let spam_data = self.spam_db_handle.get(&author_name);
match spam_data {
Ok(_) => {
if spam_data.clone().unwrap().is_some() {
let mut data_vec = convert_vec_to_str(
str::from_utf8(&spam_data.unwrap().unwrap()[..])
.unwrap()
.as_ref(),
);
if !data_vec.is_empty() {
for time in &data_vec {
if time < &expire_time {
expired_msgs.push(*time)
let spam_data = self.spam_db_handle.get(&author_name);
match spam_data {
Ok(_) => {
if spam_data.clone().unwrap().is_some() {
let mut data_vec = convert_vec_to_str(
str::from_utf8(&spam_data.unwrap().unwrap()[..])
.unwrap()
.as_ref(),
);
if !data_vec.is_empty() {
for time in &data_vec {
if time < &expire_time {
expired_msgs.push(*time)
}
}
for msg in expired_msgs {
let _ = &data_vec.retain(|value| *value != msg);
}
}
for msg in expired_msgs {
let _ = &data_vec.retain(|value| *value != msg);
data_vec.push(curr_utc);
if data_vec.len() > 3 && author_name != self.info.user_id {
self.delete_message_from_room(&event.event_id, "Spamming")
.await;
self.update_reputation_for_member(&author, -1)
.await
.unwrap();
}
dbg!(self
.spam_db_handle
.insert(&author_name, format!("{:?}", data_vec).as_str().as_bytes())
.unwrap());
} else {
dbg!(self
.spam_db_handle
.insert(
&author_name,
format!("{:?}", vec![curr_utc]).as_str().as_bytes()
)
.unwrap());
}
data_vec.push(curr_utc);
if data_vec.len() > 3 && author_name != self.info.user_id {
self.delete_message_from_room(&event.event_id, "Spamming")
.await;
self.update_reputation_for_member(&author, -1)
.await
.unwrap();
}
dbg!(self.database_handle.remove(&author_name).unwrap()); // Removes old data, prevents duplicate entries
}
Err(_) => {
dbg!(self
.spam_db_handle
.insert(&author_name, format!("{:?}", data_vec).as_str().as_bytes())
.unwrap());
} else {
dbg!(self
.spam_db_handle
.insert(
&author_name,
format!("{:?}", vec![curr_utc]).as_str().as_bytes()
)
.insert(&author_name, "[]".as_bytes())
.unwrap());
}
}
Err(_) => {
dbg!(self
.spam_db_handle
.insert(&author_name, "[]".as_bytes())
.unwrap());
}
} else {
self.send_message("Problem getting author of message").await;
}
}
@ -432,17 +429,17 @@ pub mod bot {
.await
.unwrap()
.unwrap();
let user_data = dbg!(self
.database_handle
.get(author.user_id().as_str())
.unwrap()
.unwrap());
let (_, reputation) = convert_from_bytes_sled(&user_data);
self.send_message(
format!("Your current reputation is: {}", reputation).as_str(),
)
.await;
if let Some(user_data) =
dbg!(self.database_handle.get(author.user_id().as_str()).unwrap())
{
let (_, reputation) = convert_from_bytes_sled(&user_data);
self.send_message(
format!("Your current reputation is: {}", reputation).as_str(),
)
.await;
} else {
self.send_message("Error getting reputation").await;
}
};
}
}