Compare commits
No commits in common. "master" and "new" have entirely different histories.
@ -1,5 +1,6 @@
|
|||||||
kind: pipeline
|
kind: pipeline
|
||||||
name: leetbot
|
type: kubernetes
|
||||||
|
name: default
|
||||||
|
|
||||||
clone:
|
clone:
|
||||||
skip_verify: true
|
skip_verify: true
|
||||||
@ -18,4 +19,3 @@ steps:
|
|||||||
insecure: true
|
insecure: true
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
tags: latest
|
tags: latest
|
||||||
cache_from: registry.local/northcode/leetbot
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
FROM debian
|
FROM debian
|
||||||
|
|
||||||
RUN apt update
|
RUN apt update
|
||||||
RUN apt install -y openssl ca-certificates
|
RUN apt install -y libssl1.1 openssl ca-certificates
|
||||||
|
|
||||||
ADD target/release/telegram-leetbot /bin/.
|
ADD target/release/telegram-leetbot /bin/.
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@ type: application
|
|||||||
# This is the chart version. This version number should be incremented each time you make changes
|
# This is the chart version. This version number should be incremented each time you make changes
|
||||||
# to the chart and its templates, including the app version.
|
# to the chart and its templates, including the app version.
|
||||||
# Versions are expected to follow Semantic Versioning (https://semver.org/)
|
# Versions are expected to follow Semantic Versioning (https://semver.org/)
|
||||||
version: 0.1.1
|
version: 0.1.0
|
||||||
|
|
||||||
# This is the version number of the application being deployed. This version number should be
|
# This is the version number of the application being deployed. This version number should be
|
||||||
# incremented each time you make changes to the application. Versions are not expected to
|
# incremented each time you make changes to the application. Versions are not expected to
|
||||||
|
|||||||
@ -19,7 +19,7 @@ spec:
|
|||||||
name: leetbot
|
name: leetbot
|
||||||
envFrom:
|
envFrom:
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: {{ .Values.app.secret_name }}
|
name: {{ .Release.Name }}-token
|
||||||
- configMapRef:
|
- configMapRef:
|
||||||
name: {{ .Release.Name }}-cm
|
name: {{ .Release.Name }}-cm
|
||||||
{{- if .Values.storage.enabled }}
|
{{- if .Values.storage.enabled }}
|
||||||
|
|||||||
7
chart/templates/secret.yml
Normal file
7
chart/templates/secret.yml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Secret
|
||||||
|
metadata:
|
||||||
|
name: {{ .Release.Name }}-token
|
||||||
|
data:
|
||||||
|
TELEGRAM_BOT_TOKEN: {{ .Values.app.token | b64enc }}
|
||||||
|
DB_URL: {{ .Values.app.db | b64enc }}
|
||||||
@ -2,7 +2,8 @@ image:
|
|||||||
repository: registry.local/northcode/leetbot
|
repository: registry.local/northcode/leetbot
|
||||||
|
|
||||||
app:
|
app:
|
||||||
secret_name: leetbot-token
|
token: blabla
|
||||||
|
db: sqlite:leet.db
|
||||||
tz: Europe/Oslo
|
tz: Europe/Oslo
|
||||||
|
|
||||||
storage:
|
storage:
|
||||||
|
|||||||
10
src/lib.rs
10
src/lib.rs
@ -1,17 +1,17 @@
|
|||||||
use chrono::NaiveTime;
|
use chrono::{Local, NaiveTime};
|
||||||
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn text_is_leet(text: &str) -> bool {
|
pub fn text_is_leet(text: &str) -> bool {
|
||||||
text.eq_ignore_ascii_case("leet") || text == "l33t" || text == "1337"
|
text.eq_ignore_ascii_case("leet") || text == "l33t" || text == "1337"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
pub fn time_is_leet() -> bool {
|
||||||
pub fn time_is_leet(time: NaiveTime) -> bool {
|
let now = Local::now().time();
|
||||||
let leet = NaiveTime::from_hms(13,37,30);
|
let leet = NaiveTime::from_hms(13,37,30);
|
||||||
|
|
||||||
let allowed_timegap = 30;
|
let allowed_timegap = 30;
|
||||||
|
|
||||||
let distance = time - leet;
|
let distance = now - leet;
|
||||||
|
|
||||||
distance.num_seconds().abs() <= allowed_timegap
|
distance.num_seconds().abs() <= allowed_timegap
|
||||||
}
|
}
|
||||||
|
|||||||
65
src/main.rs
65
src/main.rs
@ -1,7 +1,7 @@
|
|||||||
mod lib;
|
mod lib;
|
||||||
use crate::lib::{text_is_leet, time_is_leet};
|
use crate::lib::*;
|
||||||
|
|
||||||
use chrono::{NaiveDateTime, NaiveTime, Local};
|
use chrono::{NaiveDateTime, Local};
|
||||||
use sqlx::AnyPool;
|
use sqlx::AnyPool;
|
||||||
use sqlx::any::AnyPoolOptions;
|
use sqlx::any::AnyPoolOptions;
|
||||||
use teloxide::{prelude::*, RequestError, dispatching::UpdateFilterExt, utils::command::BotCommands};
|
use teloxide::{prelude::*, RequestError, dispatching::UpdateFilterExt, utils::command::BotCommands};
|
||||||
@ -48,20 +48,22 @@ enum LeetError {
|
|||||||
DbError(#[from] sqlx::Error)
|
DbError(#[from] sqlx::Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_leet(username: &str, sent_time: NaiveDateTime, pool: &AnyPool) -> Result<(), LeetError> {
|
async fn check_leet(username: &str, pool: &AnyPool) -> Result<(), LeetError> {
|
||||||
if ! time_is_leet(sent_time.time()) {
|
if ! time_is_leet() {
|
||||||
return Err(LeetError::NotLeet);
|
return Err(LeetError::NotLeet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let now = Local::now().naive_local();
|
||||||
|
|
||||||
let latest_leet : Option<(i32, String, NaiveDateTime)> = sqlx::query_as("select * from leet_log where username = $1 order by log_time desc")
|
let latest_leet : Option<(i32, String, NaiveDateTime)> = sqlx::query_as("select * from leet_log where username = $1 order by log_time desc")
|
||||||
.bind(username)
|
.bind(username)
|
||||||
.fetch_optional(pool)
|
.fetch_optional(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(latest_leet) = latest_leet {
|
if let Some(latest_leet) = latest_leet {
|
||||||
let distance = sent_time - latest_leet.2;
|
let distance = now - latest_leet.2;
|
||||||
|
|
||||||
if distance.num_hours() < 1 {
|
if distance.num_hours() < 24 {
|
||||||
return Err(LeetError::AlreadyHitLeet);
|
return Err(LeetError::AlreadyHitLeet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,10 +71,16 @@ async fn check_leet(username: &str, sent_time: NaiveDateTime, pool: &AnyPool) ->
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn log_leet(username: &str, sent_time: NaiveDateTime, pool: &AnyPool) -> Result<(), sqlx::Error> {
|
async fn log_leet(username: &str, pool: &AnyPool) -> Result<(), sqlx::Error> {
|
||||||
sqlx::query("insert into leet_log (username, log_time) values ($1, $2)")
|
let sql_now = match pool.any_kind() {
|
||||||
|
sqlx::any::AnyKind::Postgres => "now()",
|
||||||
|
sqlx::any::AnyKind::Sqlite => "datetime()"
|
||||||
|
};
|
||||||
|
|
||||||
|
let query_sql = format!("insert into leet_log (username, log_time) values ($1, {})", sql_now);
|
||||||
|
|
||||||
|
sqlx::query(&query_sql)
|
||||||
.bind(username)
|
.bind(username)
|
||||||
.bind(sent_time)
|
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@ -99,11 +107,7 @@ fn message_ok() -> Result<(), MessageHandlerError> {
|
|||||||
#[command(rename = "lowercase", description = "score commands")]
|
#[command(rename = "lowercase", description = "score commands")]
|
||||||
enum LeetCommand {
|
enum LeetCommand {
|
||||||
#[command(description = "get scores")]
|
#[command(description = "get scores")]
|
||||||
Scores,
|
Scores
|
||||||
#[command(description = "get current time info")]
|
|
||||||
Time,
|
|
||||||
#[command(description = "show help about leetbot")]
|
|
||||||
Help
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error,Debug)]
|
#[derive(Error,Debug)]
|
||||||
@ -136,11 +140,7 @@ async fn leet_message_handler(message: Message, bot: AutoSend<Bot>, pool: AnyPoo
|
|||||||
.username.clone()
|
.username.clone()
|
||||||
.ok_or(MessageHandlerError::NoUsername)?;
|
.ok_or(MessageHandlerError::NoUsername)?;
|
||||||
|
|
||||||
let message_sent_time = message.date
|
let leet_check = check_leet(&username, &pool).await;
|
||||||
.with_timezone(&Local)
|
|
||||||
.naive_local();
|
|
||||||
|
|
||||||
let leet_check = check_leet(&username, message_sent_time, &pool).await;
|
|
||||||
|
|
||||||
if let Err(e) = leet_check {
|
if let Err(e) = leet_check {
|
||||||
match e {
|
match e {
|
||||||
@ -154,7 +154,7 @@ async fn leet_message_handler(message: Message, bot: AutoSend<Bot>, pool: AnyPoo
|
|||||||
return message_ok();
|
return message_ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
log_leet(&username, message_sent_time, &pool).await?;
|
log_leet(&username, &pool).await?;
|
||||||
|
|
||||||
let scores = get_scores(&pool).await?;
|
let scores = get_scores(&pool).await?;
|
||||||
|
|
||||||
@ -178,28 +178,6 @@ async fn leet_command_handler(message: Message, cmd: LeetCommand, bot: AutoSend<
|
|||||||
let msg = format!("Leet scores:\n{}", scores_str);
|
let msg = format!("Leet scores:\n{}", scores_str);
|
||||||
|
|
||||||
bot.send_message(message.chat.id, msg).await?;
|
bot.send_message(message.chat.id, msg).await?;
|
||||||
},
|
|
||||||
LeetCommand::Time => {
|
|
||||||
let message_time = message.date
|
|
||||||
.with_timezone(&Local)
|
|
||||||
.naive_local();
|
|
||||||
let current_time = Local::now().time();
|
|
||||||
let leet_time = NaiveTime::from_hms(13,37,30);
|
|
||||||
let distance_to_leet = leet_time - current_time;
|
|
||||||
|
|
||||||
let msg = format!("My current time is: {}
|
|
||||||
message was sent at: {}
|
|
||||||
leet is at: {}, in {} seconds",
|
|
||||||
current_time,
|
|
||||||
message_time,
|
|
||||||
leet_time,
|
|
||||||
distance_to_leet.num_seconds()
|
|
||||||
);
|
|
||||||
|
|
||||||
bot.send_message(message.chat.id, msg).await?;
|
|
||||||
},
|
|
||||||
LeetCommand::Help => {
|
|
||||||
bot.send_message(message.chat.id, LeetCommand::descriptions().to_string()).await?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +212,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
.dependencies(dptree::deps![db_pool])
|
.dependencies(dptree::deps![db_pool])
|
||||||
.default_handler(|_upd| async move {
|
.default_handler(|upd| async move {
|
||||||
|
println!("unhandled update: {:?}", upd);
|
||||||
})
|
})
|
||||||
.error_handler(LoggingErrorHandler::with_custom_text("error during dispatch"))
|
.error_handler(LoggingErrorHandler::with_custom_text("error during dispatch"))
|
||||||
.enable_ctrlc_handler()
|
.enable_ctrlc_handler()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user