Skip to content
GitLab
Explorer
Connexion
Navigation principale
Rechercher ou aller à…
Projet
Matrix Gatcha Bot
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki externe
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Déploiement
Releases
Registre de modèles
Analyse
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté GitLab
Contribuer à GitLab
Donner votre avis
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Ce projet est archivé. Le dépôt et les autres ressources du projet sont en lecture seule.
Afficher davantage de fils d'Ariane
Kubat
Matrix Gatcha Bot
Validations
2ad38687
Vérifiée
Valider
2ad38687
rédigé
Il y a 3 ans
par
Kubat
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
WIP: Refactor bot creation
parent
ad40be18
Branches
Branches contenant la validation
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
2
Afficher les modifications d'espaces
En ligne
Côte à côte
Affichage de
2 fichiers modifiés
src/main.rs
+12
-14
12 ajouts, 14 suppressions
src/main.rs
src/matrix.rs
+129
-44
129 ajouts, 44 suppressions
src/matrix.rs
avec
141 ajouts
et
58 suppressions
src/main.rs
+
12
−
14
Voir le fichier @
2ad38687
...
...
@@ -5,25 +5,23 @@ mod config;
mod
matrix
;
mod
utils
;
use
log
::{
error
,
info
}
;
use
crate
::
cmd
::
Cmd
;
use
matrix_sdk
;
use
std
::
{
env
,
process
::
abort
}
;
use
std
::
env
;
#[tokio::main]
async
fn
main
()
->
matrix_sdk
::
Result
<
()
>
{
config
::
setup_logging
();
config
::
check_argument_or_abort
(
true
);
let
config_file
=
env
::
args
()
.last
()
.unwrap
();
info!
(
"Using config file: {}"
,
config_file
);
return
match
config
::
from_file
(
&
config_file
)
{
Ok
(
config
)
=>
matrix
::
connect_and_handle
(
config
)
.await
,
Err
(
e
)
=>
{
error!
(
"Failed to read config file '{}' with error: {}"
,
config_file
,
e
);
abort
();
}
};
let
basic_cmds
=
vec!
[
Cmd
::
new_simple
(
format!
(
"ping"
),
0
,
|
_x
|
true
),
Cmd
::
new_echo
(
format!
(
"bot_name"
),
0
,
|
_x
|
format!
(
"toto"
)),
];
matrix
::
Bot
::
new
(
env
::
args
()
.last
()
.unwrap
())
.connect
()
.await
?
.add_package
(
format!
(
"basic"
),
basic_cmds
)
.listen
()
.await
}
Ce diff est replié.
Cliquez pour l'agrandir.
src/matrix.rs
+
129
−
44
Voir le fichier @
2ad38687
use
crate
::{
cmd
::
package
::
CmdPackage
,
cmd
::
Cmd
,
config
::
Config
,
return_if
};
use
crate
::{
cmd
::
package
::
CmdPackage
,
cmd
::
Cmd
,
config
::{
from_file
as
config_from_file
,
Config
},
return_if
,
};
use
futures
::
future
::
join_all
;
use
log
::{
error
,
info
,
warn
};
use
matrix_sdk
::{
...
...
@@ -158,6 +163,85 @@ macro_rules! register_handler_if_enabled {
}};
}
pub
struct
Bot
{
config
:
Config
,
client
:
Option
<
Client
>
,
}
impl
Bot
{
pub
fn
new
(
config_file
:
String
)
->
Bot
{
match
config_from_file
(
&
config_file
)
{
Err
(
e
)
=>
{
error!
(
"Failed to read config file '{}' with error: {}"
,
config_file
,
e
);
abort
();
}
Ok
(
config
)
=>
{
info!
(
"Using config file: {}"
,
config_file
);
Bot
{
config
:
config
,
client
:
None
,
}
}
}
}
pub
async
fn
connect
(
&
mut
self
)
->
Result
<&
mut
Self
>
{
let
alice
=
UserId
::
try_from
(
self
.config.user_name
.clone
())
?
;
let
client
=
Client
::
new
(
self
.config.homeserver_url
.clone
())
?
;
client
.login
(
alice
.localpart
(),
self
.config.user_password
.as_str
(),
None
,
Some
(
self
.config.display_name
.as_str
()),
)
.await
?
;
info!
(
"Logged as: {}"
,
alice
);
client
.sync_once
(
SyncSettings
::
default
())
.await
?
;
startup_and_log_rooms
(
&
client
)
.await
;
register_handler_if_enabled!
(
client
,
self
.config.handle_state_member
,
on_state_member
,
"state member"
);
client
.register_event_handler
(
on_msg_room_event
)
.await
;
client
.register_event_handler
(
on_msg_room_reaction
)
.await
;
self
.client
=
Some
(
client
);
return
Ok
(
self
);
}
pub
async
fn
listen
(
&
self
)
->
Result
<
()
>
{
match
&
self
.client
{
None
=>
abort
(),
Some
(
client
)
=>
{
info!
(
"Entering sync loop"
);
let
token
=
client
.sync_token
()
.await
.unwrap
();
client
.sync
(
SyncSettings
::
default
()
.token
(
token
))
.await
;
Ok
(())
}
}
}
pub
fn
add_package
(
&
self
,
name
:
String
,
cmds
:
Vec
<
Cmd
>
)
->
&
Self
{
let
pkg
=
match
self
.config
.section
(
"basic"
)
{
Some
(
section
)
=>
CmdPackage
::
from_config
(
&
section
,
cmds
),
None
=>
{
abort
();
}
};
GLOBAL_PKG
.lock
()
.unwrap
()
.push
(
pkg
);
&
self
}
pub
async
fn
connect_and_handle
(
config
:
Config
)
->
Result
<
()
>
{
// TODO: Refactor
let
pkg_commands_vec
=
vec!
[
...
...
@@ -203,3 +287,4 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
client
.sync
(
settings
)
.await
;
Ok
(())
}
}
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter