The content of this function can probably be split out into a few separate top-level functions. The modal initiation is repeated, so can be a function. The d&d related stuff can probably be a separate function as well.