From 36963e3f9bd4132299c2dc90f8bfd7f2285909de Mon Sep 17 00:00:00 2001 From: GoldOrange261 <63157008+GoldOrange261@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:54:15 +0800 Subject: [PATCH 1/8] feat: set which action type will be send --- cogs/notification.py | 13 +++++++++---- src/db_function/init_db.py | 2 +- src/notification/account_tracker.py | 3 ++- src/notification/utils.py | 8 ++++++++ src/utils.py | 2 ++ 5 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/notification/utils.py create mode 100644 src/utils.py diff --git a/cogs/notification.py b/cogs/notification.py index 1de10d4..58cd1e6 100644 --- a/cogs/notification.py +++ b/cogs/notification.py @@ -9,6 +9,7 @@ from src.log import setup_logger from src.notification.account_tracker import AccountTracker +from src.notification.utils import format_enable_type from src.discord_ui.modal import CustomizeMsgModal from src.permission import ADMINISTRATOR from configs.load_configs import configs @@ -27,7 +28,7 @@ def __init__(self, bot): customize_group = app_commands.Group(name='customize', description='Customize something', default_permissions=ADMINISTRATOR) @add_group.command(name='notifier') - async def notifier(self, itn : discord.Interaction, username: str, channel: discord.TextChannel, mention: discord.Role = None): + async def notifier(self, itn : discord.Interaction, username: str, channel: discord.TextChannel, mention: discord.Role = None, retweet: bool = True, quote: bool = True): """Add a twitter user to specific channel on your server. Parameters @@ -38,6 +39,10 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc The channel to which the bot delivers notifications. mention: discord.Role The role to mention when notifying. + retweet: bool + Whether to enable notifications for retweets + quote: bool + Whether to enable notifications for quoted tweets """ await itn.response.defer(ephemeral=True) @@ -63,10 +68,10 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc if match_user == None: cursor.execute('INSERT INTO user (id, username, lastest_tweet) VALUES (?, ?, ?)', (str(new_user.id), username, datetime.utcnow().replace(tzinfo=timezone.utc).strftime('%Y-%m-%d %H:%M:%S%z'))) cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('INSERT INTO notification (user_id, channel_id, role_id) VALUES (?, ?, ?)', (str(new_user.id), str(channel.id), roleID)) + cursor.execute('INSERT INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (str(new_user.id), str(channel.id), roleID, format_enable_type(retweet, quote))) else: cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id) VALUES (?, ?, ?)', (match_user['id'], str(channel.id), roleID)) + cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, format_enable_type(retweet, quote))) cursor.execute('UPDATE user SET enabled = 1 WHERE id = ?', (match_user['id'],)) @@ -76,7 +81,7 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc else: log.warning(f'unable to turn on notifications for {username}') else: cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id) VALUES (?, ?, ?)', (match_user['id'], str(channel.id), roleID)) + cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, format_enable_type(retweet, quote))) conn.commit() conn.close() diff --git a/src/db_function/init_db.py b/src/db_function/init_db.py index c36d2db..1aaa284 100644 --- a/src/db_function/init_db.py +++ b/src/db_function/init_db.py @@ -8,7 +8,7 @@ def init_db(): cursor.executescript(""" CREATE TABLE IF NOT EXISTS user (id TEXT PRIMARY KEY, username TEXT, lastest_tweet TEXT, enabled INTEGER DEFAULT 1); CREATE TABLE IF NOT EXISTS channel (id TEXT PRIMARY KEY, server_id TEXT); - CREATE TABLE IF NOT EXISTS notification (user_id TEXT, channel_id TEXT, role_id TEXT, enabled INTEGER DEFAULT 1, customized_msg TEXT DEFAULT NULL, FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (channel_id) REFERENCES channel (id), PRIMARY KEY(user_id, channel_id)); + CREATE TABLE IF NOT EXISTS notification (user_id TEXT, channel_id TEXT, role_id TEXT, enabled INTEGER DEFAULT 1, enable_type TEXT DEFAULT 11, customized_msg TEXT DEFAULT NULL, FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (channel_id) REFERENCES channel (id), PRIMARY KEY(user_id, channel_id)); """) conn.commit() conn.close() \ No newline at end of file diff --git a/src/notification/account_tracker.py b/src/notification/account_tracker.py index 4d858c8..8c0a515 100644 --- a/src/notification/account_tracker.py +++ b/src/notification/account_tracker.py @@ -9,6 +9,7 @@ from src.log import setup_logger from src.notification.display_tools import gen_embed, get_action from src.notification.get_tweets import get_tweets +from src.notification.utils import is_match_type from src.db_function.db_executor import execute from configs.load_configs import configs @@ -60,7 +61,7 @@ async def notification(self, username): log.info(f'find a new tweet from {username}') for data in cursor.execute('SELECT * FROM notification WHERE user_id = ? AND enabled = 1', (user['id'],)): channel = self.bot.get_channel(int(data['channel_id'])) - if channel != None: + if channel != None and is_match_type(tweet, data['enable_type']): try: mention = f"{channel.guild.get_role(int(data['role_id'])).mention} " if data['role_id'] != '' else '' author, action, url = tweet.author.name, get_action(tweet), tweet.url diff --git a/src/notification/utils.py b/src/notification/utils.py new file mode 100644 index 0000000..7989af4 --- /dev/null +++ b/src/notification/utils.py @@ -0,0 +1,8 @@ +from src.utils import bool_to_str + +def is_match_type(tweet, enable_type: str): + tweet_type = 0 if tweet.is_retweet else 1 if tweet.is_quoted else -1 + return tweet_type == -1 or enable_type[tweet_type] == '1' + +def format_enable_type(retweet: bool, quote: bool): + return bool_to_str(retweet) + bool_to_str(quote) \ No newline at end of file diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..bad730a --- /dev/null +++ b/src/utils.py @@ -0,0 +1,2 @@ +def bool_to_str(boo: bool): + return '1' if boo else '0' \ No newline at end of file From 48f68a49252b9c0f66ed6df16b60ea345e259443 Mon Sep 17 00:00:00 2001 From: FuseFairy Date: Wed, 10 Jul 2024 20:19:20 +0800 Subject: [PATCH 2/8] feat: support fx twitter url --- configs.yml | 3 ++- src/notification/account_tracker.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/configs.yml b/configs.yml index abeeba5..6d2c57a 100644 --- a/configs.yml +++ b/configs.yml @@ -5,4 +5,5 @@ tweets_updater_retry_delay: 300 tasks_monitor_check_period: 60 tasks_monitor_log_period: 14400 auto_turn_off_notification: true -auto_unfollow: true \ No newline at end of file +auto_unfollow: true +use_fx: True \ No newline at end of file diff --git a/src/notification/account_tracker.py b/src/notification/account_tracker.py index 8c0a515..858bc4a 100644 --- a/src/notification/account_tracker.py +++ b/src/notification/account_tracker.py @@ -64,10 +64,20 @@ async def notification(self, username): if channel != None and is_match_type(tweet, data['enable_type']): try: mention = f"{channel.guild.get_role(int(data['role_id'])).mention} " if data['role_id'] != '' else '' - author, action, url = tweet.author.name, get_action(tweet), tweet.url + author, action = tweet.author.name, get_action(tweet) + + if configs['use_fx']: + url = f"https://fixupx.com/{tweet.author.username}/status/{tweet.id}" + else: + url = tweet.url + msg = data['customized_msg'] if data['customized_msg'] else "{mention}**{author}** just {action} here: \n{url}" msg = msg.format(mention=mention, author=author, action=action, url=url) - await channel.send(msg, file = discord.File('images/twitter.png', filename='twitter.png'), embeds = gen_embed(tweet)) + + if configs['use_fx']: + await channel.send(msg) + else: + await channel.send(msg, file = discord.File('images/twitter.png', filename='twitter.png'), embeds = gen_embed(tweet)) except Exception as e: if not isinstance(e, discord.errors.Forbidden): log.error(f'an unexpected error occurred at {channel.mention} while sending notification') From 31416c7f9b5533b0babf8fe27c2c354e9b030fa3 Mon Sep 17 00:00:00 2001 From: GoldOrange261 <63157008+GoldOrange261@users.noreply.github.com> Date: Thu, 11 Jul 2024 08:46:36 +0800 Subject: [PATCH 3/8] docs: use_fx is set to false by default --- configs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs.yml b/configs.yml index 6d2c57a..c15b029 100644 --- a/configs.yml +++ b/configs.yml @@ -6,4 +6,4 @@ tasks_monitor_check_period: 60 tasks_monitor_log_period: 14400 auto_turn_off_notification: true auto_unfollow: true -use_fx: True \ No newline at end of file +use_fx: false \ No newline at end of file From f1e51c2fd2c2cc1b6e09f2840f321d583514c5de Mon Sep 17 00:00:00 2001 From: GoldOrange261 <63157008+GoldOrange261@users.noreply.github.com> Date: Thu, 11 Jul 2024 10:10:22 +0800 Subject: [PATCH 4/8] feat: list users command now can display retweet/quote is enabled or not --- cogs/list_users.py | 10 +++++++--- src/utils.py | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cogs/list_users.py b/cogs/list_users.py index fc7acf6..92d53f8 100644 --- a/cogs/list_users.py +++ b/cogs/list_users.py @@ -4,8 +4,12 @@ import sqlite3 import os +from src.utils import str_to_bool as stb from src.permission import ADMINISTRATOR +CHECK = '\u2705' +XMARK = '\u274C' + class ListUsers(Cog_Extension): list_group = app_commands.Group(name='list', description='List something', default_permissions=ADMINISTRATOR) @@ -21,7 +25,7 @@ async def list_users(self, itn: discord.Interaction): cursor = conn.cursor() cursor.execute(""" - SELECT user.username, channel.id, notification.role_id + SELECT user.username, channel.id, notification.role_id, notification.enable_type FROM user JOIN notification ON user.id = notification.user_id @@ -34,8 +38,8 @@ async def list_users(self, itn: discord.Interaction): conn.close() formatted_data = [ - f"{i+1}. ```{username}``` <#{channel_id}> <@&{role_id}>" if role_id else f"{i+1}. ```{username}``` <#{channel_id}>" - for i, (username, channel_id, role_id) in enumerate(user_channel_role_data) + f"{i+1}. ```{username}``` <#{channel_id}>{f' <@&{role_id}>' if role_id else ''} {CHECK if stb(enable_type[0]) else XMARK}retweet {CHECK if stb(enable_type[1]) else XMARK}quote" + for i, (username, channel_id, role_id, enable_type) in enumerate(user_channel_role_data) ] if not formatted_data: diff --git a/src/utils.py b/src/utils.py index bad730a..53c91e0 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,2 +1,5 @@ def bool_to_str(boo: bool): - return '1' if boo else '0' \ No newline at end of file + return '1' if boo else '0' + +def str_to_bool(string: str): + return False if string == '0' else True \ No newline at end of file From 07eb9a4c9dd9930d821bc0a68208f535235fba9a Mon Sep 17 00:00:00 2001 From: GoldOrange261 <63157008+GoldOrange261@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:32:38 +0800 Subject: [PATCH 5/8] feat: default message templates can now be set in configs --- configs.yml | 5 ++++- src/notification/account_tracker.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/configs.yml b/configs.yml index c15b029..493b6e9 100644 --- a/configs.yml +++ b/configs.yml @@ -6,4 +6,7 @@ tasks_monitor_check_period: 60 tasks_monitor_log_period: 14400 auto_turn_off_notification: true auto_unfollow: true -use_fx: false \ No newline at end of file +use_fx: false +default_message: | + {mention}**{author}** just {action} here: + {url} \ No newline at end of file diff --git a/src/notification/account_tracker.py b/src/notification/account_tracker.py index 858bc4a..9c0c79d 100644 --- a/src/notification/account_tracker.py +++ b/src/notification/account_tracker.py @@ -71,7 +71,7 @@ async def notification(self, username): else: url = tweet.url - msg = data['customized_msg'] if data['customized_msg'] else "{mention}**{author}** just {action} here: \n{url}" + msg = data['customized_msg'] if data['customized_msg'] else configs['default_message'] msg = msg.format(mention=mention, author=author, action=action, url=url) if configs['use_fx']: From e923b26689b0fdaddfd947b4253e63bbb72deca1 Mon Sep 17 00:00:00 2001 From: Yuuzi261 Date: Wed, 31 Jul 2024 16:12:27 +0800 Subject: [PATCH 6/8] feat: enable_type can now be selected directly through the command --- cogs/notification.py | 17 ++++++++--------- src/notification/utils.py | 5 +---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/cogs/notification.py b/cogs/notification.py index 58cd1e6..cf7e73f 100644 --- a/cogs/notification.py +++ b/cogs/notification.py @@ -9,7 +9,6 @@ from src.log import setup_logger from src.notification.account_tracker import AccountTracker -from src.notification.utils import format_enable_type from src.discord_ui.modal import CustomizeMsgModal from src.permission import ADMINISTRATOR from configs.load_configs import configs @@ -28,7 +27,9 @@ def __init__(self, bot): customize_group = app_commands.Group(name='customize', description='Customize something', default_permissions=ADMINISTRATOR) @add_group.command(name='notifier') - async def notifier(self, itn : discord.Interaction, username: str, channel: discord.TextChannel, mention: discord.Role = None, retweet: bool = True, quote: bool = True): + @app_commands.choices(enable_type=[app_commands.Choice(name='All (default)', value='11'), app_commands.Choice(name='Tweet & Retweet Only', value='10'), app_commands.Choice(name='Tweet & Quote Only', value='01'), app_commands.Choice(name='Tweet Only', value='00')]) + @app_commands.rename(enable_type='type') + async def notifier(self, itn : discord.Interaction, username: str, channel: discord.TextChannel, mention: discord.Role = None, enable_type: str = '11'): """Add a twitter user to specific channel on your server. Parameters @@ -39,10 +40,8 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc The channel to which the bot delivers notifications. mention: discord.Role The role to mention when notifying. - retweet: bool - Whether to enable notifications for retweets - quote: bool - Whether to enable notifications for quoted tweets + type: str + Whether to enable notifications for retweets & quotes. """ await itn.response.defer(ephemeral=True) @@ -68,10 +67,10 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc if match_user == None: cursor.execute('INSERT INTO user (id, username, lastest_tweet) VALUES (?, ?, ?)', (str(new_user.id), username, datetime.utcnow().replace(tzinfo=timezone.utc).strftime('%Y-%m-%d %H:%M:%S%z'))) cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('INSERT INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (str(new_user.id), str(channel.id), roleID, format_enable_type(retweet, quote))) + cursor.execute('INSERT INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (str(new_user.id), str(channel.id), roleID, enable_type)) else: cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, format_enable_type(retweet, quote))) + cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, enable_type)) cursor.execute('UPDATE user SET enabled = 1 WHERE id = ?', (match_user['id'],)) @@ -81,7 +80,7 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc else: log.warning(f'unable to turn on notifications for {username}') else: cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id)) - cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, format_enable_type(retweet, quote))) + cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, enable_type) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, enable_type)) conn.commit() conn.close() diff --git a/src/notification/utils.py b/src/notification/utils.py index 7989af4..1db0b80 100644 --- a/src/notification/utils.py +++ b/src/notification/utils.py @@ -2,7 +2,4 @@ def is_match_type(tweet, enable_type: str): tweet_type = 0 if tweet.is_retweet else 1 if tweet.is_quoted else -1 - return tweet_type == -1 or enable_type[tweet_type] == '1' - -def format_enable_type(retweet: bool, quote: bool): - return bool_to_str(retweet) + bool_to_str(quote) \ No newline at end of file + return tweet_type == -1 or enable_type[tweet_type] == '1' \ No newline at end of file From 82e68122f71efb40316066306b6eda73558ef7c6 Mon Sep 17 00:00:00 2001 From: Yuuzi261 Date: Wed, 31 Jul 2024 16:40:12 +0800 Subject: [PATCH 7/8] docs: update README --- README.md | 5 +++++ README_zh.md | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 8a4ca75..7f4431c 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Tweetcord is a discord bot that uses the tweety-ns module to let you receive twe | `username` | str | The username of the twitter user you want to turn on notifications for | | `channel` | discord.TextChannel | The channel to which the bot delivers notifications | | `mention` | discord.Role | The role to mention when notifying | +| `type` | str | Whether to enable notifications for retweets & quotes | 👉 `/remove notifier` `username` `channel` @@ -232,6 +233,10 @@ tasks_monitor_check_period: 60 # Interval at which to check if each tasks i tasks_monitor_log_period: 14400 # Interval at which to output the list of currently running tasks to the execution log. auto_turn_off_notification: true # (v0.4 or later) If all notifications for a user are disabled, decide whether to unfollow the user. auto_unfollow: true # (v0.4 or later) If all notifications for a user is disabled, decide whether to disable notification for the user (twitter side). +use_fx: false # (v0.4.1 or later) Whether to use FxTwitter to embed content instead of using the built-in embed +default_message: | # (v0.4.1 or later) Set default message format globally + {mention}**{author}** just {action} here: + {url} ``` ### 3. Run and invite the bot to your server diff --git a/README_zh.md b/README_zh.md index f2c46f5..3a01329 100644 --- a/README_zh.md +++ b/README_zh.md @@ -42,6 +42,7 @@ Tweetcord是一個discord機器人,它使用tweety-ns模組讓你在discord上 | `username` | str | 你想要開啟通知的Twitter用戶的用戶名 | | `channel` | discord.TextChannel | 機器人發送通知的頻道 | | `mention` | discord.Role | 通知時提及的身分組 | +| `type` | str | 設定是否啟用轉推和引用的通知 | 👉 `/remove notifier` `username` `channel` @@ -232,6 +233,10 @@ tasks_monitor_check_period: 60 # 檢查每個任務是否正常運行的間 tasks_monitor_log_period: 14400 # 將當前運行中的任務列表輸出到執行日誌的間隔。 auto_turn_off_notification: true # (v0.4或更新版本) 如果某個使用者的所有通知都已停用,決定是否取消追蹤該使用者。 auto_unfollow: true # (v0.4或更新版本) 如果某個使用者的所有通知都已停用,決定是否停用該使用者的通知(Twitter端)。 +use_fx: false # (v0.4.1或更新版本) 是否使用FixTweet來嵌入內容而不是使用內建的嵌入 +default_message: | # (v0.4.1或更新版本) 全域設定預設的訊息格式 + {mention}**{author}** just {action} here: + {url} ``` ### 3. 運行機器人並邀請至你的伺服器 From cea165f45d579228eccff9d045eb012702f76dc8 Mon Sep 17 00:00:00 2001 From: Yuuzi261 Date: Wed, 31 Jul 2024 17:56:50 +0800 Subject: [PATCH 8/8] docs: update README and add UPGRADE_GUIDE --- README.md | 124 +-------------- README_zh.md | 124 +-------------- UPGRADE_GUIDE.md | 383 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 389 insertions(+), 242 deletions(-) create mode 100644 UPGRADE_GUIDE.md diff --git a/README.md b/README.md index 7f4431c..b5c2375 100644 --- a/README.md +++ b/README.md @@ -35,14 +35,14 @@ Tweetcord is a discord bot that uses the tweety-ns module to let you receive twe -👉 `/add notifier` `username` `channel` | `mention` +👉 `/add notifier` `username` `channel` | `mention` `type` | parameters | types | descriptions | | --------- | ----- | ----------- | | `username` | str | The username of the twitter user you want to turn on notifications for | | `channel` | discord.TextChannel | The channel to which the bot delivers notifications | | `mention` | discord.Role | The role to mention when notifying | -| `type` | str | Whether to enable notifications for retweets & quotes | +| `type` | str | Whether to enable notifications for retweets & quotes _(new in 0.4.1)_ | 👉 `/remove notifier` `username` `channel` @@ -90,125 +90,7 @@ In certain operating systems, you may need to use the command `pip3` instead of **📢This tutorial is suitable for version 0.3.2 or later. (Recommended: 0.3.5 or later)** -
- 📌click here to upgrade from 0.3.5 to 0.4 - -⚠️Before everything starts you must upgrade the version of `tweety-ns` to `1.0.9.2` first and download or pull the new code from this repo. - -Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade version` to upgrade. This cog can be removed after the upgrade is completed. - -```py -import discord -from discord import app_commands -from core.classes import Cog_Extension -import sqlite3 -import os - -from src.permission import ADMINISTRATOR - -class Upgrade(Cog_Extension): - - upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) - - @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4') - async def upgrade(self, itn: discord.Interaction): - - await itn.response.defer(ephemeral=True) - - conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") - cursor = conn.cursor() - - try: - cursor.executescript(""" - ALTER TABLE user ADD enabled INTEGER DEFAULT 1; - ALTER TABLE notification ADD customized_msg TEXT DEFAULT NULL; - """) - await itn.followup.send('successfully upgrade to 0.4, you can remove this cog and reboot the bot.') - except: - await itn.followup.send('upgrading to 0.4 failed, please try again or contact the author.') - - -async def setup(bot): - await bot.add_cog(Upgrade(bot)) -``` - -
- -
- 📌click here to upgrade from 0.3.4 to 0.3.5 - -Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade` to upgrade. This cog can be removed after the upgrade is completed. - -```py -import discord -from discord import app_commands -from core.classes import Cog_Extension -import sqlite3 -import os - -from src.log import setup_logger -from src.permission_check import is_administrator - -log = setup_logger(__name__) - -class Upgrade(Cog_Extension): - - @is_administrator() - @app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5') - async def upgrade(self, itn: discord.Interaction): - - await itn.response.defer(ephemeral=True) - - conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") - cursor = conn.cursor() - - cursor.executescript('ALTER TABLE channel ADD server_id TEXT') - - cursor.execute('SELECT id FROM channel') - channels = cursor.fetchall() - - for c in channels: - try: - channel = self.bot.get_channel(int(c[0])) - cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id)) - except: - log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.') - - - conn.commit() - conn.close() - - await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.') - - -async def setup(bot): - await bot.add_cog(Upgrade(bot)) -``` - -
- -
- 📌click here to upgrade from 0.3.3 to 0.3.4 - -Because the database structure has been updated, you must use the following code to update the database structure. - -```py -from dotenv import load_dotenv -import os -import sqlite3 - -load_dotenv() - -conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") -cursor = conn.cursor() - -cursor.execute('ALTER TABLE notification ADD enabled INTEGER DEFAULT 1') - -conn.commit() -conn.close() -``` - -
+### [⬆️View Version Upgrade Guides](./UPGRADE_GUIDE.md) ### 1. Create and configure the .env file diff --git a/README_zh.md b/README_zh.md index 3a01329..89fe4d0 100644 --- a/README_zh.md +++ b/README_zh.md @@ -35,14 +35,14 @@ Tweetcord是一個discord機器人,它使用tweety-ns模組讓你在discord上 -👉 `/add notifier` `username` `channel` | `mention` +👉 `/add notifier` `username` `channel` | `mention` `type` | 參數 | 類型 | 描述 | | --------- | ----- | ----------- | | `username` | str | 你想要開啟通知的Twitter用戶的用戶名 | | `channel` | discord.TextChannel | 機器人發送通知的頻道 | | `mention` | discord.Role | 通知時提及的身分組 | -| `type` | str | 設定是否啟用轉推和引用的通知 | +| `type` | str | 設定是否啟用轉推和引用的通知 _(0.4.1版本的新功能)_ | 👉 `/remove notifier` `username` `channel` @@ -90,125 +90,7 @@ pip install -r requirements.txt **📢本教學適用於0.3.2或更高版本。(建議:0.3.5或更高版本)** -
- 📌0.3.5升級到0.4請點這裡 - -⚠️在一切開始之前請先更新 `tweety-ns` 至 `1.0.9.2` 版本並且從這個repo下載或拉取新的程式碼。 - -在 `cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade version` 進行升級。升級結束後可以移除這個cog。 - -```py -import discord -from discord import app_commands -from core.classes import Cog_Extension -import sqlite3 -import os - -from src.permission import ADMINISTRATOR - -class Upgrade(Cog_Extension): - - upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) - - @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4') - async def upgrade(self, itn: discord.Interaction): - - await itn.response.defer(ephemeral=True) - - conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") - cursor = conn.cursor() - - try: - cursor.executescript(""" - ALTER TABLE user ADD enabled INTEGER DEFAULT 1; - ALTER TABLE notification ADD customized_msg TEXT DEFAULT NULL; - """) - await itn.followup.send('successfully upgrade to 0.4, you can remove this cog and reboot the bot.') - except: - await itn.followup.send('upgrading to 0.4 failed, please try again or contact the author.') - - -async def setup(bot): - await bot.add_cog(Upgrade(bot)) -``` - -
- -
- 📌0.3.4升級到0.3.5請點這裡 - -在 `cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade` 進行升級。升級結束後可以移除這個cog。 - -```py -import discord -from discord import app_commands -from core.classes import Cog_Extension -import sqlite3 -import os - -from src.log import setup_logger -from src.permission_check import is_administrator - -log = setup_logger(__name__) - -class Upgrade(Cog_Extension): - - @is_administrator() - @app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5') - async def upgrade(self, itn: discord.Interaction): - - await itn.response.defer(ephemeral=True) - - conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") - cursor = conn.cursor() - - cursor.executescript('ALTER TABLE channel ADD server_id TEXT') - - cursor.execute('SELECT id FROM channel') - channels = cursor.fetchall() - - for c in channels: - try: - channel = self.bot.get_channel(int(c[0])) - cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id)) - except: - log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.') - - - conn.commit() - conn.close() - - await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.') - - -async def setup(bot): - await bot.add_cog(Upgrade(bot)) -``` - -
- -
- 📌0.3.3升級到0.3.4請點這裡 - -因為資料庫結構更新因此必須使用以下程式碼更新資料庫結構。 - -```py -from dotenv import load_dotenv -import os -import sqlite3 - -load_dotenv() - -conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") -cursor = conn.cursor() - -cursor.execute('ALTER TABLE notification ADD enabled INTEGER DEFAULT 1') - -conn.commit() -conn.close() -``` - -
+### [⬆️查看歷史版本升級指南](./UPGRADE_GUIDE.md) ### 1. 創建並配置.env文件 diff --git a/UPGRADE_GUIDE.md b/UPGRADE_GUIDE.md new file mode 100644 index 0000000..e03991d --- /dev/null +++ b/UPGRADE_GUIDE.md @@ -0,0 +1,383 @@ +## English + +
+ ⬆️click here to upgrade from 0.4 to 0.4.1 + +Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade version` to upgrade. This cog can be removed after the upgrade is completed. + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.permission import ADMINISTRATOR + +class Upgrade(Cog_Extension): + + upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) + + @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4.1') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + try: + cursor.executescript(""" + PRAGMA foreign_keys=off; + + BEGIN TRANSACTION; + + ALTER TABLE notification RENAME TO old_notification; + + CREATE TABLE IF NOT EXISTS notification ( + user_id TEXT, + channel_id TEXT, + role_id TEXT, + enabled INTEGER DEFAULT 1, + enable_type TEXT DEFAULT '11', + customized_msg TEXT DEFAULT NULL, + FOREIGN KEY (user_id) REFERENCES user (id), + FOREIGN KEY (channel_id) REFERENCES channel (id), + PRIMARY KEY(user_id, channel_id) + ); + + INSERT INTO notification (user_id, channel_id, role_id, enabled, customized_msg) + SELECT user_id, channel_id, role_id, enabled, customized_msg + FROM old_notification; + + DROP TABLE old_notification; + + COMMIT; + + PRAGMA foreign_keys=on; + """) + await itn.followup.send('Successfully upgraded to 0.4.1. You can remove this cog and reboot the bot.') + except Exception as e: + await itn.followup.send(f'Upgrading to 0.4.1 failed. Please try again or contact the author. Error: {str(e)}') + finally: + conn.close() + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) + +``` + +
+ +
+ ⬆️click here to upgrade from 0.3.5 to 0.4 + +⚠️Before everything starts you must upgrade the version of `tweety-ns` to `1.0.9.2` first and download or pull the new code from this repo. + +Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade version` to upgrade. This cog can be removed after the upgrade is completed. + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.permission import ADMINISTRATOR + +class Upgrade(Cog_Extension): + + upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) + + @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + try: + cursor.executescript(""" + ALTER TABLE user ADD enabled INTEGER DEFAULT 1; + ALTER TABLE notification ADD customized_msg TEXT DEFAULT NULL; + """) + await itn.followup.send('successfully upgrade to 0.4, you can remove this cog and reboot the bot.') + except: + await itn.followup.send('upgrading to 0.4 failed, please try again or contact the author.') + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) +``` + +
+ +
+ ⬆️click here to upgrade from 0.3.4 to 0.3.5 + +Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade` to upgrade. This cog can be removed after the upgrade is completed. + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.log import setup_logger +from src.permission_check import is_administrator + +log = setup_logger(__name__) + +class Upgrade(Cog_Extension): + + @is_administrator() + @app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + cursor.executescript('ALTER TABLE channel ADD server_id TEXT') + + cursor.execute('SELECT id FROM channel') + channels = cursor.fetchall() + + for c in channels: + try: + channel = self.bot.get_channel(int(c[0])) + cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id)) + except: + log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.') + + + conn.commit() + conn.close() + + await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.') + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) +``` + +
+ +
+ ⬆️click here to upgrade from 0.3.3 to 0.3.4 + +Because the database structure has been updated, you must use the following code to update the database structure. + +```py +from dotenv import load_dotenv +import os +import sqlite3 + +load_dotenv() + +conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") +cursor = conn.cursor() + +cursor.execute('ALTER TABLE notification ADD enabled INTEGER DEFAULT 1') + +conn.commit() +conn.close() +``` + +
+ +## 繁體中文 + +
+ ⬆️0.4升級到0.4.1請點這裡 + +在 `cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade version` 進行升級。升級結束後可以移除這個cog。 + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.permission import ADMINISTRATOR + +class Upgrade(Cog_Extension): + + upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) + + @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4.1') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + try: + cursor.executescript(""" + PRAGMA foreign_keys=off; + + BEGIN TRANSACTION; + + ALTER TABLE notification RENAME TO old_notification; + + CREATE TABLE IF NOT EXISTS notification ( + user_id TEXT, + channel_id TEXT, + role_id TEXT, + enabled INTEGER DEFAULT 1, + enable_type TEXT DEFAULT '11', + customized_msg TEXT DEFAULT NULL, + FOREIGN KEY (user_id) REFERENCES user (id), + FOREIGN KEY (channel_id) REFERENCES channel (id), + PRIMARY KEY(user_id, channel_id) + ); + + INSERT INTO notification (user_id, channel_id, role_id, enabled, customized_msg) + SELECT user_id, channel_id, role_id, enabled, customized_msg + FROM old_notification; + + DROP TABLE old_notification; + + COMMIT; + + PRAGMA foreign_keys=on; + """) + await itn.followup.send('Successfully upgraded to 0.4.1. You can remove this cog and reboot the bot.') + except Exception as e: + await itn.followup.send(f'Upgrading to 0.4.1 failed. Please try again or contact the author. Error: {str(e)}') + finally: + conn.close() + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) + +``` + +
+ +
+ ⬆️0.3.5升級到0.4請點這裡 + +⚠️在一切開始之前請先更新 `tweety-ns` 至 `1.0.9.2` 版本並且從這個repo下載或拉取新的程式碼。 + +在 `cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade version` 進行升級。升級結束後可以移除這個cog。 + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.permission import ADMINISTRATOR + +class Upgrade(Cog_Extension): + + upgrade_group = app_commands.Group(name='upgrade', description='Upgrade something', default_permissions=ADMINISTRATOR) + + @upgrade_group.command(name='version', description='upgrade to Tweetcord 0.4') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + try: + cursor.executescript(""" + ALTER TABLE user ADD enabled INTEGER DEFAULT 1; + ALTER TABLE notification ADD customized_msg TEXT DEFAULT NULL; + """) + await itn.followup.send('successfully upgrade to 0.4, you can remove this cog and reboot the bot.') + except: + await itn.followup.send('upgrading to 0.4 failed, please try again or contact the author.') + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) +``` + +
+ +
+ ⬆️0.3.4升級到0.3.5請點這裡 + +在 `cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade` 進行升級。升級結束後可以移除這個cog。 + +```py +import discord +from discord import app_commands +from core.classes import Cog_Extension +import sqlite3 +import os + +from src.log import setup_logger +from src.permission_check import is_administrator + +log = setup_logger(__name__) + +class Upgrade(Cog_Extension): + + @is_administrator() + @app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5') + async def upgrade(self, itn: discord.Interaction): + + await itn.response.defer(ephemeral=True) + + conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") + cursor = conn.cursor() + + cursor.executescript('ALTER TABLE channel ADD server_id TEXT') + + cursor.execute('SELECT id FROM channel') + channels = cursor.fetchall() + + for c in channels: + try: + channel = self.bot.get_channel(int(c[0])) + cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id)) + except: + log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.') + + + conn.commit() + conn.close() + + await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.') + + +async def setup(bot): + await bot.add_cog(Upgrade(bot)) +``` + +
+ +
+ ⬆️0.3.3升級到0.3.4請點這裡 + +因為資料庫結構更新因此必須使用以下程式碼更新資料庫結構。 + +```py +from dotenv import load_dotenv +import os +import sqlite3 + +load_dotenv() + +conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db") +cursor = conn.cursor() + +cursor.execute('ALTER TABLE notification ADD enabled INTEGER DEFAULT 1') + +conn.commit() +conn.close() +``` + +
\ No newline at end of file