Repo de mon bot discord en Rust(le temps que j'héberge ça chez moi)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const TOAST_TEXT: &'static str = r#"
  2. ```
  3. Toast!
  4. ______
  5. ____(( )_
  6. |'->==)) (= \
  7. | \ ||_____|_ \
  8. |[> \___________\
  9. | | | | |
  10. \ | | .--. |
  11. \ | |)---. .---' `-. .----(]|
  12. \|____________| `-' `. .' |
  13. `---' |
  14. ```
  15. "#;
  16. const COW_TEXT: &'static str = r"
  17. ```
  18. \ ^__^
  19. \ (oo)\_______
  20. (__)\ )\/\
  21. ||----w |
  22. || ||
  23. ```
  24. ";
  25. use serenity::client::Client;
  26. use serenity::framework::standard::StandardFramework;
  27. use serenity::model::channel::Message;
  28. use serenity::model::gateway::Ready;
  29. use serenity::prelude::*;
  30. use serenity::utils::MessageBuilder;
  31. use std::env;
  32. struct Handler;
  33. impl EventHandler for Handler {
  34. fn message(&self, ctx: Context, msg: Message) {
  35. // if msg.content == "!ping" {
  36. // println!("Shard {}", ctx.shard_id);
  37. //
  38. // if let Err(why) = msg.channel_id.say("Pong!") {
  39. // println!("Error sending message: {:?}", why);
  40. // }
  41. // }
  42. if msg.content == "!messageme" {
  43. if let Err(why) = msg.author.dm(|m| m.content("Hello!")) {
  44. println!("Error when direct messaging user: {:?}", why);
  45. }
  46. }
  47. if msg.content == "!ping" {
  48. let channel = match msg.channel_id.get() {
  49. Ok(channel) => channel,
  50. Err(why) => {
  51. println!("Error getting channel: {:?}", why);
  52. return;
  53. }
  54. };
  55. let response = MessageBuilder::new()
  56. .push("User ")
  57. .push_bold_safe(msg.author.name)
  58. .push(" used the 'ping' command in the ")
  59. .mention(&channel)
  60. .push(" channel")
  61. .build();
  62. if let Err(why) = msg.channel_id.say(&response) {
  63. println!("Error sending message: {:?}", why);
  64. }
  65. }
  66. }
  67. fn ready(&self, _: Context, ready: Ready) {
  68. println!("{} is connected!", ready.user.name);
  69. }
  70. }
  71. fn main() {
  72. // Login with a bot token from the environment
  73. let mut client =
  74. Client::new(&env::var("DISCORD_TOKEN").expect("token"), Handler).expect("Token missing!");
  75. client.with_framework(
  76. StandardFramework::new()
  77. .configure(|c| c.prefix("~")) // set the bot's prefix to "~"
  78. .on("about", |_, msg, _| {
  79. msg.channel_id.say("A simple test bot")?;
  80. // The `command!` macro implicitly puts an `Ok(())` at the end of each command definiton;
  81. // signifying successful execution.
  82. //
  83. // However since we're using `on` that requires a function with the definition `Fn(Context, Message, Args) -> Result<(), _>`,
  84. // we need to manually put the `Ok` ourselves.
  85. Ok(())
  86. })
  87. .cmd("ping", ping)
  88. .cmd("test", test)
  89. .cmd("multi", multiply)
  90. .cmd("add", add),
  91. );
  92. // start listening for events by starting a single shard
  93. if let Err(why) = client.start() {
  94. println!("An error occurred while running the client: {:?}", why);
  95. }
  96. }
  97. command!(ping(_context, message) {
  98. if let Err(why) = message.reply("Pong!") {
  99. println!("Error sending pong: {:?}", why);
  100. }
  101. });
  102. command!(multiply(_context, message, args) {
  103. let first = args.single::<f64>().unwrap();
  104. let second = args.single::<f64>().unwrap();
  105. let product = first * second;
  106. if let Err(why) = message.reply(&product.to_string()) {
  107. println!("Error sending product: {:?}", why);
  108. }
  109. });
  110. command!(add(_context, message, args) {
  111. let first = args.single::<f64>().unwrap();
  112. let second = args.single::<f64>().unwrap();
  113. let product = first + second;
  114. if let Err(why) = message.reply(&product.to_string()){
  115. println!("Error sending product: {:?}", why);
  116. }
  117. });
  118. command!(test(_context, message) {
  119. if let Err(why) = message.reply("Just a test") {
  120. println!("Error sending test: {:?}", why);
  121. }
  122. });