This commit is contained in:
2024-03-09 03:42:11 +01:00
commit 3190049fd4
7 changed files with 2260 additions and 0 deletions

7
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,7 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

View File

@@ -0,0 +1,50 @@
name: Build docker image
on:
push:
branches:
- 'main'
jobs:
Build-Docker-Image:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- id: repository_name
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository }}
-
name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v5
env:
IMAGE: ${{ steps.repository_name.outputs.lowercase }}
with:
push: true
platforms: linux/amd64
tags: ghcr.io/${{ env.IMAGE }}:latest
context: .
file: ./docker/build.dockerfile
# -
# name: Invoke deployment hook
# uses: joelwmale/webhook-action@master
# with:
# url: ${{ secrets.WEBHOOK_URL }}

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/target
.vscode

2113
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "discord-to-telegram-messages-resender"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
once_cell = "1.19.0"
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros", "time"] }
serenity = "0.12.1"
reqwest = { version = "0.11.23", features = ["json"] }

29
src/config.rs Normal file
View File

@@ -0,0 +1,29 @@
use once_cell::sync::Lazy;
fn get_env(env: &'static str) -> String {
std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env))
}
pub struct Config {
pub discord_bot_token: String,
pub discord_channel_id: u64,
pub telegram_bot_token: String,
pub telgram_channel_id: i128,
}
impl Config {
pub fn load() -> Config {
Config {
discord_bot_token: get_env("DISCORD_BOT_TOKEN"),
discord_channel_id: get_env("DISCORD_CHANNEL_ID").parse().unwrap(),
telegram_bot_token: get_env("TELEGRAM_BOT_TOKEN"),
telgram_channel_id: get_env("TELEGRAM_CHANNEL_ID").parse().unwrap(),
}
}
}
pub static CONFIG: Lazy<Config> = Lazy::new(Config::load);

46
src/main.rs Normal file
View File

@@ -0,0 +1,46 @@
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::*;
pub mod config;
async fn send_to_telegram(msg: &str) {
let url = format!(
"https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}",
config::CONFIG.telegram_bot_token,
config::CONFIG.telgram_channel_id,
msg
);
reqwest::get(&url).await.expect("Error sending message to Telegram");
}
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, _ctx: Context, msg: Message) {
if msg.channel_id != config::CONFIG.discord_channel_id {
return;
}
send_to_telegram(&msg.content).await;
}
}
#[tokio::main]
async fn main() {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client =
Client::builder(&config::CONFIG.discord_bot_token, intents).event_handler(Handler).await.expect("Err creating client");
if let Err(why) = client.start().await {
println!("Client error: {why:?}");
}
}