Skip to content

Commit

Permalink
offtopic implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 17, 2023
1 parent ff4ba90 commit d28bc12
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 176 deletions.
2 changes: 0 additions & 2 deletions delta/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import which from "./which.ts";
import { Bot } from "../deps.ts";
import trigger from "./trigger.ts";
import useful from "./useful.ts";

export default async (bot: Bot) => {
await bot
.use(which)
.use(useful)
.use(trigger)
};
128 changes: 0 additions & 128 deletions delta/trigger.ts

This file was deleted.

21 changes: 0 additions & 21 deletions delta/which.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use teloxide::{
static TEXT: &str = "<b>Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:</b>\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni <a href='https://github.com/rust-lang-uz/rustina/blob/main/communities.json'>community.json</a> ni yangilang!";

pub async fn command(bot: &Bot, msg: &Message, groups: &Groups) -> ResponseResult<()> {

bot.send_message(msg.chat.id, TEXT)
.parse_mode(ParseMode::Html)
.reply_markup(keyboard_list(&groups, 1))
Expand All @@ -20,7 +19,12 @@ pub async fn command(bot: &Bot, msg: &Message, groups: &Groups) -> ResponseResul
Ok(())
}

pub async fn callback_list(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>, groups: &Groups) -> ResponseResult<()> {
pub async fn callback_list(
bot: &Bot,
q: &CallbackQuery,
args: &Vec<&str>,
groups: &Groups,
) -> ResponseResult<()> {
if !args.is_empty() {
if let Some(Message { id, chat, .. }) = q.message.clone() {
bot.edit_message_text(chat.id, id, TEXT)
Expand Down
20 changes: 11 additions & 9 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ pub mod groups;
pub mod help;
pub mod inline;
pub mod latest;
pub mod offtop;
pub mod rules;
pub mod start;
pub mod version;

pub use inline::inline;

use crate::utils::github::GitHub;
use crate::Command;
use crate::utils::group_manager::Groups;
use crate::Command;
use std::error::Error;
use teloxide::prelude::*;
use teloxide::types::*;

pub async fn commands(
bot: Bot,
_me: teloxide::types::Me,
me: Me,
msg: Message,
cmd: Command,
github: GitHub,
Expand All @@ -31,6 +33,7 @@ pub async fn commands(
Command::Groups => crate::functions::groups::command(&bot, &msg, &groups).await,
Command::Latest => crate::functions::latest::command(&bot, github, &msg).await,
Command::Version => crate::functions::version::command(&bot, github, &msg).await,
Command::Off => crate::functions::offtop::command(&bot, &msg, &me).await,
};

Ok(())
Expand All @@ -55,25 +58,24 @@ pub async fn callback(
"group" => crate::functions::groups::callback_list(&bot, &q, &args, &groups).await,
"detail" => crate::functions::groups::callback_detail(&bot, &q, &args).await,
"version" => crate::functions::version::callback_list(&bot, &q, &args, github).await,
"changelog" => crate::functions::version::callback_detail(&bot, &q, &args, github).await,
"changelog" => {
crate::functions::version::callback_detail(&bot, &q, &args, github).await
}
_ => Ok(()),
};
}

Ok(())
}

pub async fn triggers(
bot: Bot,
msg: Message,
) -> Result<(), Box<dyn Error + Send + Sync>> {
pub async fn triggers(bot: Bot, msg: Message) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(user) = msg.from() {
if let Some(username) = user.username.clone() {
if username == "Channel_Bot" {
// try to delete message and ignore error
match bot.delete_message(msg.chat.id, msg.id).await {
Ok(_) => {},
Err(_) => {},
Ok(_) => {}
Err(_) => {}
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/functions/offtop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use teloxide::{prelude::*, types::*};

static TEXT_FAIL: &str = "Ha-ha... yaxshi urinish!";
static TEXT_NON_REPLY: &str = "↪ Reply bilan ko'rsatingchi habarni!";

pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> {
if msg.reply_to_message().is_none() {
return {
bot.send_message(msg.chat.id, TEXT_NON_REPLY).await?;
Ok(())
};
}

// if replied person is bot itself, send fail message
if let Some(user) = msg.reply_to_message().as_ref().unwrap().from() {
if user.username.clone().unwrap() == me.username() {
return {
bot.send_message(msg.chat.id, TEXT_FAIL).await?;
Ok(())
};
}
}

bot.delete_message(msg.chat.id, msg.id).await?;
bot.delete_message(msg.chat.id, msg.reply_to_message().unwrap().id)
.await?;

bot.send_message(msg.chat.id, view(msg.reply_to_message().unwrap()))
.parse_mode(ParseMode::Html)
.await?;

Ok(())
}

pub fn view(msg: &Message) -> String {
format!(
"<b>Hurmatli <a href=\"tg://user?id={}\">{}</a>,</b>\
\n\n\
Tushunishim bo'yicha siz mavzudan chetlayashayabsiz. Iltimos, \
quyidagi tugmachani bosish orqali bizning offtop guruhga o'tib oling! \
Offtopic guruhimizda istalgan mavzuda suhbatlashish ruxsat etiladi. Boshqalarga halaqit qilmayliga 😉\
\n\n\
<b>Hurmat ila, Rustina (Rastina)</b>",
msg.from().unwrap().id,
msg.from().unwrap().first_name
)
}
19 changes: 11 additions & 8 deletions src/functions/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ pub async fn callback_list(
Ok(())
}


pub async fn callback_detail(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>, github: GitHub) -> ResponseResult<()> {
pub async fn callback_detail(
bot: &Bot,
q: &CallbackQuery,
args: &Vec<&str>,
github: GitHub,
) -> ResponseResult<()> {
let page = args[0].parse::<u32>().unwrap();
let version: Release = github.get_detail(args[1]).await.unwrap();

Expand Down Expand Up @@ -88,7 +92,10 @@ pub fn keyboard_list(
let mut keyboard = Keyboard::new();

for release in releases {
keyboard.text(&release.tag_name, &format!("changelog_{}_{}", page, release.tag_name));
keyboard.text(
&release.tag_name,
&format!("changelog_{}_{}", page, release.tag_name),
);
keyboard.row();
}

Expand All @@ -103,10 +110,7 @@ pub fn keyboard_list(
keyboard.get()
}

pub fn keyboard_detail(
page: u32,
release: Release,
) -> InlineKeyboardMarkup {
pub fn keyboard_detail(page: u32, release: Release) -> InlineKeyboardMarkup {
let mut keyboard = Keyboard::new();

keyboard.url("📝 GitHub da o'qish", release.html_url.as_str());
Expand All @@ -115,4 +119,3 @@ pub fn keyboard_detail(

keyboard.get()
}

14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ pub mod functions;
pub mod hooks;
pub mod utils;

use teloxide::{dispatching::{UpdateHandler, UpdateFilterExt}, prelude::*, utils::command::BotCommands};
use teloxide::{
dispatching::{UpdateFilterExt, UpdateHandler},
prelude::*,
utils::command::BotCommands,
};

#[derive(BotCommands, Clone, Debug)]
#[command(rename_rule = "lowercase", parse_with = "split")]
Expand All @@ -28,6 +32,9 @@ pub enum Command {

/// Specific version
Version,

/// Report offtopic
Off,
}

pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
Expand All @@ -42,8 +49,5 @@ pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'sta
.filter_command::<Command>()
.endpoint(functions::commands),
)
.branch(
Update::filter_message()
.endpoint(functions::triggers),
)
.branch(Update::filter_message().endpoint(functions::triggers))
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crates_io_api::AsyncClient;
use rustina::{handler, utils::{github::GitHub, group_manager::Groups}};
use rustina::{
handler,
utils::{github::GitHub, group_manager::Groups},
};
use std::error::Error;
use teloxide::prelude::*;

Expand Down

1 comment on commit d28bc12

@deno-deploy
Copy link

@deno-deploy Deno Deploy bot commented on d28bc12 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

Module not found "file:///src/delta/trigger.ts".

Please sign in to comment.