-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
119 lines (94 loc) · 3.29 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import asyncio
import logging
import os
from dotenv import load_dotenv
from wiring import (
Bot,
MultiPlatformMessage,
MultiPlatformBot,
MultiPlatformUser,
Command,
)
from wiring.errors.action_not_supported_error import ActionNotSupportedError
from wiring.platforms.discord import DiscordBot
from wiring.platforms.telegram import TelegramBot
from wiring.platforms.twitch import TwitchBot
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s: [%(levelname)s] %(name)s - %(message)s",
datefmt="%m/%d/%Y %I:%M:%S %p",
)
logger = logging.getLogger()
async def send_commands_list(bot: Bot, message: MultiPlatformMessage, args: list[str]):
if message.chat is None:
return
await bot.send_message(
message.chat.id,
"available commands:\n"
+ "\n".join(["/" + str(command.name) for command in bot.commands]),
reply_message_id=message.id,
)
async def send_greetings(bot: Bot, user: MultiPlatformUser):
if user.from_chat_group is not None:
for chat in await bot.get_chats_from_group(user.from_chat_group.id):
try:
await bot.send_message(chat.id, "hi " + user.username)
except Exception as error:
logger.error(error)
async def ban(bot: Bot, message: MultiPlatformMessage, args: list[str]):
if message.chat is None or message.chat_group is None:
return
if len(args) < 1:
await bot.send_message(
message.chat.id, "specify a name/id of a user", reply_message_id=message.id
)
return
try:
user = await bot.get_user_by_name(
args[0].removeprefix("@"), message.chat_group.id
)
if user is not None:
await bot.ban(message.chat_group.id, user.id)
await bot.send_message(
message.chat.id, "banned", reply_message_id=message.id
)
else:
await bot.send_message(
message.chat.id, "cant find the user", reply_message_id=message.id
)
except ActionNotSupportedError:
await bot.send_message(
message.chat.id,
"banning is not supported here",
reply_message_id=message.id,
)
async def send_goodbye(bot: Bot, user: MultiPlatformUser):
if user.from_chat_group is not None:
for chat in await bot.get_chats_from_group(user.from_chat_group.id):
try:
await bot.send_message(chat.id, "bye " + user.username)
except Exception as error:
logger.error(error)
async def start_bots():
bot = MultiPlatformBot()
bot.platform_bots = [
DiscordBot(os.environ["DISCORD_BOT_TOKEN"]),
TelegramBot(os.environ["TELEGRAM_BOT_TOKEN"]),
TwitchBot(
os.environ["TWITCH_BOT_TOKEN"],
streamer_usernames_to_connect=[os.environ["TWITCH_TESTING_CHANNEL"]],
),
]
async with bot:
await bot.setup_commands(
[
Command(["start", "help", "help1"], send_commands_list),
Command("ban-user", ban),
],
"!",
)
bot.add_event_handler("join", send_greetings)
bot.add_event_handler("leave", send_goodbye)
await bot.listen_to_events()
asyncio.run(start_bots())