├── .gitignore ├── README.md └── script.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | config.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # text-generation-webui-discord_bot 2 | 3 | Discord integration for the oobabooga's [text-generation-webui](https://github.com/oobabooga/text-generation-webui/) 4 | 5 | Currently it only sends any response from the chatbot to a discord Webhook of your choosing 6 | 7 | ![image](https://user-images.githubusercontent.com/45816945/234896222-532ef597-3e26-48cc-8af2-7df33d471e1b.png) 8 | 9 | Simply create a Webhook in Discord following [this tutorial]([url](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks)) and paste the webhook URL under the chat box that will show after the plugin is enabled 10 | 11 | 12 | 13 | ## Installation 14 | The extension can be installed by cloning this repository inside the ../text-generation-webui/extensions folder: 15 | 16 | ```bash 17 | cd {PATH_TO_text-generation-webui/}; 18 | cd extensions; 19 | git clone https://github.com/ChobPT/text-generation-webui-discord_bot.git discord_bot 20 | ``` 21 | 22 | Any suggestions feel free to PR, 23 | 24 | Enjoy 25 | 26 | -------------------------------------------------------------------------------- /script.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | import requests 3 | import json 4 | from pathlib import Path 5 | 6 | 7 | 8 | all_params=[] 9 | 10 | def clean_path(base_path: str, path: str): 11 | """"Strips unusual symbols and forcibly builds a path as relative to the intended directory.""" 12 | # TODO: Probably could do with a security audit to guarantee there's no ways this can be bypassed to target an unwanted path. 13 | # Or swap it to a strict whitelist of [a-zA-Z_0-9] 14 | path = path.replace('\\', '/').replace('..', '_') 15 | if base_path is None: 16 | return path 17 | 18 | return f'{Path(base_path).absolute()}/{path}' 19 | def load_params(): 20 | 21 | f_name= clean_path('extensions/discord_bot', f'config.json') 22 | if Path(f_name).is_file(): 23 | with open(f_name, 'r', encoding='utf-8') as format_file: 24 | all_params: dict[str, str] = json.load(format_file) 25 | return all_params 26 | 27 | 28 | def output_modifier(string): 29 | global all_params 30 | all_params=load_params() 31 | # Replace the webhook URL with your own 32 | webhook_url = all_params 33 | 34 | # Define the message payload 35 | payload = { 36 | "content": string.replace("#", "") 37 | } 38 | 39 | # Send the POST request to the webhook URL with the payload 40 | response = requests.post(webhook_url, json=payload) 41 | 42 | # Check the response status code 43 | if response.status_code == 204: 44 | print("Message sent successfully.") 45 | else: 46 | print(f"Error: {response.status_code}") 47 | 48 | return string 49 | def ui(): 50 | global all_params 51 | all_params=load_params() 52 | # JS to initialize the params for JS modules, we need to place this here because the params are loaded from settings.json when ui() is called 53 | 54 | with gr.Accordion('Discord Bot - Settings', elem_id='discord-bot-settings_accordion', open=True,): 55 | # Settings warning message and accordion style 56 | webhook_url = gr.Textbox(label='Webhook URL', info='The webhook URL for your bot',value=all_params) 57 | gr.HTML(value=f''' 58 | 59 |

Please wait for text generation to end before changing settings

60 | ''') 61 | all_params = [webhook_url] 62 | webhook_url.change(save_settings,all_params) 63 | 64 | def save_settings(all_params): 65 | with open(clean_path('extensions/discord_bot', f'config.json'), 'w', encoding='utf-8') as formatFile: 66 | json.dump(all_params, formatFile, ensure_ascii=False, indent=4) 67 | --------------------------------------------------------------------------------