2025-02-01 14:58:18 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from snekbot.bot import Bot
|
|
|
|
|
|
|
|
|
|
|
|
class ExampleBot(Bot):
|
|
|
|
|
|
|
|
async def on_join(self, data):
|
2025-02-01 15:16:25 +00:00
|
|
|
print(f"I joined {data.channel_uid}!")
|
2025-02-01 14:58:18 +00:00
|
|
|
|
|
|
|
async def on_leave(self, data):
|
2025-02-01 15:16:25 +00:00
|
|
|
print(f"I left {data.channel_uid}!")
|
2025-02-01 14:58:18 +00:00
|
|
|
|
|
|
|
async def on_ping(self, data):
|
2025-02-01 15:16:25 +00:00
|
|
|
print(f"Ping from {data.user_nick}")
|
2025-02-01 14:58:18 +00:00
|
|
|
await self.send_message(
|
|
|
|
data.channel_uid,
|
|
|
|
"I should respond with Bong according to BordedDev. So here, bong!",
|
|
|
|
)
|
|
|
|
|
|
|
|
async def on_own_message(self, data):
|
|
|
|
print(f"Received my own message: {data.message}")
|
|
|
|
|
|
|
|
async def on_mention(self, data):
|
|
|
|
|
|
|
|
message = data.message[len(self.username) + 2 :]
|
2025-02-01 15:16:25 +00:00
|
|
|
print(f"Mention from {data.user_nick}: {message}")
|
2025-02-01 14:58:18 +00:00
|
|
|
|
|
|
|
result = f'Hey {data.user_nick}, Thanks for mentioning me "{message}".'
|
|
|
|
if "source" in message:
|
|
|
|
with open(__file__) as f:
|
|
|
|
result = f.read()
|
|
|
|
result = result.replace(f'"{self.username}"', '"example username"')
|
|
|
|
result = result.replace(self.password, "example password")
|
|
|
|
result = (
|
|
|
|
"This is the actual source code running me now. Fresh from the bakery:\n\n```python\n"
|
|
|
|
+ result
|
|
|
|
+ "\n```"
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.send_message(data.channel_uid, result)
|
|
|
|
|
|
|
|
async def on_message(self, data):
|
2025-02-01 15:16:25 +00:00
|
|
|
print(f"Message from {data.user_nick}: {data.message}")
|
2025-02-01 14:58:18 +00:00
|
|
|
message = data.message.lower()
|
|
|
|
result = None
|
|
|
|
if "hey" in message or "hello" in message:
|
|
|
|
result = f"Hi {data.user_nick}"
|
|
|
|
elif "bye" in message:
|
|
|
|
result = f"Bye {data.user_nick}"
|
|
|
|
|
|
|
|
if result:
|
|
|
|
await self.send_message(data.channel_uid, result)
|
|
|
|
|
|
|
|
|
2025-02-01 15:16:25 +00:00
|
|
|
bot = ExampleBot(username="example", password="example")
|
2025-02-01 14:58:18 +00:00
|
|
|
asyncio.run(bot.run())
|