58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
|
import asyncio
|
||
|
|
||
|
from snekbot.bot import Bot
|
||
|
|
||
|
|
||
|
class ExampleBot(Bot):
|
||
|
|
||
|
async def on_join(self, data):
|
||
|
print(f"I joined f{data.channel_uid}!")
|
||
|
|
||
|
async def on_leave(self, data):
|
||
|
print(f"I left f{data.channel_uid}!")
|
||
|
|
||
|
async def on_ping(self, data):
|
||
|
print(f"Ping from f{data.user_nick}")
|
||
|
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 :]
|
||
|
print(f"Mention from f{data.user_nick}: {message}")
|
||
|
|
||
|
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):
|
||
|
print(f"Message from f{data.user_nick}: {data.message}")
|
||
|
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)
|
||
|
|
||
|
|
||
|
bot = ExampleBot(username="example", password="xxxxxx")
|
||
|
asyncio.run(bot.run())
|