silly goober bot

feat: code expantion

+465 -11
+348 -9
Cargo.lock
··· 62 62 ] 63 63 64 64 [[package]] 65 + name = "atomic-waker" 66 + version = "1.1.2" 67 + source = "registry+https://github.com/rust-lang/crates.io-index" 68 + checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" 69 + 70 + [[package]] 65 71 name = "autocfg" 66 72 version = "1.3.0" 67 73 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 113 119 "color-eyre", 114 120 "dotenv", 115 121 "humantime", 122 + "lazy_static", 116 123 "poise", 117 124 "regex", 125 + "reqwest 0.12.4", 118 126 "serenity", 119 127 "tokio", 120 128 ] ··· 464 472 checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 465 473 466 474 [[package]] 475 + name = "foreign-types" 476 + version = "0.3.2" 477 + source = "registry+https://github.com/rust-lang/crates.io-index" 478 + checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" 479 + dependencies = [ 480 + "foreign-types-shared", 481 + ] 482 + 483 + [[package]] 484 + name = "foreign-types-shared" 485 + version = "0.1.1" 486 + source = "registry+https://github.com/rust-lang/crates.io-index" 487 + checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" 488 + 489 + [[package]] 467 490 name = "form_urlencoded" 468 491 version = "1.2.1" 469 492 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 611 634 ] 612 635 613 636 [[package]] 637 + name = "h2" 638 + version = "0.4.5" 639 + source = "registry+https://github.com/rust-lang/crates.io-index" 640 + checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" 641 + dependencies = [ 642 + "atomic-waker", 643 + "bytes", 644 + "fnv", 645 + "futures-core", 646 + "futures-sink", 647 + "http 1.1.0", 648 + "indexmap", 649 + "slab", 650 + "tokio", 651 + "tokio-util", 652 + "tracing", 653 + ] 654 + 655 + [[package]] 614 656 name = "hashbrown" 615 657 version = "0.14.5" 616 658 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 656 698 ] 657 699 658 700 [[package]] 701 + name = "http-body" 702 + version = "1.0.0" 703 + source = "registry+https://github.com/rust-lang/crates.io-index" 704 + checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" 705 + dependencies = [ 706 + "bytes", 707 + "http 1.1.0", 708 + ] 709 + 710 + [[package]] 711 + name = "http-body-util" 712 + version = "0.1.1" 713 + source = "registry+https://github.com/rust-lang/crates.io-index" 714 + checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" 715 + dependencies = [ 716 + "bytes", 717 + "futures-core", 718 + "http 1.1.0", 719 + "http-body 1.0.0", 720 + "pin-project-lite", 721 + ] 722 + 723 + [[package]] 659 724 name = "httparse" 660 725 version = "1.8.0" 661 726 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 683 748 "futures-channel", 684 749 "futures-core", 685 750 "futures-util", 686 - "h2", 751 + "h2 0.3.26", 687 752 "http 0.2.12", 688 - "http-body", 753 + "http-body 0.4.6", 689 754 "httparse", 690 755 "httpdate", 691 756 "itoa", ··· 698 763 ] 699 764 700 765 [[package]] 766 + name = "hyper" 767 + version = "1.3.1" 768 + source = "registry+https://github.com/rust-lang/crates.io-index" 769 + checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" 770 + dependencies = [ 771 + "bytes", 772 + "futures-channel", 773 + "futures-util", 774 + "h2 0.4.5", 775 + "http 1.1.0", 776 + "http-body 1.0.0", 777 + "httparse", 778 + "itoa", 779 + "pin-project-lite", 780 + "smallvec", 781 + "tokio", 782 + "want", 783 + ] 784 + 785 + [[package]] 701 786 name = "hyper-rustls" 702 787 version = "0.24.2" 703 788 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 705 790 dependencies = [ 706 791 "futures-util", 707 792 "http 0.2.12", 708 - "hyper", 793 + "hyper 0.14.29", 709 794 "rustls 0.21.12", 710 795 "tokio", 711 796 "tokio-rustls 0.24.1", 797 + ] 798 + 799 + [[package]] 800 + name = "hyper-tls" 801 + version = "0.6.0" 802 + source = "registry+https://github.com/rust-lang/crates.io-index" 803 + checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" 804 + dependencies = [ 805 + "bytes", 806 + "http-body-util", 807 + "hyper 1.3.1", 808 + "hyper-util", 809 + "native-tls", 810 + "tokio", 811 + "tokio-native-tls", 812 + "tower-service", 813 + ] 814 + 815 + [[package]] 816 + name = "hyper-util" 817 + version = "0.1.5" 818 + source = "registry+https://github.com/rust-lang/crates.io-index" 819 + checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" 820 + dependencies = [ 821 + "bytes", 822 + "futures-channel", 823 + "futures-util", 824 + "http 1.1.0", 825 + "http-body 1.0.0", 826 + "hyper 1.3.1", 827 + "pin-project-lite", 828 + "socket2", 829 + "tokio", 830 + "tower", 831 + "tower-service", 832 + "tracing", 712 833 ] 713 834 714 835 [[package]] ··· 885 1006 ] 886 1007 887 1008 [[package]] 1009 + name = "native-tls" 1010 + version = "0.2.12" 1011 + source = "registry+https://github.com/rust-lang/crates.io-index" 1012 + checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" 1013 + dependencies = [ 1014 + "libc", 1015 + "log", 1016 + "openssl", 1017 + "openssl-probe", 1018 + "openssl-sys", 1019 + "schannel", 1020 + "security-framework", 1021 + "security-framework-sys", 1022 + "tempfile", 1023 + ] 1024 + 1025 + [[package]] 888 1026 name = "num-conv" 889 1027 version = "0.1.0" 890 1028 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 925 1063 checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" 926 1064 927 1065 [[package]] 1066 + name = "openssl" 1067 + version = "0.10.64" 1068 + source = "registry+https://github.com/rust-lang/crates.io-index" 1069 + checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" 1070 + dependencies = [ 1071 + "bitflags 2.5.0", 1072 + "cfg-if", 1073 + "foreign-types", 1074 + "libc", 1075 + "once_cell", 1076 + "openssl-macros", 1077 + "openssl-sys", 1078 + ] 1079 + 1080 + [[package]] 1081 + name = "openssl-macros" 1082 + version = "0.1.1" 1083 + source = "registry+https://github.com/rust-lang/crates.io-index" 1084 + checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" 1085 + dependencies = [ 1086 + "proc-macro2", 1087 + "quote", 1088 + "syn 2.0.66", 1089 + ] 1090 + 1091 + [[package]] 1092 + name = "openssl-probe" 1093 + version = "0.1.5" 1094 + source = "registry+https://github.com/rust-lang/crates.io-index" 1095 + checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" 1096 + 1097 + [[package]] 1098 + name = "openssl-sys" 1099 + version = "0.9.102" 1100 + source = "registry+https://github.com/rust-lang/crates.io-index" 1101 + checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" 1102 + dependencies = [ 1103 + "cc", 1104 + "libc", 1105 + "pkg-config", 1106 + "vcpkg", 1107 + ] 1108 + 1109 + [[package]] 928 1110 name = "owo-colors" 929 1111 version = "3.5.0" 930 1112 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 960 1142 checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" 961 1143 962 1144 [[package]] 1145 + name = "pin-project" 1146 + version = "1.1.5" 1147 + source = "registry+https://github.com/rust-lang/crates.io-index" 1148 + checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" 1149 + dependencies = [ 1150 + "pin-project-internal", 1151 + ] 1152 + 1153 + [[package]] 1154 + name = "pin-project-internal" 1155 + version = "1.1.5" 1156 + source = "registry+https://github.com/rust-lang/crates.io-index" 1157 + checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" 1158 + dependencies = [ 1159 + "proc-macro2", 1160 + "quote", 1161 + "syn 2.0.66", 1162 + ] 1163 + 1164 + [[package]] 963 1165 name = "pin-project-lite" 964 1166 version = "0.2.14" 965 1167 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 970 1172 version = "0.1.0" 971 1173 source = "registry+https://github.com/rust-lang/crates.io-index" 972 1174 checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 1175 + 1176 + [[package]] 1177 + name = "pkg-config" 1178 + version = "0.3.30" 1179 + source = "registry+https://github.com/rust-lang/crates.io-index" 1180 + checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" 973 1181 974 1182 [[package]] 975 1183 name = "poise" ··· 1120 1328 "encoding_rs", 1121 1329 "futures-core", 1122 1330 "futures-util", 1123 - "h2", 1331 + "h2 0.3.26", 1124 1332 "http 0.2.12", 1125 - "http-body", 1126 - "hyper", 1333 + "http-body 0.4.6", 1334 + "hyper 0.14.29", 1127 1335 "hyper-rustls", 1128 1336 "ipnet", 1129 1337 "js-sys", ··· 1134 1342 "percent-encoding", 1135 1343 "pin-project-lite", 1136 1344 "rustls 0.21.12", 1137 - "rustls-pemfile", 1345 + "rustls-pemfile 1.0.4", 1138 1346 "serde", 1139 1347 "serde_json", 1140 1348 "serde_urlencoded", ··· 1150 1358 "wasm-streams", 1151 1359 "web-sys", 1152 1360 "webpki-roots 0.25.4", 1153 - "winreg", 1361 + "winreg 0.50.0", 1362 + ] 1363 + 1364 + [[package]] 1365 + name = "reqwest" 1366 + version = "0.12.4" 1367 + source = "registry+https://github.com/rust-lang/crates.io-index" 1368 + checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" 1369 + dependencies = [ 1370 + "base64 0.22.1", 1371 + "bytes", 1372 + "encoding_rs", 1373 + "futures-core", 1374 + "futures-util", 1375 + "h2 0.4.5", 1376 + "http 1.1.0", 1377 + "http-body 1.0.0", 1378 + "http-body-util", 1379 + "hyper 1.3.1", 1380 + "hyper-tls", 1381 + "hyper-util", 1382 + "ipnet", 1383 + "js-sys", 1384 + "log", 1385 + "mime", 1386 + "native-tls", 1387 + "once_cell", 1388 + "percent-encoding", 1389 + "pin-project-lite", 1390 + "rustls-pemfile 2.1.2", 1391 + "serde", 1392 + "serde_json", 1393 + "serde_urlencoded", 1394 + "sync_wrapper", 1395 + "system-configuration", 1396 + "tokio", 1397 + "tokio-native-tls", 1398 + "tower-service", 1399 + "url", 1400 + "wasm-bindgen", 1401 + "wasm-bindgen-futures", 1402 + "web-sys", 1403 + "winreg 0.52.0", 1154 1404 ] 1155 1405 1156 1406 [[package]] ··· 1223 1473 ] 1224 1474 1225 1475 [[package]] 1476 + name = "rustls-pemfile" 1477 + version = "2.1.2" 1478 + source = "registry+https://github.com/rust-lang/crates.io-index" 1479 + checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" 1480 + dependencies = [ 1481 + "base64 0.22.1", 1482 + "rustls-pki-types", 1483 + ] 1484 + 1485 + [[package]] 1226 1486 name = "rustls-pki-types" 1227 1487 version = "1.7.0" 1228 1488 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1265 1525 ] 1266 1526 1267 1527 [[package]] 1528 + name = "schannel" 1529 + version = "0.1.23" 1530 + source = "registry+https://github.com/rust-lang/crates.io-index" 1531 + checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" 1532 + dependencies = [ 1533 + "windows-sys 0.52.0", 1534 + ] 1535 + 1536 + [[package]] 1268 1537 name = "scopeguard" 1269 1538 version = "1.2.0" 1270 1539 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1291 1560 ] 1292 1561 1293 1562 [[package]] 1563 + name = "security-framework" 1564 + version = "2.11.0" 1565 + source = "registry+https://github.com/rust-lang/crates.io-index" 1566 + checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" 1567 + dependencies = [ 1568 + "bitflags 2.5.0", 1569 + "core-foundation", 1570 + "core-foundation-sys", 1571 + "libc", 1572 + "security-framework-sys", 1573 + ] 1574 + 1575 + [[package]] 1576 + name = "security-framework-sys" 1577 + version = "2.11.0" 1578 + source = "registry+https://github.com/rust-lang/crates.io-index" 1579 + checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" 1580 + dependencies = [ 1581 + "core-foundation-sys", 1582 + "libc", 1583 + ] 1584 + 1585 + [[package]] 1294 1586 name = "semver" 1295 1587 version = "1.0.23" 1296 1588 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1372 1664 "mime_guess", 1373 1665 "parking_lot", 1374 1666 "percent-encoding", 1375 - "reqwest", 1667 + "reqwest 0.11.27", 1376 1668 "secrecy", 1377 1669 "serde", 1378 1670 "serde_cow", ··· 1644 1936 ] 1645 1937 1646 1938 [[package]] 1939 + name = "tokio-native-tls" 1940 + version = "0.3.1" 1941 + source = "registry+https://github.com/rust-lang/crates.io-index" 1942 + checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" 1943 + dependencies = [ 1944 + "native-tls", 1945 + "tokio", 1946 + ] 1947 + 1948 + [[package]] 1647 1949 name = "tokio-rustls" 1648 1950 version = "0.24.1" 1649 1951 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 1692 1994 "pin-project-lite", 1693 1995 "tokio", 1694 1996 ] 1997 + 1998 + [[package]] 1999 + name = "tower" 2000 + version = "0.4.13" 2001 + source = "registry+https://github.com/rust-lang/crates.io-index" 2002 + checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" 2003 + dependencies = [ 2004 + "futures-core", 2005 + "futures-util", 2006 + "pin-project", 2007 + "pin-project-lite", 2008 + "tokio", 2009 + "tower-layer", 2010 + "tower-service", 2011 + ] 2012 + 2013 + [[package]] 2014 + name = "tower-layer" 2015 + version = "0.3.2" 2016 + source = "registry+https://github.com/rust-lang/crates.io-index" 2017 + checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" 1695 2018 1696 2019 [[package]] 1697 2020 name = "tower-service" ··· 1892 2215 version = "0.1.0" 1893 2216 source = "registry+https://github.com/rust-lang/crates.io-index" 1894 2217 checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" 2218 + 2219 + [[package]] 2220 + name = "vcpkg" 2221 + version = "0.2.15" 2222 + source = "registry+https://github.com/rust-lang/crates.io-index" 2223 + checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" 1895 2224 1896 2225 [[package]] 1897 2226 name = "version_check" ··· 2190 2519 version = "0.50.0" 2191 2520 source = "registry+https://github.com/rust-lang/crates.io-index" 2192 2521 checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" 2522 + dependencies = [ 2523 + "cfg-if", 2524 + "windows-sys 0.48.0", 2525 + ] 2526 + 2527 + [[package]] 2528 + name = "winreg" 2529 + version = "0.52.0" 2530 + source = "registry+https://github.com/rust-lang/crates.io-index" 2531 + checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" 2193 2532 dependencies = [ 2194 2533 "cfg-if", 2195 2534 "windows-sys 0.48.0",
+2
Cargo.toml
··· 18 18 color-eyre = "0.6.3" 19 19 tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] } 20 20 regex = "1.10.5" 21 + reqwest = "0.12.4" 22 + lazy_static = "1.4.0"
+109
src/event_handler/code_expantion.rs
··· 1 + use color_eyre::eyre::Result; 2 + use poise::serenity_prelude::{self as serenity, Context, CreateMessage, FullEvent}; 3 + use regex::Regex; 4 + use reqwest::Client; 5 + 6 + pub async fn handle(ctx: &Context, event: &FullEvent) -> Result<()> { 7 + let pattern = 8 + r"https?:\/\/github\.com\/([\w-]+\/[\w.-]+)\/blob\/(.+?)\/(.+?)#L(\d+)[~-]?L?(\d*)"; 9 + let re = Regex::new(pattern)?; 10 + 11 + if let FullEvent::Message { new_message } = event { 12 + let content = &new_message.content; 13 + if re.captures(content.as_str()).is_some() { 14 + let mut code_blocks = Vec::new(); 15 + 16 + for caps in re.captures_iter(content) { 17 + let full_url = caps.get(0).unwrap().as_str(); 18 + let repo = caps.get(1).unwrap().as_str(); 19 + let reference = caps.get(2).unwrap().as_str(); 20 + let file = caps.get(3).unwrap().as_str(); 21 + let start_str = caps.get(4).unwrap().as_str(); 22 + let start = start_str.parse::<usize>().unwrap_or(1); 23 + let end = if let Some(end_str) = caps.get(5) { 24 + end_str.as_str().parse::<usize>().unwrap_or(start) 25 + } else { 26 + start 27 + }; 28 + 29 + let raw_url = format!( 30 + "https://raw.githubusercontent.com/{}/{}/{}", 31 + repo, reference, file 32 + ); 33 + let response = Client::new().get(&raw_url).send().await; 34 + 35 + if let Ok(response) = response { 36 + if !response.status().is_success() { 37 + println!("Failed to fetch {} contents.", full_url); 38 + continue; 39 + } 40 + 41 + let text = response.text().await?; 42 + let mut content = text 43 + .lines() 44 + .skip(start - 1) 45 + .take(end - start + 1) 46 + .collect::<Vec<&str>>() 47 + .join("\n"); 48 + 49 + let mut lines_sliced = 0; 50 + 51 + while content.len() > 1950 { 52 + let lines = content.lines().collect::<Vec<&str>>(); 53 + if lines.len() == 1 { 54 + content = content.chars().take(1950).collect(); 55 + break; 56 + } 57 + content = lines[..lines.len() - 1].join("\n"); 58 + lines_sliced += 1; 59 + } 60 + let end = end - lines_sliced; 61 + 62 + let name = format!( 63 + "{repo}@{} {file} L{start}{}", 64 + if reference.len() == 40 { 65 + &reference[..8] 66 + } else { 67 + reference 68 + }, 69 + if end > start { 70 + format!("-{}", end) 71 + } else { 72 + "".to_string() 73 + } 74 + ); 75 + 76 + let body = if lines_sliced > 0 { 77 + format!("... ({} lines not displayed)", lines_sliced) 78 + } else { 79 + "".to_string() 80 + }; 81 + 82 + let language = file.split('.').last().unwrap_or("").to_string(); 83 + code_blocks.push((name, language, content, body)); 84 + } 85 + } 86 + 87 + code_blocks.retain(|(_, _, content, _)| !content.trim().is_empty()); 88 + 89 + if !code_blocks.is_empty() { 90 + let embeds = code_blocks 91 + .into_iter() 92 + .map(|(name, language, content, body)| { 93 + serenity::CreateEmbed::default() 94 + .title(name) 95 + .description(format!("```{}\n{}\n```", language, content).to_owned()) 96 + .footer(serenity::CreateEmbedFooter::new(body)) 97 + }) 98 + .collect::<Vec<_>>(); 99 + 100 + new_message 101 + .channel_id 102 + .send_message(&ctx, CreateMessage::default().embeds(embeds)) 103 + .await?; 104 + } 105 + } 106 + } 107 + 108 + Ok(()) 109 + }
+6 -2
src/event_handler/mod.rs
··· 1 1 use color_eyre::eyre::Result; 2 2 use poise::serenity_prelude::{Context, FullEvent}; 3 3 4 + mod code_expantion; 5 + 4 6 use crate::Data; 5 7 6 - // TODO: add more events 7 - pub async fn event_handler(_ctx: &Context, event: &FullEvent, _data: &Data) -> Result<()> { 8 + // TODO: add starboard 9 + pub async fn event_handler(ctx: &Context, event: &FullEvent, _data: &Data) -> Result<()> { 8 10 if let FullEvent::Ready { data_about_bot } = event { 9 11 println!("Logged in as {}", data_about_bot.user.name); 10 12 } 13 + 14 + code_expantion::handle(ctx, event).await?; 11 15 12 16 Ok(()) 13 17 }