Skip to content

Commit

Permalink
Add a bit of redis stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNorom committed Feb 8, 2022
1 parent 3790839 commit 95374a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# e621 nombot

## Redis format

The key `BOT_PREFIX::GUILD_ID::CHANNEL_ID::CONF` points to a hashmap of possible configuration parameters.
Current config parameters are:
- tags (string):
the search query with each tag separated by spaces
- timeout (int):
if random_timeout is `false`, amount of minutes to wait till the next post.
if random_timeout is `true`, the maximum amount of minutes a timeout is choosen from.
- random_timeout (bool):
if a random timeout should be used
- nsfw (string):
decides if queries are done against e621 or e926.
if "sfw" e926.net is used.
if "nsfw" e621.net is used.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum Error {
Rs621(#[from] rs621::error::Error),
#[error("serenity error")]
Serenity(#[from] poise::serenity_prelude::Error),
#[error("redis error")]
Redis(#[from] fred::error::RedisError),
#[error("Command must be run in guild")]
CommandNotRunInGuild,
#[error("No tags have been set")]
Expand Down
23 changes: 20 additions & 3 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fmt::Debug, sync::Arc};

use dashmap::DashMap;
use fred::{types::{RedisConfig, ReconnectPolicy}, client::RedisClient, prelude::RedisError};
use poise::{
serenity_prelude::{ChannelId, Context, GuildId, Ready},
Framework,
Expand All @@ -26,6 +27,8 @@ pub struct Data {
e926_client: Arc<Client>,
/// serenity context
context: Context,
/// redis db handle
redis: RedisClient,
/// when a shutdown command is executed, this signal
/// will be switched to true, signaling the shutdown functions
/// to run
Expand All @@ -44,7 +47,7 @@ impl Debug for Data {
}

impl Data {
fn new(context: Context, shutdown_sender: Sender<bool>) -> Result<Self, crate::Error> {
async fn new(context: Context, shutdown_sender: Sender<bool>) -> Result<Self, crate::Error> {
let user_agent = "CutePokebot/0.1.0 (norom)";

let (e6_client, e9_client) =
Expand All @@ -63,16 +66,30 @@ impl Data {
Client::new("https://e926.net", &user_agent)?,
)
};

let redis = async {
let config = RedisConfig::default();
let policy = ReconnectPolicy::new_exponential(0, 100, 30_000, 2);
let client = RedisClient::new(config);
client.connect(Some(policy)).await??;
client.wait_for_connect().await?;
Ok::<RedisClient, RedisError>(client)
}.await?;

Ok(Self {
guild_configurations: Arc::new(DashMap::new()),
e621_client: Arc::new(e6_client),
e926_client: Arc::new(e9_client),
context,
redis,
shutdown_sender: Arc::new(shutdown_sender),
})
}

async fn restore_from_db(&self) -> Result<(), crate::Error> {

Ok(())
}
/// Start sending images to channel inside guild
#[instrument(skip(self))]
pub async fn start(&self, guild: GuildId, channel: ChannelId) {
Expand Down Expand Up @@ -214,7 +231,7 @@ impl Data {
Ok(post?)
}

/// Get a reference to the data's context.
/// Get a reference to the data's serenity context.
pub fn context(&self) -> &Context {
&self.context
}
Expand All @@ -226,7 +243,7 @@ pub async fn setup<U, E>(
_framework: &Framework<U, E>,
shutdown_sender: Sender<bool>,
) -> Result<crate::Data, crate::Error> {
let data = Data::new(context.clone(), shutdown_sender)?;
let data = Data::new(context.clone(), shutdown_sender).await?;
let _ = tokio::spawn(delete_button_listener(context.clone()));
Ok(data)
}

0 comments on commit 95374a7

Please sign in to comment.